示例#1
0
        private static async Task <IRestResponse <TResult> > ExecuteRequestWithRetryPolicyAsync <TResult>(
            HttpMethod httpMethod, int maxRetryAttempts,
            int retryFactor, HttpStatusCode[] httpStatusCodesWorthRetrying, RestSharp.IRestClient restClient,
            IRestRequest restRequest)
        {
            switch (httpMethod)
            {
            case HttpMethod.GET:
                return(await RetryPolicy.ExecuteWithRetryPolicyAsync(maxRetryAttempts, retryFactor,
                                                                     httpStatusCodesWorthRetrying, () => restClient.ExecuteGetTaskAsync <TResult>(restRequest))
                       .ConfigureAwait(false));

            case HttpMethod.POST:
                return(await RetryPolicy.ExecuteWithRetryPolicyAsync(maxRetryAttempts, retryFactor,
                                                                     httpStatusCodesWorthRetrying, () => restClient.ExecutePostTaskAsync <TResult>(restRequest))
                       .ConfigureAwait(false));

            case HttpMethod.PUT:
            case HttpMethod.PATCH:
            case HttpMethod.DELETE:
                return(await RetryPolicy.ExecuteWithRetryPolicyAsync(maxRetryAttempts, retryFactor,
                                                                     httpStatusCodesWorthRetrying, () => restClient.ExecuteTaskAsync <TResult>(restRequest))
                       .ConfigureAwait(false));

            default:
                throw new ArgumentOutOfRangeException(nameof(httpMethod), httpMethod, null);
            }
        }