| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="HttpClient"/> |
| | 5 | | /// </summary> |
| | 6 | | public static class HttpClientExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Sends a <see cref="HttpMethod.Delete"/> |
| | 10 | | /// <see cref="HttpRequestMessage"/>. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="client">The client.</param> |
| | 13 | | /// <param name="uri">The URI.</param> |
| | 14 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 15 | | public static async Task<HttpResponseMessage?> DeleteAsync(this HttpClient? client, Uri? uri, |
| | 16 | | Action<HttpRequestMessage>? requestMessageAction) => |
| 0 | 17 | | await client.SendAsync(uri, HttpMethod.Delete, requestMessageAction); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Downloads resource at URI to the specified path. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="client">The request.</param> |
| | 23 | | /// <param name="uri">The URI.</param> |
| | 24 | | /// <param name="path">The path.</param> |
| | 25 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 26 | | public static async Task DownloadToFileAsync(this HttpClient? client, Uri? uri, string path, |
| | 27 | | Action<HttpRequestMessage>? requestMessageAction) |
| 1 | 28 | | { |
| 1 | 29 | | if (client == null) return; |
| | 30 | |
|
| 1 | 31 | | var buffer = new byte[32768]; |
| | 32 | | int bytesRead; |
| | 33 | |
|
| 1 | 34 | | using var request = new HttpRequestMessage(HttpMethod.Get, uri); |
| | 35 | |
|
| 1 | 36 | | requestMessageAction?.Invoke(request); |
| | 37 | |
|
| 1 | 38 | | using var response = await client |
| 1 | 39 | | .SendAsync(request) |
| 1 | 40 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 41 | |
|
| 1 | 42 | | await using var stream = await response.Content |
| 1 | 43 | | .ReadAsStreamAsync() |
| 1 | 44 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 45 | |
|
| 1 | 46 | | await using var fs = File.Create(path); |
| | 47 | |
|
| 6 | 48 | | while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) |
| 5 | 49 | | { |
| 5 | 50 | | fs.Write(buffer, 0, bytesRead); |
| 5 | 51 | | } |
| 1 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Downloads resource at URI to the specified path. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="client">The request.</param> |
| | 58 | | /// <param name="uri">The URI.</param> |
| | 59 | | /// <param name="path">The path.</param> |
| | 60 | | public static async Task DownloadToFileAsync(this HttpClient? client, Uri? uri, string path) => |
| 1 | 61 | | await client.DownloadToFileAsync(uri, path, requestMessageAction: null); |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Downloads resource at URI to <see cref="string" />. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="client">The request.</param> |
| | 67 | | /// <param name="uri">The URI.</param> |
| | 68 | | public static async Task<string?> DownloadToStringAsync(this HttpClient? client, Uri? uri) |
| 1 | 69 | | { |
| 1 | 70 | | if (client == null) return null; |
| | 71 | |
|
| 1 | 72 | | var content = await client |
| 1 | 73 | | .GetStringAsync(uri) |
| 1 | 74 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 75 | |
|
| 1 | 76 | | return content; |
| 1 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Downloads resource at URI to <see cref="string" />. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="client">The request.</param> |
| | 83 | | /// <param name="uri">The URI.</param> |
| | 84 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 85 | | public static async Task<string?> DownloadToStringAsync(this HttpClient? client, Uri? uri, |
| | 86 | | Action<HttpRequestMessage>? requestMessageAction) |
| 1 | 87 | | { |
| 1 | 88 | | if (client == null) return null; |
| | 89 | |
|
| 1 | 90 | | using var request = new HttpRequestMessage(HttpMethod.Get, uri); |
| | 91 | |
|
| 1 | 92 | | requestMessageAction?.Invoke(request); |
| | 93 | |
|
| | 94 | | string? content; |
| | 95 | |
|
| 1 | 96 | | using var response = await client |
| 1 | 97 | | .SendAsync(request) |
| 1 | 98 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 99 | |
|
| 1 | 100 | | content = await response.Content |
| 1 | 101 | | .ReadAsStringAsync() |
| 1 | 102 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 103 | |
|
| 1 | 104 | | return content; |
| 1 | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Sends a <see cref="HttpMethod.Get"/> |
| | 109 | | /// <see cref="HttpRequestMessage"/>. |
| | 110 | | /// </summary> |
| | 111 | | /// <param name="client">The client.</param> |
| | 112 | | /// <param name="uri">The URI.</param> |
| | 113 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 114 | | public static async Task<HttpResponseMessage?> GetAsync(this HttpClient? client, Uri? uri, |
| | 115 | | Action<HttpRequestMessage>? requestMessageAction) => |
| 0 | 116 | | await client.SendAsync(uri, HttpMethod.Get, requestMessageAction); |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 120 | | /// with the specified <see cref="MimeTypes.ApplicationFormUrlEncoded" /> |
| | 121 | | /// request body using <see cref="HttpMethod.Post" /> |
| | 122 | | /// and <see cref="Encoding.UTF8" />. |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="client">The client.</param> |
| | 125 | | /// <param name="uri">The URI.</param> |
| | 126 | | /// <param name="formData">The form data.</param> |
| | 127 | | public static async Task<HttpResponseMessage?> PostFormAsync(this HttpClient? client, Uri? uri, |
| 0 | 128 | | Hashtable formData) => await client.PostFormAsync(uri, formData, requestMessageAction: null); |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 132 | | /// with the specified <see cref="MimeTypes.ApplicationFormUrlEncoded" /> |
| | 133 | | /// request body using <see cref="HttpMethod.Post" /> |
| | 134 | | /// and <see cref="Encoding.UTF8" />. |
| | 135 | | /// </summary> |
| | 136 | | /// <param name="client">The client.</param> |
| | 137 | | /// <param name="uri">The URI.</param> |
| | 138 | | /// <param name="formData">The form data.</param> |
| | 139 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 140 | | public static async Task<HttpResponseMessage?> PostFormAsync(this HttpClient? client, Uri? uri, |
| | 141 | | Hashtable formData, Action<HttpRequestMessage>? requestMessageAction) |
| 0 | 142 | | { |
| | 143 | | string GetPostData(Hashtable postData) |
| 0 | 144 | | { |
| 0 | 145 | | var sb = new StringBuilder(); |
| 0 | 146 | | var s = sb.ToString(); |
| | 147 | |
|
| 0 | 148 | | foreach (DictionaryEntry entry in postData) |
| 0 | 149 | | { |
| 0 | 150 | | s = (string.IsNullOrWhiteSpace(s)) |
| 0 | 151 | | ? string.Format(CultureInfo.InvariantCulture, "{0}={1}", entry.Key, entry.Value) |
| 0 | 152 | | : string.Format(CultureInfo.InvariantCulture, "&{0}={1}", entry.Key, entry.Value); |
| 0 | 153 | | sb.Append(s); |
| 0 | 154 | | } |
| | 155 | |
|
| 0 | 156 | | return sb.ToString(); |
| 0 | 157 | | } |
| | 158 | |
|
| 0 | 159 | | var postParams = GetPostData(formData); |
| | 160 | |
|
| 0 | 161 | | return await client.SendBodyAsync(uri, HttpMethod.Post, postParams, Encoding.UTF8, |
| 0 | 162 | | MimeTypes.ApplicationFormUrlEncoded, requestMessageAction: null); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 167 | | /// with the specified <see cref="MimeTypes.ApplicationJson"/> |
| | 168 | | /// request body using <see cref="HttpMethod.Post" /> |
| | 169 | | /// and <see cref="Encoding.UTF8"/>. |
| | 170 | | /// </summary> |
| | 171 | | /// <param name="client">The client.</param> |
| | 172 | | /// <param name="uri">The URI.</param> |
| | 173 | | /// <param name="requestBody">The request body.</param> |
| | 174 | | public static async Task<HttpResponseMessage?> |
| 0 | 175 | | PostJsonAsync(this HttpClient? client, Uri? uri, string requestBody) => await client.SendBodyAsync(uri, |
| 0 | 176 | | HttpMethod.Post, requestBody, Encoding.UTF8, MimeTypes.ApplicationJson, requestMessageAction: null); |
| | 177 | |
|
| | 178 | | /// <summary> |
| | 179 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 180 | | /// with the specified <see cref="MimeTypes.ApplicationJson" /> |
| | 181 | | /// request body using <see cref="HttpMethod.Post" /> |
| | 182 | | /// and <see cref="Encoding.UTF8" />. |
| | 183 | | /// </summary> |
| | 184 | | /// <param name="client">The client.</param> |
| | 185 | | /// <param name="uri">The URI.</param> |
| | 186 | | /// <param name="requestBody">The request body.</param> |
| | 187 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 188 | | public static async Task<HttpResponseMessage?> PostJsonAsync(this HttpClient? client, Uri? uri, |
| 0 | 189 | | string requestBody, Action<HttpRequestMessage>? requestMessageAction) => await client.SendBodyAsync(uri, |
| 0 | 190 | | HttpMethod.Post, requestBody, Encoding.UTF8, MimeTypes.ApplicationJson, requestMessageAction); |
| | 191 | |
|
| | 192 | | /// <summary> |
| | 193 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 194 | | /// with the specified <see cref="MimeTypes.ApplicationXml"/> |
| | 195 | | /// request body using <see cref="HttpMethod.Post" /> |
| | 196 | | /// and <see cref="Encoding.UTF8"/>. |
| | 197 | | /// </summary> |
| | 198 | | /// <param name="client">The client.</param> |
| | 199 | | /// <param name="uri">The URI.</param> |
| | 200 | | /// <param name="requestBody">The request body.</param> |
| | 201 | | public static async Task<HttpResponseMessage?> |
| 0 | 202 | | PostXmlAsync(this HttpClient? client, Uri? uri, string requestBody) => await client.SendBodyAsync(uri, |
| 0 | 203 | | HttpMethod.Post, requestBody, Encoding.UTF8, MimeTypes.ApplicationXml, requestMessageAction: null); |
| | 204 | |
|
| | 205 | | /// <summary> |
| | 206 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 207 | | /// with the specified <see cref="MimeTypes.ApplicationXml" /> |
| | 208 | | /// request body using <see cref="HttpMethod.Post" /> |
| | 209 | | /// and <see cref="Encoding.UTF8" />. |
| | 210 | | /// </summary> |
| | 211 | | /// <param name="client">The client.</param> |
| | 212 | | /// <param name="uri">The URI.</param> |
| | 213 | | /// <param name="requestBody">The request body.</param> |
| | 214 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 215 | | public static async Task<HttpResponseMessage?> PostXmlAsync(this HttpClient? client, Uri? uri, |
| 0 | 216 | | string requestBody, Action<HttpRequestMessage>? requestMessageAction) => await client.SendBodyAsync(uri, |
| 0 | 217 | | HttpMethod.Post, requestBody, Encoding.UTF8, MimeTypes.ApplicationXml, requestMessageAction); |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 221 | | /// with the specified <see cref="MimeTypes.ApplicationJson"/> |
| | 222 | | /// request body using <see cref="HttpMethod.Put" /> |
| | 223 | | /// and <see cref="Encoding.UTF8"/>. |
| | 224 | | /// </summary> |
| | 225 | | /// <param name="client">The client.</param> |
| | 226 | | /// <param name="uri">The URI.</param> |
| | 227 | | /// <param name="requestBody">The request body.</param> |
| | 228 | | public static async Task<HttpResponseMessage?> |
| 0 | 229 | | PutJsonAsync(this HttpClient? client, Uri? uri, string requestBody) => await client.SendBodyAsync(uri, |
| 0 | 230 | | HttpMethod.Put, requestBody, Encoding.UTF8, MimeTypes.ApplicationJson, requestMessageAction: null); |
| | 231 | |
|
| | 232 | | /// <summary> |
| | 233 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 234 | | /// with the specified <see cref="MimeTypes.ApplicationJson" /> |
| | 235 | | /// request body using <see cref="HttpMethod.Put" /> |
| | 236 | | /// and <see cref="Encoding.UTF8" />. |
| | 237 | | /// </summary> |
| | 238 | | /// <param name="client">The client.</param> |
| | 239 | | /// <param name="uri">The URI.</param> |
| | 240 | | /// <param name="requestBody">The request body.</param> |
| | 241 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 242 | | public static async Task<HttpResponseMessage?> PutJsonAsync(this HttpClient? client, Uri? uri, |
| 0 | 243 | | string requestBody, Action<HttpRequestMessage>? requestMessageAction) => await client.SendBodyAsync(uri, |
| 0 | 244 | | HttpMethod.Put, requestBody, Encoding.UTF8, MimeTypes.ApplicationJson, requestMessageAction); |
| | 245 | |
|
| | 246 | | /// <summary> |
| | 247 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 248 | | /// with the specified <see cref="MimeTypes.ApplicationXml"/> |
| | 249 | | /// request body using <see cref="HttpMethod.Put" /> |
| | 250 | | /// and <see cref="Encoding.UTF8"/>. |
| | 251 | | /// </summary> |
| | 252 | | /// <param name="client">The client.</param> |
| | 253 | | /// <param name="uri">The URI.</param> |
| | 254 | | /// <param name="requestBody">The request body.</param> |
| | 255 | | public static async Task<HttpResponseMessage?> |
| 0 | 256 | | PutXmlAsync(this HttpClient? client, Uri? uri, string requestBody) => await client.SendBodyAsync(uri, |
| 0 | 257 | | HttpMethod.Put, requestBody, Encoding.UTF8, MimeTypes.ApplicationXml, requestMessageAction: null); |
| | 258 | |
|
| | 259 | | /// <summary> |
| | 260 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 261 | | /// with the specified <see cref="MimeTypes.ApplicationXml" /> |
| | 262 | | /// request body using <see cref="HttpMethod.Put" /> |
| | 263 | | /// and <see cref="Encoding.UTF8" />. |
| | 264 | | /// </summary> |
| | 265 | | /// <param name="client">The client.</param> |
| | 266 | | /// <param name="uri">The URI.</param> |
| | 267 | | /// <param name="requestBody">The request body.</param> |
| | 268 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 269 | | public static async Task<HttpResponseMessage?> PutXmlAsync(this HttpClient? client, Uri? uri, |
| 0 | 270 | | string requestBody, Action<HttpRequestMessage>? requestMessageAction) => await client.SendBodyAsync(uri, |
| 0 | 271 | | HttpMethod.Put, requestBody, Encoding.UTF8, MimeTypes.ApplicationXml, requestMessageAction); |
| | 272 | |
|
| | 273 | | /// <summary> |
| | 274 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 275 | | /// </summary> |
| | 276 | | /// <param name="client">The client.</param> |
| | 277 | | /// <param name="uri">The URI.</param> |
| | 278 | | /// <param name="method">The method.</param> |
| | 279 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 280 | | public static async Task<HttpResponseMessage?> SendAsync(this HttpClient? client, Uri? uri, HttpMethod method, |
| | 281 | | Action<HttpRequestMessage>? requestMessageAction) |
| 0 | 282 | | { |
| 0 | 283 | | if (client == null) return null; |
| 0 | 284 | | ArgumentNullException.ThrowIfNull(uri); |
| | 285 | |
|
| 0 | 286 | | using var request = new HttpRequestMessage(method, uri); |
| | 287 | |
|
| 0 | 288 | | requestMessageAction?.Invoke(request); |
| | 289 | |
|
| 0 | 290 | | var response = await client |
| 0 | 291 | | .SendAsync(request) |
| 0 | 292 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 293 | |
|
| 0 | 294 | | return response; |
| 0 | 295 | | } |
| | 296 | |
|
| | 297 | | /// <summary> |
| | 298 | | /// Calls <see cref="HttpClient.SendAsync(HttpRequestMessage)" /> |
| | 299 | | /// with the specified request body. |
| | 300 | | /// </summary> |
| | 301 | | /// <param name="client">The client.</param> |
| | 302 | | /// <param name="uri">The URI.</param> |
| | 303 | | /// <param name="method">The method.</param> |
| | 304 | | /// <param name="requestBody">The request body.</param> |
| | 305 | | /// <param name="encoding">The encoding.</param> |
| | 306 | | /// <param name="mediaType">Type of the media.</param> |
| | 307 | | /// <param name="requestMessageAction">The request message action.</param> |
| | 308 | | public static async Task<HttpResponseMessage?> SendBodyAsync(this HttpClient? client, Uri? uri, HttpMethod method, |
| | 309 | | string? requestBody, Encoding encoding, string? mediaType, Action<HttpRequestMessage>? requestMessageAction) |
| 0 | 310 | | { |
| 0 | 311 | | if (client == null) return null; |
| | 312 | |
|
| 0 | 313 | | ArgumentNullException.ThrowIfNull(uri); |
| 0 | 314 | | requestBody.ThrowWhenNullOrWhiteSpace(); |
| 0 | 315 | | mediaType.ThrowWhenNullOrWhiteSpace(); |
| | 316 | |
|
| 0 | 317 | | using var request = new HttpRequestMessage(method, uri) |
| 0 | 318 | | { |
| 0 | 319 | | Content = new StringContent(requestBody, encoding, mediaType) |
| 0 | 320 | | }; |
| | 321 | |
|
| 0 | 322 | | requestMessageAction?.Invoke(request); |
| | 323 | |
|
| 0 | 324 | | var response = await client |
| 0 | 325 | | .SendAsync(request) |
| 0 | 326 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 327 | |
|
| 0 | 328 | | return response; |
| 0 | 329 | | } |
| | 330 | | } |