Exemplo n.º 1
0
        static async Task TestProjects(SpeckleApiClient myClient)
        {
            string projectId = "lol";

            Console.WriteLine();
            try
            {
                Console.WriteLine("Creating a project.");
                var Response = await myClient.ProjectCreateAsync(new Project()
                {
                    Name = "A simple project"
                });

                Console.WriteLine("OK: " + Response.Resource.ToJson());

                projectId = Response.Resource._id;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine();
            try
            {
                Console.WriteLine("Updating a project.");
                var Response = await myClient.ProjectUpdateAsync(projectId, new Project()
                {
                    Name = "A more complicated project", Private = false
                });

                Console.WriteLine("OK: " + Response.Resource.ToJson());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine();
            try
            {
                Console.WriteLine("Getting a project.");
                var Response = await myClient.ProjectGetAsync(projectId);

                Console.WriteLine("OK: " + Response.Resource.ToJson());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine();
            try
            {
                Console.WriteLine("Getting all users projects.");
                var Response = await myClient.ProjectGetAllAsync();

                Console.WriteLine("OK: " + Response.Resources.Count);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine();
            try
            {
                Console.WriteLine("Deleteing  a project.");
                var Response = await myClient.ProjectDeleteAsync(projectId);

                Console.WriteLine("OK: " + Response.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //var streams = new List<SpeckleStream>();
            var     streamObjects = new List <object>();
            var     streamIds     = new List <string>();
            Project project       = null;
            Account account       = null;

            DA.GetData("Project", ref project);
            DA.GetDataList("Streams", streamObjects);
            DA.GetData("Account", ref account);

            if (null == project)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
                                  "You must connect an account to this component");
                return;
            }

            if (null == streamObjects)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
                                  "You must connect at least one stream to this component");
                return;
            }

            if (null == project)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,
                                  "You must connect a project to this component");
                return;
            }

            foreach (object streamObject in streamObjects)
            {
                if (streamObject is Grasshopper.Kernel.Types.GH_String)
                {
                    var streamId = (streamObject as Grasshopper.Kernel.Types.GH_String).Value;
                    streamIds.Add(streamId);
                    continue;
                }
                if (streamObject is Grasshopper.Kernel.Types.GH_ObjectWrapper)
                {
                    var stream = (SpeckleStream)(streamObject as Grasshopper.Kernel.Types.GH_ObjectWrapper).Value;
                    if (null != stream)
                    {
                        streamIds.Add(stream.StreamId);
                        continue;
                    }
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "This component only works with streamIds or SpeckleStream objects");
                }
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "This component only works with streamIds or SpeckleStream objects");
                return;
            }

            if (null != AddedStreamIds && AddedStreamIds.Count > 0)
            {
                string addedList = "";
                foreach (string streamId in AddedStreamIds)
                {
                    addedList += streamId + ",";
                }
                addedList = addedList.Substring(0, addedList.Length - 1);
                AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Added " +
                                  addedList +
                                  "to the project");
            }

            //Do nothing if the project already contains these streams
            var streamsToAdd = streamIds.Where(streamId => !project.Streams.Contains(streamId)).ToList();

            if (streamsToAdd.Count() < 1)
            {
                AddedStreamIds.Clear();
                return;
            }

            project.Streams.AddRange(streamsToAdd);

            Client.BaseUrl   = account.RestApi;
            Client.AuthToken = account.Token;

            Client.ProjectUpdateAsync(project._id, project).ContinueWith(task =>
            {
                if (task.Result.Success == false)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, task.Result.Message);
                    return;
                }
                AddedStreamIds.Clear();
                AddedStreamIds.AddRange(streamsToAdd);
                Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponent);
            });
        }