public async Task ProcessAsync(HttpMessage message, ReadOnlyMemory <IHttpPipelinePolicy> pipeline, NextPolicy next)
 {
     message.Response = await _httpPipelineTransport.SendAsync(message);
 }
Ejemplo n.º 2
0
    public async Task ProcessAsync(HttpMessage message, ReadOnlyMemory <IHttpPipelinePolicy> pipeline, NextPolicy next)
    {
        var attempt = 0;
        List <Exception>?exceptions = null;

        do
        {
            Exception?lastException = null;

            try
            {
                attempt++;
                await next();
            }
            catch (Exception ex)
            {
                if (!message.ResponseClassifier.IsRetriableException(ex))
                {
                    throw;
                }

                _logger.LogWarning(ex, "Attempt {attempt} failed", attempt);

                lastException = ex;
                exceptions ??= new();
                exceptions.Add(ex);
            }

            var shouldRetry = attempt <= _options.MaxRetries;
            var delay       = TimeSpan.Zero;

            if (lastException != null)
            {
                if (shouldRetry)
                {
                    delay = _options.RetryDelay;
                }
                else
                {
                    if (exceptions !.Count == 1)
                    {
                        ExceptionDispatchInfo.Capture(exceptions[0]).Throw();
                    }
                    else
                    {
                        throw new AggregateException($"Request failed after {attempt} tries.", exceptions);
                    }
                }
            }
Ejemplo n.º 3
0
    public async Task ProcessAsync(HttpMessage message, ReadOnlyMemory <IHttpPipelinePolicy> pipeline, NextPolicy next)
    {
        await next();

        if (message.Request.ResponseType is Type responseType && message.Response.HttpResponseMessage != null)
        {
            var responseObject = JsonConvert.DeserializeObject(
                await message.Response.HttpResponseMessage.Content.ReadAsStringAsync(),
                responseType);

            message.Response.Content = responseObject;
        }
    }
    public async Task ProcessAsync(HttpMessage message, ReadOnlyMemory <IHttpPipelinePolicy> pipeline, NextPolicy next)
    {
        if (_memoryCache.TryGetValue(CacheKey(message.Request), out var value) &&
            value is not null &&
            value.GetType() == message.Request.ResponseType)
        {
            message.Response = new Response(value);

            // stop processing when cache is found
            return;
        }

        await next();

        if (message.Response.Content != null && message.Response.HttpResponseMessage?.IsSuccessStatusCode == true)
        {
            _memoryCache.Set(
                CacheKey(message.Request),
                message.Response.Content,
                TimeSpan.FromMinutes(1));
        }
    }
 public async Task ProcessAsync(HttpMessage message, ReadOnlyMemory <IHttpPipelinePolicy> pipeline, NextPolicy next)
 {
     message.Request.SetHeader("Authorization", $"Bearer {await _tokenCache.GetTokenAsync(GetScope(message))}");
     await next();
 }
Ejemplo n.º 6
0
    public async Task ProcessAsync(HttpMessage message, ReadOnlyMemory <IHttpPipelinePolicy> pipeline, NextPolicy next)
    {
        await next();

        // it is important to redact any PII here in real world scenarios

        var responseBody = message.Response.HttpResponseMessage?.Content is HttpContent content
            ? await content.ReadAsStringAsync()
            : default;

        _logger.LogInformation("Response body was: {responseBody}", responseBody);

        // more response details can be logged, like headers and stuff
    }
Ejemplo n.º 7
0
    public Task ProcessAsync(HttpMessage message, ReadOnlyMemory <IHttpPipelinePolicy> pipeline, NextPolicy next)
    {
        // it is important to redact any PII here in real world scenarios

        var requestBody = message.Request.Content is BinaryData content
            ? content.ToString()
            : default;

        _logger.LogInformation("Request body was: {requestBody}", requestBody);

        // more request details can be logged, like headers and stuff

        return(next());
    }
Ejemplo n.º 8
0
    public async Task ProcessAsync(HttpMessage message, ReadOnlyMemory <IHttpPipelinePolicy> pipeline, NextPolicy next)
    {
        await next();

        if (message.Request.EnsureSuccessStatusCode && message.Response.HttpResponseMessage != null)
        {
            message.Response.HttpResponseMessage.EnsureSuccessStatusCode();
        }
    }