Пример #1
0
        public async Task <T> SendRequestAsync <T>(Uri serverUri, SeafRequest <T> request)
        {
            HttpRequestMessage requestMessage = CreateHttpRequestMessage(serverUri, request);

            HttpResponseMessage response;
            HttpClientHandler   handler = new HttpClientHandler();

            handler.AllowAutoRedirect = false;
            using (HttpClient client = new HttpClient(handler))
                response = await client.SendAsync(requestMessage);

            if (request.WasSuccessful(response))
            {
                return(await request.ParseResponseAsync(response));
            }
            else
            {
                throw new SeafException(request.GetSeafError(response));
            }
        }
Пример #2
0
        SeafException GetSeafException <T>(SeafRequest <T> request, HttpResponseMessage response)
        {
            if (response.StatusCode == (System.Net.HttpStatusCode) 429) // TooManyRequests
            {
                IEnumerable <string> values;
                if (response.Headers.TryGetValues("X-Throttle-Wait-Seconds", out values))
                {
                    int seconds;
                    if (int.TryParse(values.First(), out seconds))
                    {
                        return(new SeafTooManyRequestsException(TimeSpan.FromSeconds(seconds)));
                    }
                }

                // could not read the wait header
                return(new SeafTooManyRequestsException(TimeSpan.FromSeconds(30)));
            }
            else
            {
                return(new SeafException(request.GetSeafError(response)));
            }
        }