예제 #1
0
        public async Task <HttpContent> CreateContent(object value, ITypedBuilderContext context)
        {
            var content = value as HttpContent;

            if (content != null)
            {
                return(content);
            }

            var type      = context.ContentType;
            var mediaType = context.MediaType;
            var header    = new MediaTypeHeaderValue(mediaType);
            var formatter = MediaTypeFormatters.FindWriter(type, header);

            if (formatter == null)
            {
                throw new UnsupportedMediaTypeException(string.Format(SR.NoWriteFormatterForMimeTypeErrorFormat, type.FormattedTypeName(), mediaType), header);
            }

            using (var stream = new MemoryStream())
            {
                await formatter.WriteToStreamAsync(type, value, stream, null, null);

                content = new ByteArrayContent(stream.ToArray());
            }

            formatter.SetDefaultContentHeaders(type, content.Headers, header);

            return(content);
        }
예제 #2
0
        public TypedExceptionContext(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response, Exception exception)
            : base(context, request)
        {
            Exception = exception;
            Response  = response;

            _exceptionHandled = new Modifiable <bool>(false);
        }
예제 #3
0
        public Task <object> DeserializeError(HttpResponseMessage response, ITypedBuilderContext context)
        {
            var error = _errors.GetResponse(new MockTypedRequestContext(context, new MockHttpRequestMessage(response.RequestMessage)));

            if (error == null)
            {
                return(_innerFormatter.DeserializeError(response, context));
            }

            return(Task.FromResult(error.Result));
        }
예제 #4
0
        public async Task <Modifiable> OnError(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response, object error)
        {
            TypedErrorContext handlerContext = null;

            foreach (var handlerInfo in GetHandlerInfo(HandlerType.Error))
            {
                if (handlerContext == null)
                {
                    handlerContext = ((Func <ITypedBuilderContext, HttpRequestMessage, HttpResponseMessage, object, TypedErrorContext>)handlerInfo.InitialConstructor)(context, request, response, error);
                }
                else
                {
                    handlerContext = ((Func <TypedErrorContext, TypedErrorContext>)handlerInfo.ContinuationConstructor)(handlerContext);
                }

                await handlerInfo.Handler(handlerContext);
            }

            return(handlerContext?.GetHandlerResult());
        }
예제 #5
0
        public static async Task <object> CreateResult(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response)
        {
            object result;

            if (typeof(IEmptyResult).IsAssignableFrom(context.ResultType))
            {
                result = TypeHelpers.GetDefaultValueForType(context.ResultType);
            }
            else
            {
                result = await context.Formatter.DeserializeResult(response, context);
            }

            var resultWithMeta = result as IResultWithWritableMetadata;

            if (resultWithMeta != null)
            {
                resultWithMeta.Metadata = CachingHelpers.CreateResponseMetadata(result, request, response, context.CacheMetadata);
            }

            return(result);
        }
예제 #6
0
        public static async Task <object> CreateError(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response, Exception exception)
        {
            var allowNullError = typeof(IEmptyError).IsAssignableFrom(context.ErrorType);

            object error = null;

            if (!allowNullError && response != null)
            {
                try
                {
                    error = await context.Formatter.DeserializeError(response, context);
                }
                catch (Exception ex)
                {
                    exception = exception != null ? new AggregateException(ex, exception) : ex;
                }
            }

            if (error == null)
            {
                error = exception != null
                    ?  context.DefaultErrorFactory?.Invoke(context.ErrorType, exception)
                    : TypeHelpers.GetDefaultValueForType(context.ErrorType);
            }

            if (error == null && !allowNullError && exception != null)
            {
                throw exception;
            }

            var errorWithMeta = error as IResultWithWritableMetadata;

            if (errorWithMeta != null)
            {
                errorWithMeta.Metadata = CachingHelpers.CreateResponseMetadata(error, request, response, context.CacheMetadata);
            }

            return(error);
        }
 public TypedBuilderContext(ITypedBuilderContext context)
 {
     Items      = context.Items;
     ResultType = context.ResultType;
     Builder    = context.Builder;
     SuppressCancellationErrors = context.SuppressCancellationErrors;
     Formatter                      = context.Formatter;
     ContentFactory                 = context.ContentFactory;
     HttpContentFactory             = context.HttpContentFactory;
     ResultFactory                  = context.ResultFactory;
     ErrorFactory                   = context.ErrorFactory;
     ContentType                    = context.ContentType;
     ErrorType                      = context.ErrorType;
     MediaType                      = context.MediaType;
     DefaultResultFactory           = context.DefaultResultFactory;
     DefaultErrorFactory            = context.DefaultErrorFactory;
     HandlerRegister                = context.HandlerRegister;
     SuppressTypeMismatchExceptions = context.SuppressTypeMismatchExceptions;
     ExceptionFactory               = context.ExceptionFactory;
     DeserializeResult              = context.DeserializeResult;
     Token             = context.Token;
     CacheMetadata     = context.CacheMetadata;
     ResponseValidator = context.ResponseValidator;
 }
예제 #8
0
 public static Task <HttpContent> CreateHttpContent(ITypedBuilderContext context, object content)
 {
     // TODO: handle empty request?
     return(context.Formatter.CreateContent(content, context));
 }
예제 #9
0
 protected TypedHandlerContext(ITypedBuilderContext context, HttpRequestMessage request)
     : base(context)
 {
     Request = request;
 }
예제 #10
0
 public ExceptionCreationContext(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response,
                                 object error, Exception innerException)
     : base(context, request, response, error)
 {
     InnerException = innerException;
 }
예제 #11
0
        private async Task <object> ResultAsync(ITypedBuilderContext context, CancellationToken token)
        {
            HttpRequestMessage  request  = null;
            HttpResponseMessage response = null;
            object result = null;

            try
            {
                try
                {
                    //build content before creating request
                    //var hasContent = false;
                    object content = null;

                    if (context.ContentFactory != null)
                    {
                        content = context.ContentFactory();

                        if (content != null)
                        {
                            _innerBuilder.WithContent(async ctx =>
                            {
                                var httpContent = await context.HttpContentFactory(context, content);

                                return(httpContent);
                            });
                        }
                    }

                    token.ThrowIfCancellationRequested();

                    request = await _innerBuilder.CreateRequest();

                    token.ThrowIfCancellationRequested();

                    var sendingResult = await context.HandlerRegister.OnSending(context, request, content);

                    if (sendingResult.IsDirty)
                    {
                        return(sendingResult.Value);
                    }

                    token.ThrowIfCancellationRequested();

                    response = await GetResponse(context, request, token);

                    if (!context.IsSuccessfulResponse(response))
                    {
                        throw ObjectHelpers.CreateHttpException(response, request);
                    }

                    var sentResult = await context.HandlerRegister.OnSent(context, request, response);

                    if (sentResult.IsDirty)
                    {
                        result = sentResult.Value;
                    }
                    else
                    {
                        if (context.DeserializeResult)
                        {
                            token.ThrowIfCancellationRequested();

                            result = await context.ResultFactory(context, request, response);
                        }
                    }

                    token.ThrowIfCancellationRequested();

                    var resultResult = await context.HandlerRegister.OnResult(context, request, response, result);

                    result = resultResult.Value;

                    if (result != null)
                    {
                        TypeHelpers.ValidateType(result, context.ResultType, context.SuppressTypeMismatchExceptions, () => response.GetExceptionMessage(request));

                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    var error = await context.ErrorFactory(context, request, response, ex) ?? TypeHelpers.GetDefaultValueForType(context.ErrorType);

                    TypeHelpers.ValidateType(error, context.ErrorType, context.SuppressTypeMismatchExceptions, () => response.GetExceptionMessage(request));

                    var errorResult = await context.HandlerRegister.OnError(context, request, response, error);

                    var errorHandled = (bool)errorResult.Value;

                    if (!errorHandled)
                    {
                        var newEx = context.ExceptionFactory?.Invoke(new ExceptionCreationContext(context, request, response, error, ex));

                        if (newEx != null)
                        {
                            throw newEx;
                        }

                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                var exceptionResult = await context.HandlerRegister.OnException(context, request, response, ex);

                if (!(bool)exceptionResult.Value)
                {
                    if (!context.SuppressCancellationErrors || !(ex is OperationCanceledException))
                    {
                        throw;
                    }
                }
            }
            finally
            {
                if (result != response) // if the result is of type http response message and equal to the response don't dispose.
                {
                    ObjectHelpers.Dispose(request);
                    ObjectHelpers.Dispose(response);
                }
            }

            var defaultResult = context.DefaultResultFactory?.Invoke(context.ResultType);

            TypeHelpers.ValidateType(defaultResult, context.ResultType, context.SuppressTypeMismatchExceptions,
                                     () => response.GetExceptionMessage(request));

            return(defaultResult);
        }
예제 #12
0
 public TypedSentContext(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response)
     : base(context, request, response)
 {
 }
 public TypedResultContext(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response, TResult result)
     : base(context, request, response, result)
 {
 }
예제 #14
0
 public Task <object> DeserializeError(HttpResponseMessage response, ITypedBuilderContext context)
 {
     return(ObjectHelpers.Deserialize(response, context.ErrorType, MediaTypeFormatters, context.Token));
 }
예제 #15
0
 public TypedErrorContext(ITypedBuilderContext context, HttpRequestMessage request, HttpResponseMessage response, TError error)
     : base(context, request, response, error)
 {
 }
예제 #16
0
 public MockTypedRequestContext(ITypedBuilderContext context, IMockRequestContext request)
 {
     BuilderContext = context;
     Request        = request;
 }
예제 #17
0
 protected virtual Task <HttpResponseMessage> GetResponse(ITypedBuilderContext context, HttpRequestMessage request, CancellationToken token)
 {
     return(_innerBuilder.ResultFromRequestAsync(request, token));
 }
예제 #18
0
 public Task <HttpContent> CreateContent(object value, ITypedBuilderContext context)
 {
     return(_innerFormatter.CreateContent(value, context));
 }
 public TypedSendingContext(ITypedBuilderContext context, HttpRequestMessage request, TContent content)
     : base(context, request, content)
 {
 }