|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Net.Http.Formatting; |
| 7 | +using System.Net.Http.Headers; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace Pathoschild.Http.Client.Default |
| 11 | +{ |
| 12 | + /// <summary>Builds and dispatches an asynchronous HTTP request.</summary> |
| 13 | + public class Request : IRequest |
| 14 | + { |
| 15 | + /********* |
| 16 | + ** Properties |
| 17 | + *********/ |
| 18 | + /// <summary>Constructs implementations for the fluent client.</summary> |
| 19 | + protected IFactory Factory { get; set; } |
| 20 | + |
| 21 | + /// <summary>Executes an HTTP request.</summary> |
| 22 | + protected Func<IRequest, Task<HttpResponseMessage>> ResponseBuilder { get; set; } |
| 23 | + |
| 24 | + |
| 25 | + /********* |
| 26 | + ** Accessors |
| 27 | + *********/ |
| 28 | + /// <summary>The underlying HTTP request message.</summary> |
| 29 | + public HttpRequestMessage Message { get; set; } |
| 30 | + |
| 31 | + /// <summary>The formatters used for serializing and deserializing message bodies.</summary> |
| 32 | + public MediaTypeFormatterCollection Formatters { get; set; } |
| 33 | + |
| 34 | + /// <summary>Whether to handle errors from the upstream server by throwing an exception.</summary> |
| 35 | + public bool ThrowError { get; set; } |
| 36 | + |
| 37 | + |
| 38 | + /********* |
| 39 | + ** Public methods |
| 40 | + *********/ |
| 41 | + /// <summary>Construct an instance.</summary> |
| 42 | + /// <param name="message">The underlying HTTP request message.</param> |
| 43 | + /// <param name="formatters">The formatters used for serializing and deserializing message bodies.</param> |
| 44 | + /// <param name="dispatcher">Executes an HTTP request.</param> |
| 45 | + /// <param name="factory">Constructs implementations for the fluent client.</param> |
| 46 | + public Request(HttpRequestMessage message, MediaTypeFormatterCollection formatters, Func<IRequest, Task<HttpResponseMessage>> dispatcher, IFactory factory = null) |
| 47 | + { |
| 48 | + this.Message = message; |
| 49 | + this.Formatters = formatters; |
| 50 | + this.ResponseBuilder = dispatcher; |
| 51 | + this.Factory = factory ?? new Factory(); |
| 52 | + this.ThrowError = true; |
| 53 | + } |
| 54 | + |
| 55 | + /*** |
| 56 | + ** Build request |
| 57 | + ***/ |
| 58 | + /// <summary>Set the body content of the HTTP request.</summary> |
| 59 | + /// <param name="body">The value to serialize into the HTTP body content.</param> |
| 60 | + /// <param name="contentType">The request body format (or <c>null</c> to use the first supported Content-Type in the <see cref="Client.IRequest.Formatters"/>).</param> |
| 61 | + /// <returns>Returns the request builder for chaining.</returns> |
| 62 | + /// <exception cref="InvalidOperationException">No MediaTypeFormatters are available on the API client for this content type.</exception> |
| 63 | + public virtual IRequest WithBody<T>(T body, MediaTypeHeaderValue contentType = null) |
| 64 | + { |
| 65 | + MediaTypeFormatter formatter = this.Factory.GetFormatter(this.Formatters, contentType); |
| 66 | + string mediaType = contentType != null ? contentType.MediaType : null; |
| 67 | + return this.WithBody<T>(body, formatter, mediaType); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary>Set the body content of the HTTP request.</summary> |
| 71 | + /// <param name="body">The value to serialize into the HTTP body content.</param> |
| 72 | + /// <param name="formatter">The media type formatter with which to format the request body format.</param> |
| 73 | + /// <param name="mediaType">The HTTP media type (or <c>null</c> for the <paramref name="formatter"/>'s default).</param> |
| 74 | + /// <returns>Returns the request builder for chaining.</returns> |
| 75 | + public virtual IRequest WithBody<T>(T body, MediaTypeFormatter formatter, string mediaType = null) |
| 76 | + { |
| 77 | + return this.WithBodyContent(new ObjectContent<T>(body, formatter, mediaType)); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary>Set the body content of the HTTP request.</summary> |
| 81 | + /// <param name="body">The formatted HTTP body content.</param> |
| 82 | + /// <returns>Returns the request builder for chaining.</returns> |
| 83 | + public virtual IRequest WithBodyContent(HttpContent body) |
| 84 | + { |
| 85 | + this.Message.Content = body; |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary>Set an HTTP header.</summary> |
| 90 | + /// <param name="key">The key of the HTTP header.</param> |
| 91 | + /// <param name="value">The value of the HTTP header.</param> |
| 92 | + /// <returns>Returns the request builder for chaining.</returns> |
| 93 | + public virtual IRequest WithHeader(string key, string value) |
| 94 | + { |
| 95 | + this.Message.Headers.Add(key, value); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary>Set an HTTP query string argument.</summary> |
| 100 | + /// <param name="key">The key of the query argument.</param> |
| 101 | + /// <param name="value">The value of the query argument.</param> |
| 102 | + /// <returns>Returns the request builder for chaining.</returns> |
| 103 | + public virtual IRequest WithArgument(string key, object value) |
| 104 | + { |
| 105 | + var query = this.Message.RequestUri.ParseQueryString(); |
| 106 | + query.Add(key, value.ToString()); |
| 107 | + string uri = this.Message.RequestUri.GetLeftPart(UriPartial.Path) + "?" + query; |
| 108 | + this.Message.RequestUri = new Uri(uri); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary>Customize the underlying HTTP request message.</summary> |
| 113 | + /// <param name="request">The HTTP request message.</param> |
| 114 | + /// <returns>Returns the request builder for chaining.</returns> |
| 115 | + public virtual IRequest WithCustom(Action<HttpRequestMessage> request) |
| 116 | + { |
| 117 | + request(this.Message); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + /*** |
| 122 | + ** Retrieve response |
| 123 | + ***/ |
| 124 | + /// <summary>Asynchronously retrieve the HTTP response.</summary> |
| 125 | + /// <exception cref="ApiException">An error occurred processing the response.</exception> |
| 126 | + public virtual Task<HttpResponseMessage> AsMessage() |
| 127 | + { |
| 128 | + return this.ValidateResponse(this.ResponseBuilder(this)); |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary>Asynchronously retrieve the response body as a deserialized model.</summary> |
| 132 | + /// <typeparam name="T">The response model to deserialize into.</typeparam> |
| 133 | + /// <exception cref="ApiException">An error occurred processing the response.</exception> |
| 134 | + public virtual async Task<T> As<T>() |
| 135 | + { |
| 136 | + HttpResponseMessage message = await this.AsMessage(); |
| 137 | + return await message.Content.ReadAsAsync<T>(this.Formatters); |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary>Asynchronously retrieve the response body as a list of deserialized models.</summary> |
| 141 | + /// <typeparam name="T">The response model to deserialize into.</typeparam> |
| 142 | + /// <exception cref="ApiException">An error occurred processing the response.</exception> |
| 143 | + public virtual Task<List<T>> AsList<T>() |
| 144 | + { |
| 145 | + return this.As<List<T>>(); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary>Asynchronously retrieve the response body as an array of <see cref="byte"/>.</summary> |
| 149 | + /// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns> |
| 150 | + /// <exception cref="ApiException">An error occurred processing the response.</exception> |
| 151 | + public virtual async Task<byte[]> AsByteArray() |
| 152 | + { |
| 153 | + HttpResponseMessage message = await this.AsMessage(); |
| 154 | + return await message.Content.ReadAsByteArrayAsync(); |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary>Asynchronously retrieve the response body as a <see cref="string"/>.</summary> |
| 158 | + /// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns> |
| 159 | + /// <exception cref="ApiException">An error occurred processing the response.</exception> |
| 160 | + public virtual async Task<string> AsString() |
| 161 | + { |
| 162 | + HttpResponseMessage message = await this.AsMessage(); |
| 163 | + return await message.Content.ReadAsStringAsync(); |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary>Asynchronously retrieve the response body as a <see cref="Stream"/>.</summary> |
| 167 | + /// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns> |
| 168 | + /// <exception cref="ApiException">An error occurred processing the response.</exception> |
| 169 | + public virtual async Task<Stream> AsStream() |
| 170 | + { |
| 171 | + HttpResponseMessage message = await this.AsMessage(); |
| 172 | + Stream stream = await message.Content.ReadAsStreamAsync(); |
| 173 | + stream.Position = 0; |
| 174 | + return stream; |
| 175 | + } |
| 176 | + |
| 177 | + /*** |
| 178 | + ** Synchronize |
| 179 | + ***/ |
| 180 | + /// <summary>Block the current thread until the asynchronous request completes.</summary> |
| 181 | + /// <exception cref="ApiException">The HTTP response returned a non-success <see cref="HttpStatusCode"/> and <see cref="ThrowError"/> is <c>true</c>.</exception> |
| 182 | + public void Wait() |
| 183 | + { |
| 184 | + this.AsMessage().Wait(); |
| 185 | + } |
| 186 | + |
| 187 | + /********* |
| 188 | + ** Protected methods |
| 189 | + *********/ |
| 190 | + /// <summary>Validate the HTTP response and raise any errors in the response as exceptions.</summary> |
| 191 | + /// <param name="request">The response message to validate.</param> |
| 192 | + /// <exception cref="ApiException">The HTTP response returned a non-success <see cref="HttpStatusCode"/> and <see cref="ThrowError"/> is <c>true</c>.</exception> |
| 193 | + protected async Task<HttpResponseMessage> ValidateResponse(Task<HttpResponseMessage> request) |
| 194 | + { |
| 195 | + // fetch request |
| 196 | + HttpResponseMessage response = await request; |
| 197 | + this.ValidateResponse(response); |
| 198 | + return response; |
| 199 | + } |
| 200 | + |
| 201 | + /// <summary>Validate the HTTP response and raise any errors in the response as exceptions.</summary> |
| 202 | + /// <param name="message">The response message to validate.</param> |
| 203 | + /// <exception cref="ApiException">The HTTP response returned a non-success <see cref="HttpStatusCode"/> and <see cref="ThrowError"/> is <c>true</c>.</exception> |
| 204 | + protected virtual void ValidateResponse(HttpResponseMessage message) |
| 205 | + { |
| 206 | + if (this.ThrowError && !message.IsSuccessStatusCode) |
| 207 | + throw new ApiException(message, message.StatusCode, String.Format("The API query failed with status code {0}: {1}", message.StatusCode, message.ReasonPhrase)); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments