Пример #1
0
        public WebhookService(ILogger <RechargeSharpService> logger, IHttpClientFactory httpClientFactory, IOptions <RechargeServiceOptions> rechargeServiceOptions)
        {
            _logger                 = logger;
            _httpClientFactory      = httpClientFactory;
            _rechargeServiceOptions = rechargeServiceOptions.Value;

            _client = _httpClientFactory.CreateClient("RechargeSharpWebhookClient");
            _client.DefaultRequestHeaders.Remove("X-Recharge-Access-Token");
            _client.DefaultRequestHeaders.Add("X-Recharge-Access-Token", _rechargeServiceOptions.GetWebhookApiKey());

            AsyncRetryPolicy = Policy.HandleResult <HttpResponseMessage>(x =>
            {
                if (!x.IsSuccessStatusCode)
                {
                    _logger.LogError(x.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                    if ((int)x.StatusCode > 399 && x.StatusCode != HttpStatusCode.TooManyRequests && (x.StatusCode != HttpStatusCode.NotFound && x.RequestMessage.Method != HttpMethod.Get))
                    {
                        throw new Exception(x.Content.ReadAsStringAsync().GetAwaiter().GetResult());
                    }

                    if (x.StatusCode == HttpStatusCode.NotFound && x.RequestMessage.Method == HttpMethod.Get)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }).WaitAndRetryForeverAsync(
                retryAttempt =>
                TimeSpan.FromSeconds(3));
        }
Пример #2
0
        public RechargeSharpOptionsTest()
        {
            var guidStringList = new List <string>();

            for (int i = 0; i < 10; i++)
            {
                guidStringList.Add(Guid.NewGuid().ToString());
            }

            guidStringList.Add("");

            _sut = new RechargeServiceOptions()
            {
                ApiKeyArray   = guidStringList,
                WebhookApiKey = guidStringList.First()
            };
        }