Example #1
0
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Account Account = null;

            DA.GetData(0, ref Account);

            if (Account == null)
            {
                AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Couldn't set the account");
                return;
            }

            DA.SetDataList(0, UserStreams);

            Client.BaseUrl = Account.RestApi; Client.AuthToken = Account.Token;
            Client.StreamsGetAllAsync("fields=streamId,name,description&isComputedResult=false&deleted=false").ContinueWith(tsk =>
            {
                if (tsk.Result.Success == false)
                {
                    AddRuntimeMessage(GH_RuntimeMessageLevel.Error, tsk.Result.Message);
                    return;
                }
                var newStreams = tsk.Result.Resources.ToList();
                var notUpdated = UserStreams.Select(x => x._id).SequenceEqual(newStreams.Select(x => x._id));

                if (!notUpdated)
                {
                    UserStreams = tsk.Result.Resources.ToList();
                    Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponent);
                }
            });
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            object         preAccount = null;
            SpeckleAccount Account    = null;

            DA.GetData(0, ref preAccount);

            Account = ((SpeckleAccount)preAccount.GetType().GetProperty("Value").GetValue(preAccount, null));

            if (Account == null)
            {
                return;
            }

            DA.SetDataList(0, UserStreams);

            if (Account == OldAccount)
            {
                return;
            }

            OldAccount = Account;

            Client.BaseUrl = Account.restApi; Client.AuthToken = Account.apiToken;
            Client.StreamsGetAllAsync().ContinueWith(tsk =>
            {
                UserStreams = tsk.Result.Resources.ToList();
                Rhino.RhinoApp.MainApplicationWindow.Invoke(ExpireComponent);
            });
        }
        /// <summary>
        /// Exposed method for users to call when trying to download the meta data of the Streams the current
        /// logged in user is able to access. Use this to get the IDs of the streams you would later want to start
        /// receiving or use the rest of the data to populate your UI with data describing the Streams that are
        /// available.
        /// </summary>
        /// <param name="callBack">A method callback which takes a <c>SpeckleStream</c> array.</param>
        /// <returns>An async Task which can be awaited with a coroutine or just ignored.</returns>
        /// <remarks>If download was successful, the resulting array is passed back. If failed, null
        /// is passed. Need to be using the <c>SpeckleCore</c> namespace to access this type.</remarks>
        public virtual async Task GetAllStreamMetaDataForUserAsync(Action <SpeckleStream[]> callBack)
        {
            if (loggedInUser == null)
            {
                throw new UnauthorizedAccessException("Need to be logged in before getting stream meta data.");
            }

            SpeckleApiClient userStreamsClient = new SpeckleApiClient(serverUrl.Trim());

            userStreamsClient.AuthToken = loggedInUser.Apitoken;

            ResponseStream streamsGet = await userStreamsClient.StreamsGetAllAsync(null);

            if (streamsGet == null)
            {
                Debug.LogError("Could not get streams for user");

                callBack?.Invoke(null);
            }
            else
            {
                List <SpeckleStream> streams = streamsGet.Resources;

                Debug.Log("Got " + streams.Count + " streams for user");

                callBack?.Invoke(streams.ToArray());
            }
        }
Example #4
0
        /// <summary>
        /// Returns streams associated with account.
        /// </summary>
        /// <param name="restApi">Server address</param>
        /// <param name="apiToken">API token for account</param>
        /// <returns>List of tuple containing the name and the streamID of each stream</returns>
        public static async Task <List <SpeckleStream> > GetStreams(string restApi, string apiToken, ISpeckleAppMessenger messenger)
        {
            SpeckleApiClient myClient = new SpeckleApiClient()
            {
                BaseUrl = restApi, AuthToken = apiToken
            };
            var queryString = "limit=500&sort=-updatedAt&parent=&fields=name,streamId,parent";

            try
            {
                ResponseStream response = await myClient.StreamsGetAllAsync(queryString);

                List <SpeckleStream> ret = new List <SpeckleStream>();

                foreach (SpeckleStream s in response.Resources.Where(r => r.Parent == null))
                {
                    ret.Add(s);
                }

                return(ret);
            }
            catch (SpeckleException se)
            {
                if (messenger != null)
                {
                    messenger.Message(MessageIntent.Display, MessageLevel.Error, "Unable to access stream list information from the server");
                    var context = new List <string>()
                    {
                        "Unable to access stream list information from the server",
                        "StatusCode=" + se.StatusCode, "ResponseData=" + se.Response, "Message=" + se.Message, "BaseUrl=" + restApi,
                        "Endpoint=StreamsGetAllAsync", "QueryString=\"" + queryString + "\""
                    };
                    if (se is SpeckleException <ResponseBase> && ((SpeckleException <ResponseBase>)se).Result != null)
                    {
                        var responseJson = ((SpeckleException <ResponseBase>)se).Result.ToJson();
                        context.Add("ResponseJson=" + responseJson);
                    }
                    messenger.Message(MessageIntent.TechnicalLog, MessageLevel.Error, se, context.ToArray());
                }
            }
            catch (Exception ex)
            {
                if (messenger != null)
                {
                    messenger.Message(MessageIntent.Display, MessageLevel.Error, "Unable to access stream list information from the server");
                    messenger.Message(MessageIntent.TechnicalLog, MessageLevel.Error, ex, "Unable to access stream list information",
                                      "BaseUrl=" + restApi);
                }
            }
            return(null);
        }
Example #5
0
        /// <summary>
        /// Returns streams associated with account.
        /// </summary>
        /// <param name="restApi">Server address</param>
        /// <param name="apiToken">API token for account</param>
        /// <returns>List of tuple containing the name and the streamID of each stream</returns>
        public static async Task <List <Tuple <string, string> > > GetStreams(string restApi, string apiToken)
        {
            SpeckleApiClient myClient = new SpeckleApiClient()
            {
                BaseUrl = restApi, AuthToken = apiToken
            };

            ResponseStream response = await myClient.StreamsGetAllAsync("fields=name,streamId");

            List <Tuple <string, string> > ret = new List <Tuple <string, string> >();

            foreach (SpeckleStream s in response.Resources)
            {
                ret.Add(new Tuple <string, string>(s.Name, s.StreamId));
            }

            return(ret);
        }
Example #6
0
        static async Task TestStreams(SpeckleApiClient myClient)
        {
            string streamId       = "lol";
            string secondStreamId = "hai";

            var myPoint = new SpecklePoint()
            {
                Value = new List <double>()
                {
                    1, 2, 3
                }
            };
            var mySecondPoint = new SpecklePoint()
            {
                Value = new List <double>()
                {
                    23, 33, 12
                }
            };
            var myCircle = new SpeckleCircle()
            {
                Radius = 21
            };


            myPoint.Properties = new Dictionary <string, object>();
            myPoint.Properties.Add("Really", mySecondPoint);

            myCircle.Properties = new Dictionary <string, object>();
            myCircle.Properties.Add("a property", "Hello!");
            myCircle.Properties.Add("point", myPoint);

            SpeckleStream myStream = new SpeckleStream()
            {
                Name    = "Hello World My Little Stream",
                Objects = new List <SpeckleObject>()
                {
                    myCircle, myPoint
                }
            };

            SpeckleStream secondStream = new SpeckleStream()
            {
                Name    = "Second Little Stream",
                Objects = new List <SpeckleObject>()
                {
                    myCircle, mySecondPoint
                }
            };

            Console.WriteLine();
            try
            {
                Console.WriteLine("Creating a stream.");
                var Response = await myClient.StreamCreateAsync(myStream);

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

                streamId = Response.Resource.StreamId;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine();
            try
            {
                Console.WriteLine("Creating a second stream.");
                var Response = await myClient.StreamCreateAsync(secondStream);

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

                secondStreamId = Response.Resource.StreamId;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            Console.WriteLine();
            try
            {
                Console.WriteLine("Diffing two streams!");
                var Response = await myClient.StreamDiffAsync(streamId, secondStreamId);

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

            Console.WriteLine();
            try
            {
                Console.WriteLine("Getting a stream.");
                var Response = await myClient.StreamGetAsync(streamId, null);

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

            Console.WriteLine();
            try
            {
                Console.WriteLine("Getting a stream's objects.");
                var Response = await myClient.StreamGetObjectsAsync(streamId, null);

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

            Console.WriteLine();
            try
            {
                Console.WriteLine("Updating a stream.");
                var Response = await myClient.StreamUpdateAsync(streamId, new SpeckleStream()
                {
                    Name = "I hate api testing", ViewerLayers = new List <object>()
                    {
                        new { test = "test" }
                    }
                });

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

            Console.WriteLine();
            try
            {
                Console.WriteLine("Getting a stream field.");
                var Response = await myClient.StreamGetAsync(streamId, "fields=viewerLayers,name,owner");

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

            Console.WriteLine();
            try
            {
                Console.WriteLine("Getting all users's streams.");
                var Response = await myClient.StreamsGetAllAsync();

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


            Console.WriteLine();
            try
            {
                Console.WriteLine("Cloning a stream.");
                var Response = await myClient.StreamCloneAsync(streamId);

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

            Console.WriteLine();
            try
            {
                Console.WriteLine("Deleting a stream: " + streamId);
                var Response = await myClient.StreamDeleteAsync(streamId);

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