Inheritance: System.Net.Http.DelegatingHandler
        internal static void ProcessRequest(HttpRequestMessage request, HttpResponseMessage response, JsonApiConfiguration config, bool requiresMediaType)
        {
            var hasMediaType = request.Headers.Accept.Any(x => x.MediaType == Constants.MediaType);

            var statusCode = (int)response.StatusCode;

            if ((requiresMediaType && !hasMediaType) || (statusCode >= 400 && statusCode < 500))
            {
                // probably malformed request or not found
                return;
            }

            var value = response.Content as ObjectContent;

            if (config == null)
            {
                config = new JsonApiConfiguration();
            }

            var content = PreprocessingDelegatingHandler.PreprocessRequest(value?.Value, request, config);

            if (content.ErrorContent != null)
            {
                response.StatusCode = ApiError.IsClientError(content.ErrorContent)
                    ? HttpStatusCode.BadRequest
                    : HttpStatusCode.InternalServerError;
            }

            request.Properties.Add(Constants.PropertyNames.PreprocessResult, content);
        }
Example #2
0
        /// <inheritdoc/>
        public override async Task WriteToStreamAsync(
            Type type,
            object value,
            Stream writeStream,
            HttpContent content,
            TransportContext transportContext)
        {
            PreprocessResult preprocessed;

            if (_request.Properties.ContainsKey(Constants.PreprocessResultPropertyName))
            {
                preprocessed = _request.Properties[Constants.PreprocessResultPropertyName]
                               as PreprocessResult;
            }
            else
            {
                // backwards compatibility with old way to do Saule setup
                preprocessed = PreprocessingDelegatingHandler.PreprocessRequest(value, _request, _config);
            }

            var json = JsonApiSerializer.Serialize(preprocessed);

            await WriteJsonToStream(json, writeStream);
        }