コード例 #1
0
        public HttpRequestMessage SigningNative()
        {
            _contentStream.Position = 0;
            CleanupHeaders(_httpRequest);

            var meta = new SigningMetadata(RegionEndpoint.USEast1, new AwsCredentials("accessKey", "secretKey"), DateTime.UtcNow,
                                           _httpClient.DefaultRequestHeaders, null);

            AwsRequestSigner.Sign(_httpRequest, _contentStream, meta);

            return(_httpRequest);
        }
コード例 #2
0
        public async ValueTask <HttpResponseMessage> SendAsync(DynamoDbContextConfig config, HttpContent httpContent, CancellationToken cancellationToken = default)
        {
            try
            {
                int internalServerErrorRetries           = 0,
                    limitExceededRetries                 = 0,
                    provisionedThroughputExceededRetries = 0,
                    requestLimitExceededRetries          = 0,
                    serviceUnavailableRetries            = 0,
                    throttlingRetries = 0;
                while (true)
                {
                    TimeSpan delay;
                    try
                    {
                        using var request = new HttpRequestMessage(HttpMethod.Post, config.RegionEndpoint.RequestUri)
                              {
                                  Content = httpContent
                              };

                        try
                        {
                            var httpClient = _httpClientFactory.CreateHttpClient();
                            var stream     = await httpContent.ReadAsStreamAsync().ConfigureAwait(false);

                            var credentials = await config.CredentialsProvider.GetCredentialsAsync(cancellationToken).ConfigureAwait(false);

                            var metadata = new SigningMetadata(config.RegionEndpoint, credentials, DateTime.UtcNow, httpClient.DefaultRequestHeaders,
                                                               httpClient.BaseAddress);
                            AwsRequestSigner.Sign(request, (RecyclableMemoryStream)stream, in metadata);

                            var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

                            if (!response.IsSuccessStatusCode)
                            {
                                await ErrorHandler.ProcessErrorAsync(config.Metadata, response, cancellationToken).ConfigureAwait(false);
                            }

                            return(response);
                        }
                        finally
                        {
                            request.Content = null;
                        }
                    }
                    catch (InternalServerErrorException)
                    {
                        if (!config.RetryStrategies.InternalServerErrorStrategy.TryGetRetryDelay(internalServerErrorRetries++, out delay))
                        {
                            throw;
                        }
                    }
                    catch (LimitExceededException)
                    {
                        if (!config.RetryStrategies.LimitExceededStrategy.TryGetRetryDelay(limitExceededRetries++, out delay))
                        {
                            throw;
                        }
                    }
                    catch (ProvisionedThroughputExceededException)
                    {
                        if (!config.RetryStrategies.ProvisionedThroughputExceededStrategy.TryGetRetryDelay(provisionedThroughputExceededRetries++, out delay))
                        {
                            throw;
                        }
                    }
                    catch (RequestLimitExceededException)
                    {
                        if (!config.RetryStrategies.RequestLimitExceededStrategy.TryGetRetryDelay(requestLimitExceededRetries++, out delay))
                        {
                            throw;
                        }
                    }
                    catch (ServiceUnavailableException)
                    {
                        if (!config.RetryStrategies.ServiceUnavailableStrategy.TryGetRetryDelay(serviceUnavailableRetries++, out delay))
                        {
                            throw;
                        }
                    }
                    catch (ThrottlingException)
                    {
                        if (!config.RetryStrategies.ThrottlingStrategy.TryGetRetryDelay(throttlingRetries++, out delay))
                        {
                            throw;
                        }
                    }

                    await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
                }
            }
            finally
            {
                httpContent.Dispose();
            }
        }