Ejemplo n.º 1
0
        /// <summary>
        /// Execute the request (which is unguarded)
        /// </summary>
        /// <param name="request">The request to execute</param>
        /// <param name="ct">The cancellation token to use</param>
        /// <returns>The <see cref="IHttpResponseMessage"/> for the request</returns>
        protected async Task <IHttpResponseMessage> ExecuteRequest(IRestRequest request, CancellationToken ct)
        {
            AddDefaultParameters(request);
            while (true)
            {
                if (Authenticator != null && Authenticator.CanPreAuthenticate(this, request, Credentials))
                {
                    await Authenticator.PreAuthenticate(this, request, Credentials);
                }

                // Lazy initialization of the HTTP client
                if (_httpClient == null)
                {
                    _httpClient = HttpClientFactory.CreateClient(this, request);
                }

                bool failed  = true;
                var  message = HttpClientFactory.CreateRequestMessage(this, request);
                try
                {
                    var bodyData = GetContent(request);
                    if (bodyData != null)
                    {
                        message.Content = bodyData;
                    }

                    if (message.Content != null)
                    {
                        var content = message.Content;
                        foreach (var param in request.Parameters.Where(x => x.Type == ParameterType.HttpHeader && x.IsContentParameter()))
                        {
                            if (content.Headers.Contains(param.Name))
                            {
                                content.Headers.Remove(param.Name);
                            }
                            if (param.ValidateOnAdd)
                            {
                                content.Headers.Add(param.Name, param.ToRequestString());
                            }
                            else
                            {
                                content.Headers.TryAddWithoutValidation(param.Name, param.ToRequestString());
                            }
                        }
                    }

                    ModifyRequestBeforeAuthentication(_httpClient, message);

                    if (Authenticator != null && Authenticator.CanPreAuthenticate(_httpClient, message, Credentials))
                    {
                        await Authenticator.PreAuthenticate(_httpClient, message, Credentials);
                    }

                    var response = await _httpClient.SendAsync(message, ct);

                    try
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            if (Authenticator != null && Authenticator.CanHandleChallenge(_httpClient, message, Credentials, response))
                            {
                                await Authenticator.HandleChallenge(_httpClient, message, Credentials, response);

                                continue;
                            }

                            if (!IgnoreResponseStatusCode)
                            {
                                response.EnsureSuccessStatusCode();
                            }
                        }

                        failed = false;
                    }
                    finally
                    {
                        if (failed)
                        {
                            if (response != null)
                            {
                                response.Dispose();
                            }
                            else
                            {
                                message.Dispose();
                            }
                            message = null;
                        }
                    }

                    return(response);
                }
                finally
                {
                    if (failed && message != null)
                    {
                        message.Dispose();
                    }
                }
            }
        }
        /// <summary>
        /// Execute the request (which is unguarded)
        /// </summary>
        /// <param name="request">The request to execute</param>
        /// <param name="ct">The cancellation token to use</param>
        /// <returns>The <see cref="IHttpResponseMessage"/> for the request</returns>
        protected async Task <IHttpResponseMessage> ExecuteRequest(IRestRequest request, CancellationToken ct)
        {
            while (true)
            {
                if (Authenticator != null && Authenticator.CanPreAuthenticate(this, request, Credentials))
                {
                    await Authenticator.PreAuthenticate(this, request, Credentials);
                }

                var  requestParameters = this.MergeParameters(request);
                bool failed            = true;
                var  httpClient        = _httpClient.Value;
                var  message           = HttpClientFactory.CreateRequestMessage(this, request, requestParameters.OtherParameters);
                var  bodyData          = GetContent(request, requestParameters);
                if (bodyData != null)
                {
                    message.Content = bodyData;
                }

                ModifyRequestBeforeAuthentication(httpClient, message);

                if (Authenticator != null && Authenticator.CanPreAuthenticate(httpClient, message, Credentials))
                {
                    await Authenticator.PreAuthenticate(httpClient, message, Credentials);
                }

                var response = await httpClient.SendAsync(message, ct);

                try
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        if (Authenticator != null && Authenticator.CanHandleChallenge(httpClient, message, Credentials, response))
                        {
                            await Authenticator.HandleChallenge(httpClient, message, Credentials, response);

                            continue;
                        }

                        if (!IgnoreResponseStatusCode)
                        {
                            response.EnsureSuccessStatusCode();
                        }
                    }

                    failed = false;
                }
                finally
                {
                    if (failed)
                    {
                        if (response != null)
                        {
                            response.Dispose();
                        }
                        else
                        {
                            message.Dispose();
                        }
                    }
                }

                return(response);
            }
        }