/// <summary>
        /// Like ExecuteAsStream, but catch non-success HTTP codes and convert them
        /// into an exception.
        /// </summary>
        public async static Task <Stream> ExecuteAsStreamOrThrowAsync <TResponse>(
            this IClientServiceRequest <TResponse> request,
            CancellationToken cancellationToken)
        {
            using (var httpRequest = request.CreateRequest())
            {
                var httpResponse = await request.Service.HttpClient.SendAsync(
                    httpRequest,
                    cancellationToken).ConfigureAwait(false);

                // NB. ExecuteAsStream does not do this check.
                if (!httpResponse.IsSuccessStatusCode)
                {
                    var error = await request.Service.DeserializeError(httpResponse).ConfigureAwait(false);

                    throw new GoogleApiException(request.Service.Name, error.ToString())
                          {
                              Error          = error,
                              HttpStatusCode = httpResponse.StatusCode
                          };
                }

                cancellationToken.ThrowIfCancellationRequested();
                return(await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false));
            }
        }
Exemplo n.º 2
0
        internal static async Task <HttpContent> CreateIndividualRequest(IClientServiceRequest request)
        {
            HttpRequestMessage requestMessage = request.CreateRequest(false);
            string             requestContent = await CreateRequestContentString(requestMessage).ConfigureAwait(false);

            var content = new StringContent(requestContent);

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/http");
            return(content);
        }