Esempio n. 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);
            }
        }
Esempio n. 2
0
        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);
            }
        }