Esempio n. 1
0
        public HttpResponse DoAction <T>(AcsRequest <T> request, bool autoRetry, int maxRetryNumber, string regionId,
                                         Credential credential, ISigner signer, FormatType?format, List <Endpoint> endpoints) where T : AcsResponse
        {
            FormatType?requestFormatType = request.AcceptFormat;

            if (null != requestFormatType)
            {
                format = requestFormatType;
            }
            if (null == request.RegionId)
            {
                request.RegionId = regionId;
            }
            ProductDomain domain = Endpoint.FindProductDomain(regionId, request.Product, endpoints, this);

            if (null == domain)
            {
                throw new ClientException("SDK.InvalidRegionId", "Can not find endpoint to access.");
            }
            HttpRequest  httpRequest = request.SignRequest(signer, credential, format, domain);
            int          retryTimes  = 1;
            HttpResponse response    = HttpResponse.GetResponse(httpRequest, timeoutInMilliSeconds);

            while (500 <= response.Status && autoRetry && retryTimes < maxRetryNumber)
            {
                httpRequest = request.SignRequest(signer, credential, format, domain);
                response    = HttpResponse.GetResponse(httpRequest, timeoutInMilliSeconds);
                retryTimes++;
            }
            return(response);
        }
        public virtual HttpResponse DoAction <T>(AcsRequest <T> request, bool autoRetry, int maxRetryNumber, string regionId,
                                                 AlibabaCloudCredentials credentials, Signer signer, FormatType?format, List <Endpoint> endpoints) where T : AcsResponse
        {
            FormatType?requestFormatType = request.AcceptFormat;

            if (null != requestFormatType)
            {
                format = requestFormatType;
            }
            ProductDomain domain = null;

            if (request.ProductDomain != null)
            {
                domain = request.ProductDomain;
            }
            else
            {
                domain = Endpoint.FindProductDomain(regionId, request.Product, endpoints);
            }
            if (null == domain)
            {
                throw new ClientException("SDK.InvalidRegionId", "Can not find endpoint to access.");
            }

            request.Headers["User-Agent"] = UserAgent.Resolve(request.GetSysUserAgentConfig(), this.userAgentConfig);

            bool shouldRetry = true;

            for (int retryTimes = 0; shouldRetry; retryTimes++)
            {
                shouldRetry = autoRetry && retryTimes < maxRetryNumber;
                HttpRequest  httpRequest = request.SignRequest(signer, credentials, format, domain);
                HttpResponse response;

                response = this.GetResponse(httpRequest);

                PrintHttpDebugMsg(request, response);

                if (response.Content == null)
                {
                    if (shouldRetry)
                    {
                        continue;
                    }
                    else
                    {
                        throw new ClientException("SDK.ConnectionReset", "Connection reset.");
                    }
                }

                if (500 <= response.Status && shouldRetry)
                {
                    continue;
                }

                return(response);
            }

            return(null);
        }
Esempio n. 3
0
        public async Task <HttpResponse> DoActionAsync <T>(AcsRequest <T> request, bool autoRetry, int maxRetryNumber, string regionId,
                                                           AlibabaCloudCredentials credentials, Signer signer, FormatType?format, List <Endpoint> endpoints, CancellationToken ct) where T : AcsResponse
        {
            FormatType?requestFormatType = request.AcceptFormat;

            if (null != requestFormatType)
            {
                format = requestFormatType;
            }
            ProductDomain domain = null;

            if (request.ProductDomain != null)
            {
                domain = request.ProductDomain;
            }
            else
            {
                domain = Endpoint.FindProductDomain(regionId, request.Product, endpoints);
            }
            if (null == domain)
            {
                throw new ClientException("SDK.InvalidRegionId", "Can not find endpoint to access.");
            }

            bool shouldRetry = true;

            for (int retryTimes = 0; shouldRetry; retryTimes++)
            {
                shouldRetry = autoRetry && retryTimes < maxRetryNumber;
                HttpRequest  httpRequest = request.SignRequest(signer, credentials, format, domain);
                HttpResponse response;
                response = await HttpResponse.GetResponseAsync(httpRequest, ct).ConfigureAwait(false);

                if (response.Content == null)
                {
                    if (shouldRetry)
                    {
                        continue;
                    }
                    else
                    {
                        throw new ClientException("SDK.ConnectionReset", "Connection reset.");
                    }
                }

                if (500 <= response.Status && shouldRetry)
                {
                    continue;
                }

                return(response);
            }

            return(null);
        }
        public virtual HttpResponse DoAction <T>(AcsRequest <T> request, bool autoRetry, int maxRetryNumber,
                                                 string regionId,
                                                 AlibabaCloudCredentials credentials, Signer signer, FormatType?format, List <Endpoint> endpoints)
            where T : AcsResponse
        {
            var                httpStatusCode    = "";
            var                retryAttemptTimes = 0;
            ClientException    exception;
            RetryPolicyContext retryPolicyContext;

            do
            {
                try
                {
                    var watch = Stopwatch.StartNew();

                    FormatType?requestFormatType = request.AcceptFormat;
                    format = requestFormatType;

                    var domain = request.ProductDomain ??
                                 Endpoint.FindProductDomain(regionId, request.Product, endpoints);

                    if (null == domain)
                    {
                        throw new ClientException("SDK.InvalidRegionId", "Can not find endpoint to access.");
                    }

                    request.Headers["User-Agent"] =
                        UserAgent.Resolve(request.GetSysUserAgentConfig(), userAgentConfig);
                    var httpRequest = request.SignRequest(signer, credentials, format, domain);
                    ResolveTimeout(httpRequest);
                    SetHttpsInsecure(IgnoreCertificate);
                    ResolveProxy(httpRequest, request);
                    var response = GetResponse(httpRequest);

                    httpStatusCode = response.Status.ToString();
                    PrintHttpDebugMsg(request, response);
                    watch.Stop();
                    CommonLog.ExecuteTime = watch.ElapsedMilliseconds;
                    return(response);
                }
                catch (ClientException ex)
                {
                    retryPolicyContext = new RetryPolicyContext(ex, httpStatusCode, retryAttemptTimes, request.Product,
                                                                request.Version,
                                                                request.ActionName, RetryCondition.BlankStatus);

                    CommonLog.LogException(ex, ex.ErrorCode, ex.ErrorMessage);
                    exception = ex;
                }

                Thread.Sleep(retryPolicy.GetDelayTimeBeforeNextRetry(retryPolicyContext));
            } while ((retryPolicy.ShouldRetry(retryPolicyContext) & RetryCondition.NoRetry) != RetryCondition.NoRetry);

            if (exception != null)
            {
                CommonLog.LogException(exception, exception.ErrorCode, exception.ErrorMessage);
                throw new ClientException(exception.ErrorCode, exception.ErrorMessage);
            }

            return(null);
        }
        public virtual HttpResponse DoAction <T>(AcsRequest <T> request, bool autoRetry, int maxRetryNumber,
                                                 string regionId,
                                                 AlibabaCloudCredentials credentials, Signer signer, FormatType?format, List <Endpoint> endpoints)
            where T : AcsResponse
        {
            try
            {
                SerilogHelper.StartTime = DateTime.UtcNow.ToString("o");
                var watch = Stopwatch.StartNew();

                FormatType?requestFormatType = request.AcceptFormat;
                if (null != requestFormatType)
                {
                    format = requestFormatType;
                }

                ProductDomain domain = null;
                if (request.ProductDomain != null)
                {
                    domain = request.ProductDomain;
                }
                else
                {
                    domain = Endpoint.FindProductDomain(regionId, request.Product, endpoints);
                }

                if (null == domain)
                {
                    throw new ClientException("SDK.InvalidRegionId", "Can not find endpoint to access.");
                }

                request.Headers["User-Agent"] =
                    UserAgent.Resolve(request.GetSysUserAgentConfig(), userAgentConfig);

                var shouldRetry = true;
                for (var retryTimes = 0; shouldRetry; retryTimes++)
                {
                    shouldRetry = autoRetry && retryTimes < maxRetryNumber;
                    var httpRequest = request.SignRequest(signer, credentials, format, domain);

                    ResolveTimeout(httpRequest);
                    SetHttpsInsecure(IgnoreCertificate);
                    ResolveProxy(httpRequest, request);

                    var response = GetResponse(httpRequest);

                    PrintHttpDebugMsg(request, response);

                    if (response.Content == null)
                    {
                        if (shouldRetry)
                        {
                            continue;
                        }

                        throw new ClientException("SDK.ConnectionReset", "Connection reset.");
                    }

                    if (500 <= response.Status && shouldRetry)
                    {
                        continue;
                    }

                    watch.Stop();
                    SerilogHelper.ExecuteTime = watch.ElapsedMilliseconds;
                    return(response);
                }
            }
            catch (ClientException ex)
            {
                SerilogHelper.LogException(ex, ex.ErrorCode, ex.ErrorMessage);
                throw new ClientException(ex.ErrorCode, ex.ErrorMessage);
            }

            return(null);
        }
        public virtual async Task <HttpResponse> DoActionAsync <T>(AcsRequest <T> request, bool autoRetry, int maxRetryNumber,
                                                                   string regionId,
                                                                   AlibabaCloudCredentials credentials, Signer signer, FormatType?format, List <Endpoint> endpoints,
                                                                   CancellationToken cancellationToken)
            where T : AcsResponse
        {
            var                httpStatusCode    = "";
            var                retryAttemptTimes = 0;
            ClientException    exception;
            RetryPolicyContext retryPolicyContext;

            do
            {
                try
                {
                    var watch = Stopwatch.StartNew();

                    FormatType?requestFormatType = request.AcceptFormat;

                    var domain = request.ProductDomain ??
                                 Endpoint.FindProductDomain(regionId, request.Product, endpoints);

                    if (null == domain)
                    {
                        throw new ClientException("SDK.InvalidRegionId", "Can not find endpoint to access.");
                    }

                    var userAgent = UserAgent.Resolve(request.GetSysUserAgentConfig(), userAgentConfig);
                    DictionaryUtil.Add(request.Headers, "User-Agent", userAgent);
                    DictionaryUtil.Add(request.Headers, "x-acs-version", request.Version);
                    if (!string.IsNullOrWhiteSpace(request.ActionName))
                    {
                        DictionaryUtil.Add(request.Headers, "x-acs-action", request.ActionName);
                    }
                    var httpRequest = request.SignRequest(signer, credentials, requestFormatType, domain);
                    ResolveTimeout(httpRequest, request.Product, request.Version, request.ActionName);
                    SetHttpsInsecure(IgnoreCertificate);
                    ResolveProxy(httpRequest, request);
                    var response = await GetResponseAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    httpStatusCode = response.Status.ToString();
                    PrintHttpDebugMsg(request, response);
                    watch.Stop();
                    CommonLog.ExecuteTime = watch.ElapsedMilliseconds;
                    return(response);
                }
                catch (ClientException ex)
                {
                    retryPolicyContext = new RetryPolicyContext(ex, httpStatusCode, retryAttemptTimes, request.Product,
                                                                request.Version,
                                                                request.ActionName, RetryCondition.BlankStatus);

                    CommonLog.LogException(ex, ex.ErrorCode, ex.ErrorMessage);
                    exception = ex;
                }

                await Task.Delay(retryPolicy.GetDelayTimeBeforeNextRetry(retryPolicyContext), cancellationToken).ConfigureAwait(false);
            } while ((retryPolicy.ShouldRetry(retryPolicyContext) & RetryCondition.NoRetry) != RetryCondition.NoRetry);

            if (exception != null)
            {
                CommonLog.LogException(exception, exception.ErrorCode, exception.ErrorMessage);
                throw new ClientException(exception.ErrorCode, exception.ErrorMessage);
            }

            return(null);
        }