示例#1
0
        public async Task <EntryResponse> ExecuteAsync(EntryRequest interaction)
        {
            if (interaction == null)
            {
                throw Error.ArgumentNull(nameof(interaction));
            }
            bool compressRequestBody = Settings.CompressRequestBody;

            using var requestMessage = interaction.ToHttpRequestMessage(BaseUrl, Settings);
            if (Settings.PreferCompressedResponses)
            {
                requestMessage.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
                requestMessage.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
            }

            byte[] outgoingBody = null;
            if (requestMessage.Method == HttpMethod.Post || requestMessage.Method == HttpMethod.Put)
            {
                outgoingBody = await requestMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
            }

            using var response = await Client.SendAsync(requestMessage).ConfigureAwait(false);

            try
            {
                var body = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

                LastResult = response.ToEntryResponse(body);
                return(LastResult);
            }
            catch (AggregateException ae)
            {
                throw ae.GetBaseException();
            }
        }
        internal static EntryResponse ToEntryResponse(this HttpWebResponse response, byte[] body)
        {
            var result = new EntryResponse
            {
                Status = ((int)response.StatusCode).ToString()
            };

            foreach (var key in response.Headers.AllKeys)
            {
                result.Headers.Add(key, response.Headers[key]);
            }
            result.ResponseUri = response.ResponseUri;
            result.Location    = response.Headers[HttpUtil.LOCATION] ?? response.Headers[HttpUtil.CONTENTLOCATION];

#if NETSTANDARD1_1
            if (!String.IsNullOrEmpty(response.Headers[HttpUtil.LASTMODIFIED]))
            {
                DateTimeOffset dateTimeOffset = new DateTimeOffset();
                bool           success        = DateTimeOffset.TryParse(response.Headers[HttpUtil.LASTMODIFIED], out dateTimeOffset);
                if (!success)
                {
                    throw new FormatException($"Last-Modified header has value '{response.Headers[HttpUtil.LASTMODIFIED]}', which is not recognized as a valid DateTime");
                }
                result.LastModified = dateTimeOffset;
            }
#else
            result.LastModified = response.LastModified;
#endif
            result.Etag        = getETag(response);
            result.ContentType = getContentType(response);
            result.Body        = body;

            return(result);
        }
 internal static void SetHeaders(this EntryResponse interaction, HttpResponseHeaders headers)
 {
     foreach (var header in headers)
     {
         //TODO: check multiple values for a header??
         interaction.Headers.Add(header.Key, header.Value.ToList().FirstOrDefault());
     }
 }
        public static string GetBodyAsText(this EntryResponse interaction)
        {
            var body = interaction.Body;

            if (body != null)
            {
                return(HttpUtil.DecodeBody(body, Encoding.UTF8));
            }
            else
            {
                return(null);
            }
        }
        internal static EntryResponse ToEntryResponse(this HttpResponseMessage response, byte[] body)
        {
            var result = new EntryResponse
            {
                Status       = ((int)response.StatusCode).ToString(),
                ResponseUri  = response.RequestMessage.RequestUri,//this is actually the requestUri, can't find the responseUri
                Body         = body,
                Location     = response.Headers.Location?.AbsoluteUri ?? response.Content.Headers.ContentLocation?.AbsoluteUri,
                LastModified = response.Content.Headers.LastModified,
                Etag         = response.Headers.ETag?.Tag.Trim('\"'),
                ContentType  = response.Content.Headers.ContentType?.MediaType
            };

            result.SetHeaders(response.Headers);


            return(result);
        }
        public static async Task <TypedEntryResponse> ToTypedEntryResponseAsync(this EntryResponse response, IStructureDefinitionSummaryProvider provider)
        {
            var result = new TypedEntryResponse
            {
                ContentType  = response.ContentType,
                Body         = response.Body,
                Etag         = response.Etag,
                Headers      = response.Headers,
                LastModified = response.LastModified,
                Location     = response.Location,
                ResponseUri  = response.ResponseUri,
                Status       = response.Status
            };

            var body = response.GetBodyAsText();

            if (!string.IsNullOrEmpty(body))
            {
                result.TypedElement = await parseResourceAsync(body, response.ContentType, provider, response.IsSuccessful()).ConfigureAwait(false);
            }

            return(result);
        }
 public static bool IsSuccessful(this EntryResponse response)
 {
     int.TryParse(response.Status, out int code);
     return(code >= 200 && code < 300);
 }