|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Net.Http.Formatting; |
| 6 | +using System.Net.Http.Headers; |
| 7 | + |
| 8 | +namespace Pathoschild.Http.Client.Delegating |
| 9 | +{ |
| 10 | + /// <summary>Builds an asynchronous HTTP request. This implementation delegates work to an inner request builder.</summary> |
| 11 | + public abstract class DelegatingRequestBuilder : IRequestBuilder |
| 12 | + { |
| 13 | + /********* |
| 14 | + ** Properties |
| 15 | + *********/ |
| 16 | + /// <summary>The wrapped request builder implementation.</summary> |
| 17 | + protected IRequestBuilder Implementation { get; set; } |
| 18 | + |
| 19 | + |
| 20 | + /********* |
| 21 | + ** Accessors |
| 22 | + *********/ |
| 23 | + /// <summary>The underlying HTTP request message.</summary> |
| 24 | + public virtual HttpRequestMessage Message |
| 25 | + { |
| 26 | + get { return this.Implementation.Message; } |
| 27 | + set { this.Implementation.Message = value; } |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary>The formatters used for serializing and deserializing message bodies.</summary> |
| 31 | + public virtual MediaTypeFormatterCollection Formatters |
| 32 | + { |
| 33 | + get { return this.Implementation.Formatters; } |
| 34 | + set { this.Implementation.Formatters = value; } |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + /********* |
| 39 | + ** Public methods |
| 40 | + *********/ |
| 41 | + /// <summary>Set the body content of the HTTP request.</summary> |
| 42 | + /// <param name="body">The value to serialize into the HTTP body content.</param> |
| 43 | + /// <param name="contentType">The request body format (or <c>null</c> to use the first supported Content-Type in the <see cref="IRequestBuilder.Formatters"/>).</param> |
| 44 | + /// <returns>Returns the request builder for chaining.</returns> |
| 45 | + /// <exception cref="InvalidOperationException">No MediaTypeFormatters are available on the API client for this content type.</exception> |
| 46 | + public virtual IRequestBuilder WithBody<T>(T body, MediaTypeHeaderValue contentType = null) |
| 47 | + { |
| 48 | + this.Implementation.WithBody<T>(body, contentType); |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary>Set the body content of the HTTP request.</summary> |
| 53 | + /// <param name="body">The value to serialize into the HTTP body content.</param> |
| 54 | + /// <param name="formatter">The media type formatter with which to format the request body format.</param> |
| 55 | + /// <param name="mediaType">The HTTP media type (or <c>null</c> for the <paramref name="formatter"/>'s default).</param> |
| 56 | + /// <returns>Returns the request builder for chaining.</returns> |
| 57 | + public virtual IRequestBuilder WithBody<T>(T body, MediaTypeFormatter formatter, string mediaType = null) |
| 58 | + { |
| 59 | + this.Implementation.WithBody<T>(body, formatter, mediaType); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary>Set the body content of the HTTP request.</summary> |
| 64 | + /// <param name="body">The formatted HTTP body content.</param> |
| 65 | + /// <returns>Returns the request builder for chaining.</returns> |
| 66 | + public virtual IRequestBuilder WithBodyContent(HttpContent body) |
| 67 | + { |
| 68 | + this.Implementation.WithBodyContent(body); |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary>Set an HTTP header.</summary> |
| 73 | + /// <param name="key">The key of the HTTP header.</param> |
| 74 | + /// <param name="value">The value of the HTTP header.</param> |
| 75 | + /// <returns>Returns the request builder for chaining.</returns> |
| 76 | + public virtual IRequestBuilder WithHeader(string key, string value) |
| 77 | + { |
| 78 | + this.Implementation.WithHeader(key, value); |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary>Set an HTTP query string argument.</summary> |
| 83 | + /// <param name="key">The key of the query argument.</param> |
| 84 | + /// <param name="value">The value of the query argument.</param> |
| 85 | + /// <returns>Returns the request builder for chaining.</returns> |
| 86 | + public virtual IRequestBuilder WithArgument(string key, object value) |
| 87 | + { |
| 88 | + this.Implementation.WithArgument(key, value); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary>Customize the underlying HTTP request message.</summary> |
| 93 | + /// <param name="request">The HTTP request message.</param> |
| 94 | + /// <returns>Returns the request builder for chaining.</returns> |
| 95 | + public virtual IRequestBuilder WithCustom(Action<HttpRequestMessage> request) |
| 96 | + { |
| 97 | + this.Implementation.WithCustom(request); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary>Asynchronously dispatch the request.</summary> |
| 102 | + /// <param name="throwError">Whether to handle errors from the upstream server by throwing an exception.</param> |
| 103 | + /// <returns>Returns a response.</returns> |
| 104 | + public virtual IResponse Retrieve(bool throwError = true) |
| 105 | + { |
| 106 | + return this.Implementation.Retrieve(throwError); |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary>Dispatch the request and retrieve the response as a deserialized model.</summary> |
| 110 | + /// <typeparam name="TResponse">The response body type.</typeparam> |
| 111 | + /// <param name="throwError">Whether to handle errors from the upstream server by throwing an exception.</param> |
| 112 | + /// <returns>Returns a deserialized model.</returns> |
| 113 | + /// <exception cref="ApiException">The HTTP response returned a non-success <see cref="HttpStatusCode"/>, and <paramref name="throwError"/> is <c>true</c>.</exception> |
| 114 | + public virtual TResponse RetrieveAs<TResponse>(bool throwError = true) |
| 115 | + { |
| 116 | + return this.Implementation.RetrieveAs<TResponse>(throwError); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary>Dispatch the request and retrieve the response as a deserialized list of models.</summary> |
| 120 | + /// <typeparam name="TResponse">The response body type.</typeparam> |
| 121 | + /// <param name="throwError">Whether to handle errors from the upstream server by throwing an exception.</param> |
| 122 | + /// <returns>Returns a deserialized list of models.</returns> |
| 123 | + /// <exception cref="ApiException">The HTTP response returned a non-success <see cref="HttpStatusCode"/>, and <paramref name="throwError"/> is <c>true</c>.</exception> |
| 124 | + public virtual List<TResponse> RetrieveAsList<TResponse>(bool throwError = true) |
| 125 | + { |
| 126 | + return this.Implementation.RetrieveAsList<TResponse>(throwError); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + /********* |
| 131 | + ** Protected methods |
| 132 | + *********/ |
| 133 | + /// <summary>Construct an instance.</summary> |
| 134 | + /// <param name="requestBuilder">The wrapped request builder implementation.</param> |
| 135 | + protected DelegatingRequestBuilder(IRequestBuilder requestBuilder) |
| 136 | + { |
| 137 | + this.Implementation = requestBuilder; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments