Exemplo n.º 1
0
        private static async Task <HttpResponseMessage> Send(HttpClient client, WebHookReporterOptions options, string payload, ILogger logger, CancellationToken cancellation)
        {
            var request = new HttpRequestMessage
            {
                Method     = HttpMethod.Post,
                RequestUri = new Uri(client.BaseAddress + options.Url),
                Content    = new StringContent(payload)
            };

            if (options.Authorization != null)
            {
                request.Headers.Authorization = new AuthenticationHeaderValue(options.Authorization.Scheme, options.Authorization.Parameter);
            }

            foreach (var header in options.Headers)
            {
                request.Headers.Add(header.Name, header.Value);
            }

            var response = await client.SendAsync(request, cancellation);

            logger.LogDebug($"Report send  to URL: {client.BaseAddress + options.Url} with received status: {response.StatusCode}");

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException(response.ReasonPhrase)
                      {
                          Data = { [nameof(HttpStatusCode)] = response.StatusCode }
                      }
            }
            ;

            return(response);
        }
    }
Exemplo n.º 2
0
 public WebHookReporter(IOptionsMonitor <WebHookReporterOptions> options, IHttpClientFactory factory, IRetryProvider retryProvider, ILogger <WebHookReporter> logger)
 {
     _options            = options.CurrentValue;
     _client             = factory.CreateClient();
     _client.BaseAddress = SLACK_WEBHOOK_URI;
     _retryProvider      = retryProvider;
     _logger             = logger;
 }