Пример #1
0
        public async Task <T> ProcessStreamAsync <T>(
            HttpSourceRequest request,
            Func <Stream, Task <T> > processAsync,
            SourceCacheContext cacheContext,
            ILogger log,
            CancellationToken token)
        {
            return(await ProcessResponseAsync(
                       request,
                       async response =>
            {
                if ((request.IgnoreNotFounds && response.StatusCode == HttpStatusCode.NotFound) ||
                    response.StatusCode == HttpStatusCode.NoContent)
                {
                    return await processAsync(null);
                }

                response.EnsureSuccessStatusCode();

                var networkStream = await response.Content.ReadAsStreamAsync();
                return await processAsync(networkStream);
            },
                       cacheContext,
                       log,
                       token));
        }
Пример #2
0
        public async Task <T> ProcessResponseAsync <T>(
            HttpSourceRequest request,
            Func <HttpResponseMessage, Task <T> > processAsync,
            SourceCacheContext cacheContext,
            ILogger log,
            CancellationToken token)
        {
            // Generate a new session id if no cache context was provided.
            var sessionId = cacheContext?.SessionId ?? Guid.NewGuid();

            Task <ThrottledResponse> throttledResponseFactory() => GetThrottledResponse(
                request.RequestFactory,
                request.RequestTimeout,
                request.DownloadTimeout,
                request.MaxTries,
                request.IsRetry,
                request.IsLastAttempt,
                sessionId,
                log,
                token);

            using (var throttledResponse = await throttledResponseFactory())
            {
                return(await processAsync(throttledResponse.Response));
            }
        }
Пример #3
0
 public Task <T> ProcessResponseAsync <T>(
     HttpSourceRequest request,
     Func <HttpResponseMessage, Task <T> > processAsync,
     ILogger log,
     CancellationToken token)
 {
     return(ProcessResponseAsync(request, processAsync, cacheContext: null, log: log, token: token));
 }
Пример #4
0
 public Task <T> ProcessStreamAsync <T>(
     HttpSourceRequest request,
     Func <Stream, Task <T> > processAsync,
     ILogger log,
     CancellationToken token)
 {
     return(ProcessStreamAsync <T>(request, processAsync, cacheContext: null, log: log, token: token));
 }
Пример #5
0
        public async Task <JObject> GetJObjectAsync(HttpSourceRequest request, ILogger log, CancellationToken token)
        {
            return(await ProcessStreamAsync(
                       request,
                       processAsync : stream =>
            {
                if (stream == null)
                {
                    return Task.FromResult <JObject>(null);
                }

                return stream.AsJObjectAsync();
            },
                       log : log,
                       token : token));
        }
Пример #6
0
        public async Task <T> ProcessResponseAsync <T>(
            HttpSourceRequest request,
            Func <HttpResponseMessage, Task <T> > processAsync,
            ILogger log,
            CancellationToken token)
        {
            Func <Task <ThrottledResponse> > throttledResponseFactory = () => GetThrottledResponse(
                request.RequestFactory,
                request.RequestTimeout,
                request.DownloadTimeout,
                log,
                token);

            using (var throttledResponse = await throttledResponseFactory())
            {
                return(await processAsync(throttledResponse.Response));
            }
        }
Пример #7
0
        public async Task <JObject> GetJObjectAsync(HttpSourceRequest request, ILogger log, CancellationToken token)
        {
            return(await ProcessStreamAsync(
                       request,
                       processAsync : stream =>
            {
                if (stream == null)
                {
                    return Task.FromResult((JObject)null);
                }

                using (var reader = new StreamReader(stream))
                    using (var jsonReader = new JsonTextReader(reader))
                    {
                        return Task.FromResult(JObject.Load(jsonReader));
                    }
            },
                       log : log,
                       token : token));
        }
Пример #8
0
        internal async Task <T> ProcessHttpStreamAsync <T>(
            HttpSourceRequest request,
            Func <HttpResponseMessage, Task <T> > processAsync,
            ILogger log,
            CancellationToken token)
        {
            return(await ProcessResponseAsync(
                       request,
                       async response =>
            {
                if ((request.IgnoreNotFounds && response.StatusCode == HttpStatusCode.NotFound) ||
                    response.StatusCode == HttpStatusCode.NoContent)
                {
                    return await processAsync(null);
                }

                response.EnsureSuccessStatusCode();

                return await processAsync(response);
            },
                       cacheContext : null,
                       log,
                       token));
        }