Ejemplo n.º 1
0
        public void SetUp()
        {
            _filter = new Filter();
            _filter.AddFilter("-[mscorlib]*");
            _filter.AddFilter("-[System]*");
            _filter.AddFilter("-[System.*]*");
            _filter.AddFilter("-[Microsoft.VisualBasic]*");
            _filter.AddFilter("+[OpenCover.Samples.*]*");

            _commandLine = new Mock<ICommandLine>();

            var filePersistance = new BasePersistanceStub(_commandLine.Object);
            _persistance = filePersistance;
        }
Ejemplo n.º 2
0
        public void SetUp()
        {
            _filter = new Filter(false);
            _filter.AddFilter("-[mscorlib]*");
            _filter.AddFilter("-[System]*");
            _filter.AddFilter("-[System.*]*");
            _filter.AddFilter("-[Microsoft.VisualBasic]*");
            _filter.AddFilter("+[OpenCover.Samples.*]*");

            _commandLine = new Mock <ICommandLine>();
            _logger      = new Mock <ILog>();

            var filePersistance = new BasePersistanceStub(_commandLine.Object, _logger.Object);

            _persistance = filePersistance;
        }
Ejemplo n.º 3
0
        protected IClientResponse <T> GetData <T>(string resourceEndpoint, IFilter filter)
            where T : new()
        {
            var request = new RestRequest(resourceEndpoint);

            if (filter != null)
            {
                filter.AddFilter(request);
            }

            var response = RestGet <T>(request);

            log.DebugFormat("Response: {0}", response.StatusCode);
            var clientResponse = new ClientResponse <T>()
            {
                RestResponse = response,
            };

            if (response.Data != null)
            {
                clientResponse.Data = response.Data;
            }

            DeserializeErrorData <T>(clientResponse);
            return((IClientResponse <T>)clientResponse);
        }
Ejemplo n.º 4
0
        protected IClientResponse <T> Count <T>(string resourceEndpoint, IFilter filter)
            where T :  new()
        {
            var request = new RestRequest(resourceEndpoint);

            if (filter != null)
            {
                filter.AddFilter(request);
            }

            var response = RestGet <T>(request);

            var clientResponse = new ClientResponse <T>()
            {
                RestResponse = response,
            };

            if (response.Data != null)
            {
                clientResponse.Data = response.Data;
            }

            DeserializeErrorData <T>(clientResponse);
            return(clientResponse as IClientResponse <T>);
        }
Ejemplo n.º 5
0
        protected async Task <T> GetDataAsync <T>(string resourceEndpoint, IFilter filter = null)
        {
            var endpoint = (string.IsNullOrEmpty(resourceEndpoint)) ? BaseUri : string.Format("{0}/{1}", BaseUri, resourceEndpoint);

            // If you are supplying the filters yourself
            if (resourceEndpoint.StartsWith("?") || resourceEndpoint.Contains("/"))
            {
                endpoint = string.Format("{0}{1}", BaseUri, resourceEndpoint);
            }

            var response = await RetryPolicy.ExecuteAction(() =>
            {
                var message = new HttpRequestMessage(HttpMethod.Get, endpoint);

                if (filter != null && !resourceEndpoint.Contains("?"))
                {
                    filter.AddFilter(message);
                }

                var uri = message.RequestUri.ToString();

                var resp = ExecuteRequest <T>(message);
                return(resp);
            }).ConfigureAwait(false);

            return(response.Data);
        }
Ejemplo n.º 6
0
        private async Task <IRestResponse <T> > ExecuteRequest <T>(HttpRequestMessage message, IFilter filter = null)
        {
            IRestResponse <T> response;

            if (filter != null && !message.RequestUri.ToString().Contains("?"))
            {
                filter.AddFilter(message);
            }

            var credentials = new NetworkCredential(Configuration.UserName, Configuration.UserApiKey);
            var handler     = new HttpClientHandler {
                Credentials = credentials
            };

            using (var client = new HttpClient(handler))
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                if (AuthenticationDetails != null)
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthenticationDetails.Item1, AuthenticationDetails.Item2);
                }

                client.BaseAddress = Configuration.BaseUri;

                var httpResponse = await client.SendAsync(message);

                if (!httpResponse.IsSuccessStatusCode)
                {
                    switch (httpResponse.StatusCode)
                    {
                    case (HttpStatusCode)400:
                        throw new BadRequestException(httpResponse.ReasonPhrase, httpResponse);

                    case (HttpStatusCode)401:
                        throw new NotAuthorizedException(httpResponse.ReasonPhrase, httpResponse);

                    case (HttpStatusCode)404:
                        throw new NotFoundException(httpResponse.ReasonPhrase, httpResponse);

                    case (HttpStatusCode)405:
                        throw new MethodNotAllowedException(httpResponse.ReasonPhrase, httpResponse);

                    case (HttpStatusCode)429:
                        ApiLimitRemaining = int.Parse(httpResponse.Headers.GetValues("X-Rate-Limit-Remaining").FirstOrDefault());
                        LimitResetSeconds = int.Parse(httpResponse.Headers.GetValues("X-Rate-Limit-Reset").FirstOrDefault());
                        ApiLimit          = int.Parse(httpResponse.Headers.GetValues("X-Rate-Limit-Limit").FirstOrDefault());
                        throw new ApiLimitReachedException(
                                  $"{httpResponse.ReasonPhrase}\nThere are {LimitResetSeconds} seconds remaning until a request reset.",
                                  httpResponse,
                                  ApiLimitRemaining,
                                  LimitResetSeconds);

                    case (HttpStatusCode)500:
                        throw new InternalServerErrorException(httpResponse.ReasonPhrase, httpResponse);

                    default:
                        throw new RestException($"This error is not handled\n{httpResponse.ReasonPhrase}", httpResponse);
                    }
                }

                ApiLimitRemaining = int.Parse(httpResponse.Headers.GetValues("X-Rate-Limit-Remaining").FirstOrDefault());
                LimitResetSeconds = int.Parse(httpResponse.Headers.GetValues("X-Rate-Limit-Reset").FirstOrDefault());
                ApiLimit          = int.Parse(httpResponse.Headers.GetValues("X-Rate-Limit-Limit").FirstOrDefault());

                PrintApiLimitDetails(httpResponse);
                var headerString = "";

                foreach (var header in httpResponse.Headers)
                {
                    headerString += $"{header.Key}: {header.Value}\n";
                }

                var json = await httpResponse.Content.ReadAsStringAsync();

                ApiStateLogger.LogDebug(headerString + json);

                T responseData = JsonConvert.DeserializeObject <T>(json);

                response = new RestResponse <T>
                {
                    Data     = responseData,
                    Request  = message,
                    Response = httpResponse
                };

                OnRequestCompleted(response.Request, response.Response);
            }

            return(response);
        }