示例#1
0
        public static async Task <DocumentServiceResponse> ParseResponseAsync(HttpResponseMessage responseMessage, JsonSerializerSettings serializerSettings = null, bool throwOnKnownClientErrorCodes = true)
        {
            using (responseMessage)
            {
                if ((int)responseMessage.StatusCode < 400)
                {
                    MemoryStream bufferedStream = new MemoryStream();

                    await responseMessage.Content.CopyToAsync(bufferedStream);

                    bufferedStream.Position = 0;

                    INameValueCollection headers = ClientExtensions.ExtractResponseHeaders(responseMessage);
                    return(new DocumentServiceResponse(bufferedStream, headers, responseMessage.StatusCode, serializerSettings));
                }
                else if (!throwOnKnownClientErrorCodes && (responseMessage.StatusCode == HttpStatusCode.NotFound || responseMessage.StatusCode == HttpStatusCode.PreconditionFailed || responseMessage.StatusCode == HttpStatusCode.Conflict))
                {
                    INameValueCollection headers = ClientExtensions.ExtractResponseHeaders(responseMessage);
                    return(new DocumentServiceResponse(null, headers, responseMessage.StatusCode, serializerSettings));
                }
                else
                {
                    throw await ClientExtensions.CreateDocumentClientException(responseMessage);
                }
            }
        }
示例#2
0
        public static async Task <DocumentServiceResponse> ParseResponseAsync(HttpResponseMessage responseMessage, JsonSerializerSettings serializerSettings = null, DocumentServiceRequest request = null)
        {
            using (responseMessage)
            {
                if ((int)responseMessage.StatusCode < 400)
                {
                    MemoryStream bufferedStream = new MemoryStream();

                    await responseMessage.Content.CopyToAsync(bufferedStream);

                    bufferedStream.Position = 0;

                    INameValueCollection headers = ClientExtensions.ExtractResponseHeaders(responseMessage);
                    return(new DocumentServiceResponse(bufferedStream, headers, responseMessage.StatusCode, serializerSettings));
                }
                else if (request != null &&
                         request.IsValidStatusCodeForExceptionlessRetry((int)responseMessage.StatusCode))
                {
                    INameValueCollection headers = ClientExtensions.ExtractResponseHeaders(responseMessage);
                    return(new DocumentServiceResponse(null, headers, responseMessage.StatusCode, serializerSettings));
                }
                else
                {
                    throw await ClientExtensions.CreateDocumentClientException(responseMessage);
                }
            }
        }
示例#3
0
 public static async Task <DocumentServiceResponse> ParseMediaResponseAsync(HttpResponseMessage responseMessage, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     if ((int)responseMessage.StatusCode < 400)
     {
         INameValueCollection headers     = ClientExtensions.ExtractResponseHeaders(responseMessage);
         MediaStream          mediaStream = new MediaStream(responseMessage, await responseMessage.Content.ReadAsStreamAsync());
         return(new DocumentServiceResponse(mediaStream, headers, responseMessage.StatusCode));
     }
     else
     {
         throw await ClientExtensions.CreateDocumentClientException(responseMessage);
     }
 }