Ejemplo n.º 1
0
        public async Task <HttpResponseMessage> MakeRequestAsync(ClientSet clientSet, string authorization, IList <string> cookies, string path, string method, string body, CancellationToken?token = null)
        {
            if (token == null)
            {
                token = CancellationToken.None;
            }
            ConnectionHolder connectionHolder = GetRandom(ClientsFor(clientSet));

            return(await MakeRequestAsync(connectionHolder, authorization, cookies, path, method, body, token));
        }
Ejemplo n.º 2
0
        private async Task <HttpResponseMessage> MakeRequestAsync(ConnectionHolder connectionHolder, string?authorization, IList <string>?cookies, string path, string method, string body, CancellationToken?token = null)
        {
            if (token == null)
            {
                token = CancellationToken.None;
            }

            HttpClient         client  = connectionHolder.Client;
            HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), $"{connectionHolder.Url}{path}");

            if (body != null)
            {
                request.Content = new StringContent(body, Encoding.UTF8, "application/json");
            }

            HttpRequestHeaders headers = request.Headers;

            if (connectionHolder.HostHeader != null)
            {
                headers.Host = connectionHolder.HostHeader;
            }

            if (authorization != null)
            {
                headers.Add("Authorization", authorization);
            }

            if (cookies != null && cookies.Count > 0)
            {
                headers.Add("Cookie", string.Join("; ", cookies));
            }

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(request, token.Value);

                if (response.IsSuccessStatusCode)
                {
                    return(response);
                }
            }
            catch (Exception ex)
            {
                throw new PushNetworkException(ex);
            }

            switch ((int)response.StatusCode)
            {
            case 401:
            case 403:
                throw new AuthorizationFailedException((int)response.StatusCode, "Authorization failed!");

            case 409:
                throw new RemoteAttestationResponseExpiredException("Remote attestation response expired");

            case 429:
                throw new RateLimitException($"Rate limit exceeded: {response.StatusCode}");
            }

            if (response.Content != null)
            {
                throw new NonSuccessfulResponseCodeException((int)response.StatusCode,
                                                             $"Response: {await response.Content.ReadAsStringAsync()}");
            }
            else
            {
                throw new NonSuccessfulResponseCodeException((int)response.StatusCode, $"Response: null");
            }
        }