Пример #1
0
        private static T DeserializeRestResponse <T>(IRestResponse response, ResponseBodyType format,
                                                     Action <string> log, bool throwOnError)
        {
            try
            {
                var responsePayload = response.Content;

                if (format == ResponseBodyType.XML)
                {
                    responsePayload = JsonConvert.SerializeXNode(XDocument.Parse(response.Content));  // gross.
                }
                return(JsonConvert.DeserializeObject <T>(responsePayload, GenerateSerializationSettings()));
            }
            catch (Exception ex)
            {
                var error = string.Format("Deserialization of rest response failed.  Response Content:[{0}].  " +
                                          "Exception Message: [{1}]", response.Content, ex.Message);

                log?.Invoke(error);

                if (throwOnError)
                {
                    throw new RestRequestFailureException(error, ex);
                }

                return(default(T));
            }
        }
Пример #2
0
        /// <summary>
        /// Send the specified RestSharp request and deserialize the response into the type provided.
        /// </summary>
        /// <returns>A populated object of type T.</returns>
        /// <param name="client">RestSharp client struct</param>
        /// <param name="request">RestSharp request struct</param>
        /// <param name="format">The expected format of the body of the response</param>
        /// <param name="log">An optional delegate, which will be applied to log messages.</param>
        /// <param name="throwOnError">Should we throw if the result of the http call was successful? If
        /// this is false, the method simply logs the error and returns null.  If true, a
        /// RestRequestFailureException is thrown.</param>
        /// <typeparam name="T">The response object into which we will deserialize the content of the
        /// HTTP response.</typeparam>
        /// <exception cref="AggregateException">In the case of a non-200 response (and throwOnError==false),
        /// this method will return an AggregateException which wraps a RestRequestFailureException.</exception>
        public static async Task <T> SendRestRequest <T>(RestClient client, RestRequest request,
                                                         ResponseBodyType format = ResponseBodyType.JSON, Action <string> log = null, bool throwOnError = true)
        {
            LogRequest(log, client, request);

            var response = await client.Execute(request);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                var error = string.Format("HTTP request failed with status code:[{0}].  Message: [{1}]",
                                          response.StatusCode, response.StatusDescription);

                log?.Invoke(error);

                if (throwOnError)
                {
                    throw new RestRequestFailureException(error);
                }
            }

            LogResponse(log, response);

            return(DeserializeRestResponse <T>(response, format, log, throwOnError));
        }
Пример #3
0
        private void ProcessResponseBody([NotNull] Match match)
        {
            var responseBodyType = ParsingHelper.GetResponseBodyType(match);

            if (responseBodyType == ResponseBodyType.Decoded)
            {
                return;
            }

            var internalId = match.GetSucceededGroupValue(ParsingHelper.InternalIdGroupName).ParseLong();
            var size       = match.GetSucceededGroupValue(ParsingHelper.SizeGroupName).ParseLong();

            var harEntry = _internalIdToHarEntryMap.EnsureNotNull().GetValueOrDefault(internalId);

            if (harEntry == null)
            {
                throw new InvalidOperationException(
                          $"Cannot find an entry with Internal ID = {internalId} (line {_lineIndex}).");
            }

            var harResponse = harEntry.Response.EnsureNotNull();
            var harContent  = harResponse.Content.EnsureNotNull();

            harResponse.BodySize = harResponse.BodySize.GetValueOrDefault() + size;

            if (responseBodyType != ResponseBodyType.Encoded)
            {
                harContent.Size = harContent.Size.GetValueOrDefault() + size;
                harContent.SavedByCompression = 0;
            }
            else
            {
                const ResponseBodyType ExpectedBodyType = ResponseBodyType.Decoded;

                FetchLine();
                var nextMatch = _line.MatchAgainst(ParsingHelper.ResponseBodyMarkerRegex);
                if (!nextMatch.Success)
                {
                    //// No decoded body
                    _skipFetchOnce = true;
                    return;
                }

                var nextMatchBodyType = ParsingHelper.GetResponseBodyType(nextMatch);
                if (nextMatchBodyType != ExpectedBodyType)
                {
                    throw new InvalidOperationException(
                              $@"The type of the response body that follows the response body of the type '{responseBodyType
                            }' must be '{ExpectedBodyType}' (line {_lineIndex}).");
                }

                var nextInternalId = nextMatch.GetSucceededGroupValue(ParsingHelper.InternalIdGroupName).ParseLong();
                if (nextInternalId != internalId)
                {
                    throw new InvalidOperationException(
                              $@"The internal ID mismatch occurred at line {_lineIndex}. Expected: {internalId}, actual: {
                            nextInternalId}.");
                }

                var nextSize = nextMatch.GetSucceededGroupValue(ParsingHelper.SizeGroupName).ParseLong();
                harContent.Size = harContent.Size.GetValueOrDefault() + nextSize;
                harContent.SavedByCompression = harContent.SavedByCompression.GetValueOrDefault() + (nextSize - size);
            }

            //// TODO [vmcl] Parse response body (content)
        }