Пример #1
0
        private static TResponse SetBody <TResponse>(ApiCallDetails details, RequestData requestData, Stream responseStream, string mimeType)
            where TResponse : class, IElasticsearchResponse, new()
        {
            byte[] bytes = null;
            var    disableDirectStreaming = requestData.PostData?.DisableDirectStreaming ?? requestData.ConnectionSettings.DisableDirectStreaming;

            if (disableDirectStreaming || NeedsToEagerReadStream <TResponse>())
            {
                var inMemoryStream = requestData.MemoryStreamFactory.Create();
                responseStream.CopyTo(inMemoryStream, BufferSize);
                bytes = SwapStreams(ref responseStream, ref inMemoryStream);
                details.ResponseBodyInBytes = bytes;
            }

            using (responseStream)
            {
                if (SetSpecialTypes <TResponse>(bytes, out var r))
                {
                    return(r);
                }

                if (details.HttpStatusCode.HasValue && requestData.SkipDeserializationForStatusCodes.Contains(details.HttpStatusCode.Value))
                {
                    return(null);
                }

                if (requestData.CustomConverter != null)
                {
                    return(requestData.CustomConverter(details, responseStream) as TResponse);
                }
                return(mimeType == null || !mimeType.StartsWith(requestData.RequestMimeType, StringComparison.Ordinal)
                                                ? null
                                                : requestData.ConnectionSettings.RequestResponseSerializer.Deserialize <TResponse>(responseStream));
            }
        }
Пример #2
0
        private static ApiCallDetails Initialize(
            RequestData requestData, Exception exception, int?statusCode, IEnumerable <string> warnings, string mimeType)
        {
            var success            = false;
            var allowedStatusCodes = requestData.AllowedStatusCodes.ToList();

            if (statusCode.HasValue)
            {
                success = statusCode >= 200 && statusCode < 300 ||
                          (requestData.Method == HttpMethod.HEAD && statusCode == 404) ||
                          allowedStatusCodes.Contains(statusCode.Value) ||
                          allowedStatusCodes.Contains(-1);
            }
            var details = new ApiCallDetails
            {
                Success            = success,
                OriginalException  = exception,
                HttpStatusCode     = statusCode,
                RequestBodyInBytes = requestData.PostData?.WrittenBytes,
                Uri                 = requestData.Uri,
                HttpMethod          = requestData.Method,
                DeprecationWarnings = warnings ?? Enumerable.Empty <string>(),
                ResponseMimeType    = mimeType
            };

            return(details);
        }
Пример #3
0
        private static async Task <TResponse> SetBodyAsync <TResponse>(
            ApiCallDetails details, RequestData requestData, Stream responseStream, string mimeType, CancellationToken cancellationToken
            )
            where TResponse : class, IElasticsearchResponse, new()
        {
            byte[] bytes = null;
            var    disableDirectStreaming = requestData.PostData?.DisableDirectStreaming ?? requestData.ConnectionSettings.DisableDirectStreaming;

            if (disableDirectStreaming || NeedsToEagerReadStream <TResponse>())
            {
                var inMemoryStream = requestData.MemoryStreamFactory.Create();
                await responseStream.CopyToAsync(inMemoryStream, BufferSize, cancellationToken).ConfigureAwait(false);

                bytes = SwapStreams(ref responseStream, ref inMemoryStream);
                details.ResponseBodyInBytes = bytes;
            }

            using (responseStream)
            {
                if (SetSpecialTypes <TResponse>(mimeType, bytes, requestData.MemoryStreamFactory, out var r))
                {
                    return(r);
                }

                if (details.HttpStatusCode.HasValue && requestData.SkipDeserializationForStatusCodes.Contains(details.HttpStatusCode.Value))
                {
                    return(null);
                }

                var serializer = requestData.ConnectionSettings.RequestResponseSerializer;
                if (requestData.CustomResponseBuilder != null)
                {
                    return(await requestData.CustomResponseBuilder.DeserializeResponseAsync(serializer, details, responseStream, cancellationToken).ConfigureAwait(false) as TResponse);
                }

                return(mimeType == null || !mimeType.StartsWith(requestData.RequestMimeType, StringComparison.Ordinal)
                                        ? null
                                        : await serializer
                       .DeserializeAsync <TResponse>(responseStream, cancellationToken)
                       .ConfigureAwait(false));
            }
        }
Пример #4
0
        private static ApiCallDetails Initialize(
            RequestData requestData, Exception exception, int?statusCode, IEnumerable <string> warnings, string mimeType
            )
        {
            var success            = false;
            var allowedStatusCodes = requestData.AllowedStatusCodes;

            if (statusCode.HasValue)
            {
                if (allowedStatusCodes.Contains(-1) || allowedStatusCodes.Contains(statusCode.Value))
                {
                    success = true;
                }
                else
                {
                    success = requestData.ConnectionSettings
                              .StatusCodeToResponseSuccess(requestData.Method, statusCode.Value);
                }
            }
            //mimeType can include charset information on .NET full framework
            if (!string.IsNullOrEmpty(mimeType) && !mimeType.StartsWith(requestData.RequestMimeType))
            {
                success = false;
            }

            var details = new ApiCallDetails
            {
                Success            = success,
                OriginalException  = exception,
                HttpStatusCode     = statusCode,
                RequestBodyInBytes = requestData.PostData?.WrittenBytes,
                Uri                     = requestData.Uri,
                HttpMethod              = requestData.Method,
                DeprecationWarnings     = warnings ?? Enumerable.Empty <string>(),
                ResponseMimeType        = mimeType,
                ConnectionConfiguration = requestData.ConnectionSettings
            };

            return(details);
        }