protected IEnumerable <RestItem> FindRelationAndFollow(string relation, string errorHeading, StringBuilder loggingStringBuilder)
        {
            var stopwatch = new Stopwatch();

            IEnumerable <RestItem> result = new List <RestItem>();

            if (State != null)
            {
                var theUri = FindRelationUri(relation);

                loggingStringBuilder.AppendFormat("Call to url={0} ", theUri);
                stopwatch.Start();
                var response = ConnectClient.Request <List <RestItem> >(theUri, Method.GET);

                loggingStringBuilder.AppendFormat("took duration={0}ms", stopwatch.ElapsedMilliseconds);
                if (response.ErrorException != null)
                {
                    RestErrorHelper.LogRestError(Logger, response, errorHeading);
                    throw new Exception(string.Format("Error calling {0}", theUri), response.ErrorException);
                }
                result = response.Data;
            }

            stopwatch.Stop();
            return(result);
        }
Esempio n. 2
0
        protected async ValueTask <IEnumerable <RestItem> > FindRelationAndFollowAsync(string relation, string errorHeading, StringBuilder loggingStringBuilder)
        {
            var stopwatch = new Stopwatch();

            IEnumerable <RestItem> result = new List <RestItem>();

            if (State != null)
            {
                var theUri = FindRelationUri(relation);

                loggingStringBuilder.AppendFormat("Call to url={0} ", theUri);
                stopwatch.Start();
                IRestResponse <List <RestItem> > response = new RestResponse <List <RestItem> >();
                int tryIterationCounter = 1;
                while (tryIterationCounter <= 3)
                {
                    try
                    {
                        response = await ConnectClient.RequestAsync <List <RestItem> >(theUri, Method.GET);

                        break;
                    }
                    catch (JsonSerializationException ex)
                    {
                        if (tryIterationCounter == 3)
                        {
                            Logger.LogWarning($"JsonSerializationException Method=FindRelationAndFollow {ex}");
                            return(null);
                        }
                        await Task.Delay(500);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    tryIterationCounter++;
                }

                loggingStringBuilder.AppendFormat("took duration={0}ms - ", stopwatch.ElapsedMilliseconds);
                if (response.ErrorException != null)
                {
                    RestErrorHelper.LogRestError(Logger, response, errorHeading);
                    throw new Exception($"Error calling {theUri} - ", response.ErrorException);
                }
                result = response.Data;
            }

            stopwatch.Stop();
            return(result);
        }
        private async Task <IEnumerable <RestItem> > GetRootAsync()
        {
            var stopwatch            = new Stopwatch();
            var messageStringBuilder = new StringBuilder("GetRoot request...");

            try
            {
                stopwatch.Start();

                var getRootResponse = await ConnectClient.LoginAsync();

                messageStringBuilder.AppendFormat("took {0}ms", stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();

                if (getRootResponse.ErrorException != null || getRootResponse.Content == null)
                {
                    RestErrorHelper.LogRestError(Logger, getRootResponse, "GetRoot HTTP error");
                    throw new Exception("Error calling GetRoot", getRootResponse.ErrorException);
                }

                if (getRootResponse.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new NotAuthenticatedException("Username or password are incorrect");
                }

                if (getRootResponse.Content != null)
                {
                    return(getRootResponse.Content.FromJson <List <RestItem> >());
                }
            }
            catch (NotAuthenticatedException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Logger.LogError("GetRoot exception", ex);
                throw;
            }
            finally
            {
                Logger.LogError(messageStringBuilder.ToString());
                stopwatch.Stop();
            }

            return(null);
        }
        protected string FindRelationAndFollowAsString(string relation, string errorHeading, StringBuilder loggingStringBuilder)
        {
            var stopwatch = new Stopwatch();
            var result    = string.Empty;

            if (State != null)
            {
                var theUri = FindRelationUri(relation);

                loggingStringBuilder.AppendFormat("Beginning call to url={0} ", theUri);
                stopwatch.Start();
                var response = ConnectClient.Request(theUri, Method.GET);
                loggingStringBuilder.AppendFormat("took duration={0}ms", stopwatch.ElapsedMilliseconds);
                if (response.ErrorException != null || response.Content == null)
                {
                    RestErrorHelper.LogRestError(Logger, response, errorHeading);
                    throw new Exception(string.Format("Error calling {0}", theUri), response.ErrorException);
                }
                result = response.Content;
            }
            stopwatch.Stop();
            return(result);
        }
        public void SendEcho()
        {
            if (string.IsNullOrEmpty(_virtualHost))
            {
                throw new Exception("virtualHost is not defined");
            }

            var link    = State.Links.First(restLink => restLink.Relation == "http://api.sportingsolutions.com/rels/stream/batchecho");
            var echouri = new Uri(link.Href);

            var streamEcho = new StreamEcho
            {
                Host    = _virtualHost,
                Message = Guid.NewGuid() + ";" + DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
            };

            var response = ConnectClient.Request(echouri, RestSharp.Method.POST, streamEcho, UDAPI.Configuration.ContentType, 3000);

            if (response.ErrorException != null || response.Content == null)
            {
                RestErrorHelper.LogRestError(Logger, response, "Error sending echo request");
                throw new Exception(string.Format("Error calling {0}", echouri), response.ErrorException);
            }
        }