コード例 #1
0
        private async Task <ArraySegment <byte> > InternalExecuteAsync(FormattedRoute route, IRestRequestContent content, IRestRequestOptions options)
        {
            var request = new DefaultRestRequest(route, content, options);

            if (options is DefaultRestRequestOptions defaultOptions)
            {
                defaultOptions.RequestAction?.Invoke(request);
            }

            await RateLimiter.EnqueueRequestAsync(request).ConfigureAwait(false);

            var response = await request.WaitAsync().ConfigureAwait(false);

            using (var jsonStream = await response.HttpResponse.ReadAsync().ConfigureAwait(false))
            {
                var stream = new MemoryStream((int)jsonStream.Length);
                await jsonStream.CopyToAsync(stream).ConfigureAwait(false);

                stream.TryGetBuffer(out var buffer);

                if (!IsValidResponse(response))
                {
                    var errorModel = Serializer.Deserialize <RestApiErrorJsonModel>(buffer);
                    throw new RestApiException(response.HttpResponse.Code, errorModel);
                }

                return(buffer);
            }
        }
コード例 #2
0
        private async ValueTask <Stream> InternalExecuteAsync(FormattedRoute route, IRestRequestContent content, IRestRequestOptions options)
        {
            content?.Validate();

            var request = new DefaultRestRequest(route, content, options);

            var defaultOptions = options as DefaultRestRequestOptions;

            defaultOptions?.RequestAction?.Invoke(request);

            await RateLimiter.EnqueueRequestAsync(request).ConfigureAwait(false);

            var response = await request.WaitForCompletionAsync().ConfigureAwait(false);

            defaultOptions?.HeadersAction?.Invoke(new DefaultRestResponseHeaders(response.HttpResponse.Headers));

            var responseStream = await response.HttpResponse.ReadAsync().ConfigureAwait(false);

            var statusCode = (int)response.HttpResponse.Code;

            if (statusCode > 199 && statusCode < 300)
            {
                return(responseStream);
            }

            if (statusCode > 499 && statusCode < 600)
            {
                throw new RestApiException(response.HttpResponse.Code, response.HttpResponse.ReasonPhrase, null);
            }

            RestApiErrorJsonModel errorModel = null;

            try
            {
                errorModel = Serializer.Deserialize <RestApiErrorJsonModel>(responseStream);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "An exception occurred while attempting to deserialize the error model.");
            }
            finally
            {
                await responseStream.DisposeAsync().ConfigureAwait(false);
            }

            throw new RestApiException(response.HttpResponse.Code, response.HttpResponse.ReasonPhrase, errorModel);
        }
コード例 #3
0
ファイル: DefaultRestApiClient.cs プロジェクト: Quahu/Disqord
        private async ValueTask <Stream> InternalExecuteAsync(FormattedRoute route, IRestRequestContent content, IRestRequestOptions options)
        {
            var request        = new DefaultRestRequest(route, content, options);
            var defaultOptions = options as DefaultRestRequestOptions;

            defaultOptions?.RequestAction?.Invoke(request);
            await RateLimiter.EnqueueRequestAsync(request).ConfigureAwait(false);

            var response = await request.WaitAsync().ConfigureAwait(false);

            defaultOptions?.HeadersAction?.Invoke(new DefaultRestResponseHeaders(response.HttpResponse.Headers));
            var jsonStream = await response.HttpResponse.ReadAsync().ConfigureAwait(false);

            if (IsValidResponse(response))
            {
                return(jsonStream);
            }

            await using (jsonStream)
            {
                var errorModel = Serializer.Deserialize <RestApiErrorJsonModel>(jsonStream);
                throw new RestApiException(response.HttpResponse.Code, errorModel);
            }
        }