public async Task <Stream> GetStreamAsync(string url, RestParameters parameters)
        {
            //var handler = _modernHttpClient.GetNativeHandler();
            //var outerHandler = new RetryHandler(handler, 3);
            //var client = _modernHttpClient.Get(outerHandler);
            var client = new HttpClient();

            client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
            client.MaxResponseContentBufferSize = 3000000;

            try
            {
                var result = await client.GetStreamAsync(url);

                return(result);
            }
            catch (NotFoundException)
            {
            }
            catch (Exception)
            {
            }

            return(null);
        }
 public static RestParameters AddNoCache(this RestParameters parameters)
 {
     parameters.AddParameter("nocache", DateTime.UtcNow.Ticks);
     parameters.AddHeader("Cache-Control", "no-cache");
     parameters.AddHeader("Pragma", "no-cache");
     return(parameters);
 }
        public static RestParameters AddBasicAuthorization(this RestParameters parameters, string userName,
                                                           string password)
        {
            var encoding = Encoding.GetEncoding("iso-8859-1");
            var userPass = String.Format("{0}:{1}", userName, password);
            var encoded  = Convert.ToBase64String(encoding.GetBytes(userPass));

            return(parameters.AddAuthorizationHeader("Basic", encoded));
        }
 public SimpleRestClient(IMvxJsonConverter jsonConverter)
 {
     _jsonConverter = jsonConverter;
     if (String.IsNullOrWhiteSpace(MediaType))
     {
         MediaType = "application/json";
     }
     if (DefaultParameters == null)
     {
         DefaultParameters = new RestParameters();
     }
 }
        public static RestParameters AddAuthorizationHeader(this RestParameters parameters, string securityMethod,
                                                            string securityToken)
        {
            //TODO: If there's already an authorization param, replace it

            if (String.IsNullOrWhiteSpace(securityMethod) || String.IsNullOrWhiteSpace(securityToken))
            {
                //Can't add authorization with no token
                return(parameters);
            }
            parameters.AddParameter(securityMethod, securityToken, RestParameterTypes.Authorization);
            return(parameters);
        }
        public async Task <TResult> MakeRequestAsync <TResult>(string resource, HttpMethod methodType,
                                                               RestParameters parameters, int retries = 3, CancellationTokenSource token = null, string mediaType = null)
        {
            Mvx.TaggedTrace("RestClient", "MakeRequestAsync - Start - {0})", resource);

            _currentToken = token ?? new CancellationTokenSource();

            //var handler = _modernHttpClient.GetNativeHandler();
            //var outerHandler = new RetryHandler(handler, retries);
            //var client = _modernHttpClient.Get(outerHandler);

            var client = new HttpClient();

            var allParams = new RestParameters();

            allParams.AddRange(DefaultParameters);
            allParams.AddRange(parameters);

            // Build the uri
            var relativeAddress = BuildUri(BaseUrl, resource, allParams);

            client.BaseAddress = new Uri(BaseUrl);
            var requestMediaType = String.IsNullOrWhiteSpace(mediaType) ? MediaType : mediaType;

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(requestMediaType));

            //Add Authorization
            var authorizationParam = allParams.FirstOrDefault(p => p.ParameterType == RestParameterTypes.Authorization);

            if (authorizationParam != null)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorizationParam.Name, authorizationParam.Value.ToString());
            }

            var request = new HttpRequestMessage(methodType, relativeAddress);

            //Add Body
            var bodyParam = allParams.FirstOrDefault(p => p.ParameterType == RestParameterTypes.Body);

            if (bodyParam != null)
            {
                if (bodyParam.Value.GetType() == typeof(MemoryStream))
                {
                    request.Content = new StreamContent(bodyParam.Value as MemoryStream);
                }
                else
                {
                    var serialized = Serialize(bodyParam.Value, requestMediaType);
                    request.Content = new StringContent(serialized, Encoding.UTF8, requestMediaType);
                }
            }

            //Add headers
            foreach (var p in allParams.Where(p => p.ParameterType == RestParameterTypes.Header))
            {
                request.Headers.Add(p.Name, p.Value.ToString());
            }

            //Make Request
            Mvx.TaggedTrace("RestClient", "MakeRequestAsync - Request - {0}", request.RequestUri.ToString());
            var result = await client.SendAsync(request, _currentToken.Token);

            Mvx.TaggedTrace("RestClient", "MakeRequestAsync - Result - {0} - {1}", request.RequestUri.ToString(), result.StatusCode);

            switch (result.StatusCode)
            {
            case HttpStatusCode.Forbidden:
            case HttpStatusCode.Unauthorized:
                result.Dispose();
                throw new UnAuthorizedException(request.RequestUri.ToString());

            case HttpStatusCode.InternalServerError:
            case HttpStatusCode.BadRequest:
                try
                {
                    //Error message might be in the content, try to get it and pass it up to the caller
                    var content = await result.Content.ReadAsStringAsync();

                    result.Dispose();
                    throw new BadRequestException(content);
                }
                catch (Exception ex)
                {
                    throw new BadRequestException(ex.Message);
                }

            case HttpStatusCode.NotAcceptable:
                try
                {
                    var content = await result.Content.ReadAsStringAsync();

                    result.Dispose();
                    return(Deserialize <TResult>(content));
                }
                catch (Exception ex)
                {
                    throw new NotFoundException(request.RequestUri.ToString(), ex);
                }

            case HttpStatusCode.NotFound:
                throw new NotFoundException(request.RequestUri.ToString());

            case HttpStatusCode.NoContent:
                throw new NoContentException("NoContent");

            case HttpStatusCode.Accepted:
            case HttpStatusCode.Created:
            case HttpStatusCode.OK:
                var contents = await result.Content.ReadAsStringAsync();

                result.Dispose();
                return(Deserialize <TResult>(contents));

            default:
                result.Dispose();
                throw new NetworkException(request.RequestUri.ToString());
            }
        }
 public static RestParameters AddPagingParameters(this RestParameters parameters, int pageNumber, int pageSize)
 {
     parameters.AddParameter("page", pageNumber);
     parameters.AddParameter("size", pageSize);
     return(parameters);
 }