Beispiel #1
0
 public static void NoBodyParserSelected(
     this ILogger logger,
     BodyParserContext formatterContext)
 {
     if (logger.IsEnabled(LogLevel.Debug))
     {
         var contentType = formatterContext.HttpContext.Request.ContentType;
         _noBodyParserSelected(logger, contentType, null);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Supporting method that will attempt to populate the given operation from the body of
        /// a HTTP request by searching for an applicable <see cref="IBodyParser" /> that has been
        /// specified in the <see cref="BlueprintHttpOptions.BodyParsers" /> property.
        /// </summary>
        /// <param name="httpContext">The current HTTP context.</param>
        /// <param name="context">The operation context.</param>
        /// <param name="log">A log to write to.</param>
        /// <param name="options">The registered HTTP options.</param>
        /// <param name="instance">The instance to populate.</param>
        /// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
        /// <typeparam name="T">The type of operation being populated.</typeparam>
        // ReSharper disable once MemberCanBePrivate.Global Used in generated code
        public static async Task <T> PopulateFromMessageBody <T>(
            HttpContext httpContext,
            ApiOperationContext context,
            ILogger <HttpBodyMessagePopulationSource> log,
            IOptions <BlueprintHttpOptions> options,
            T instance)
        {
            var type    = typeof(T);
            var request = httpContext.Request;

            if (request.Body == null || request.ContentType == null)
            {
                return(instance);
            }

            log?.AttemptingToParseBody(type);

            var formatterContext = new BodyParserContext(
                context,
                httpContext,
                instance,
                type);

            var parsers   = options.Value.BodyParsers;
            var formatter = (IBodyParser)null;

            for (var i = 0; i < parsers.Count; i++)
            {
                if (parsers[i].CanRead(formatterContext))
                {
                    formatter = parsers[i];
                    log.BodyParserSelected(formatter, formatterContext);
                    break;
                }

                log.BodyParserRejected(parsers[i], formatterContext);
            }

            if (formatter == null)
            {
                // TODO: How to handle no body parsers matching?
                log.NoBodyParserSelected(formatterContext);

                return(instance);
            }

            // TODO: Handle exceptions
            try
            {
                return((T)await formatter.ReadAsync(formatterContext));
            }
            catch (Exception e)
            {
                log.BodyParsingException(type, e);

                throw;
            }
            finally
            {
                log.DoneParsingBody(type, formatter);
            }
        }