| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="HttpResponseMessage"/>. |
| | 5 | | /// </summary> |
| | 6 | | public static class HttpResponseMessageExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Downloads <see cref="HttpResponseMessage.Content"/> |
| | 10 | | /// from <see cref="byte"/> array to file. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="response">The response.</param> |
| | 13 | | /// <param name="fileInfo">The file information.</param> |
| | 14 | | public static async Task DownloadByteArrayToFileAsync(this HttpResponseMessage? response, FileSystemInfo? fileInfo) |
| 1 | 15 | | { |
| 1 | 16 | | ArgumentNullException.ThrowIfNull(fileInfo); |
| | 17 | |
|
| 1 | 18 | | await response.DownloadByteArrayToFileAsync(fileInfo.FullName); |
| 1 | 19 | | } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Downloads <see cref="HttpResponseMessage.Content"/> |
| | 23 | | /// from <see cref="byte"/> array to file. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="response">The response.</param> |
| | 26 | | /// <param name="target">The target.</param> |
| | 27 | | public static async Task DownloadByteArrayToFileAsync(this HttpResponseMessage? response, string target) |
| 1 | 28 | | { |
| 1 | 29 | | if (response == null) return; |
| | 30 | |
|
| 1 | 31 | | var data = await response.Content |
| 1 | 32 | | .ReadAsByteArrayAsync() |
| 1 | 33 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 34 | |
|
| 1 | 35 | | await File.WriteAllBytesAsync(target, data); |
| 1 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Downloads <see cref="HttpResponseMessage.Content"/> |
| | 40 | | /// from <see cref="byte"/> array to file. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="response">The response.</param> |
| | 43 | | /// <param name="fileInfo">The file information.</param> |
| | 44 | | public static async Task DownloadStringToFileAsync(this HttpResponseMessage? response, FileSystemInfo? fileInfo) |
| 1 | 45 | | { |
| 1 | 46 | | ArgumentNullException.ThrowIfNull(fileInfo); |
| | 47 | |
|
| 1 | 48 | | await response.DownloadStringToFileAsync(fileInfo.FullName); |
| 1 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Downloads <see cref="HttpResponseMessage.Content"/> |
| | 53 | | /// from <see cref="byte"/> array to file. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="response">The response.</param> |
| | 56 | | /// <param name="target">The target.</param> |
| | 57 | | public static async Task DownloadStringToFileAsync(this HttpResponseMessage? response, string? target) |
| 1 | 58 | | { |
| 1 | 59 | | if (response == null) return; |
| | 60 | |
|
| 1 | 61 | | target.ThrowWhenNullOrWhiteSpace(); |
| | 62 | |
|
| 1 | 63 | | var data = await response.Content |
| 1 | 64 | | .ReadAsStringAsync() |
| 1 | 65 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 66 | |
|
| 1 | 67 | | await File.WriteAllTextAsync(target, data); |
| 1 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Returns <c>true</c> when <see cref="HttpResponseMessage"/> |
| | 72 | | /// is <see cref="HttpStatusCode.Moved"/>, <see cref="HttpStatusCode.MovedPermanently"/> |
| | 73 | | /// or <see cref="HttpStatusCode.Redirect"/>. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="response">The response.</param> |
| | 76 | | public static bool IsMovedOrRedirected(this HttpResponseMessage? response) => |
| 0 | 77 | | response?.StatusCode is HttpStatusCode.Moved or HttpStatusCode.MovedPermanently or HttpStatusCode.Redirect; |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Serializes the <see cref="HttpResponseMessage"/> |
| | 81 | | /// to the specified <c>TInstance</c> |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="response">The response.</param> |
| | 84 | | /// <typeparam name="TInstance">The type of the instance.</typeparam> |
| | 85 | | /// <remarks> |
| | 86 | | /// This method uses the Microsoft API to deserialize. |
| | 87 | | /// </remarks> |
| | 88 | | public static async Task<TInstance?> StreamToInstanceAsync<TInstance>(this HttpResponseMessage? response) => |
| 0 | 89 | | await response.StreamToInstanceAsync<TInstance>(options: null); |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Serializes the <see cref="HttpResponseMessage"/> |
| | 93 | | /// to the specified <c>TInstance</c> |
| | 94 | | /// </summary> |
| | 95 | | /// <param name="response">The response.</param> |
| | 96 | | /// <param name="options">The <see cref="JsonSerializerOptions"/></param> |
| | 97 | | /// <typeparam name="TInstance">The type of the instance.</typeparam> |
| | 98 | | /// <remarks> |
| | 99 | | /// This method uses the Microsoft API to deserialize. |
| | 100 | | /// </remarks> |
| | 101 | | public static async Task<TInstance?> StreamToInstanceAsync<TInstance>(this HttpResponseMessage? response, |
| | 102 | | JsonSerializerOptions? options) |
| 1 | 103 | | { |
| 1 | 104 | | if (response == null) |
| 0 | 105 | | return await Task |
| 0 | 106 | | .FromResult(default(TInstance)) |
| 0 | 107 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 108 | |
|
| 1 | 109 | | await using var stream = await response.Content.ReadAsStreamAsync(); |
| | 110 | |
|
| 1 | 111 | | if (stream.CanRead == false) |
| 0 | 112 | | return await Task |
| 0 | 113 | | .FromResult(default(TInstance)) |
| 0 | 114 | | .ConfigureAwait(continueOnCapturedContext: false); |
| | 115 | |
|
| 1 | 116 | | using var streamReader = new StreamReader(stream); |
| | 117 | |
|
| 1 | 118 | | var instance = await JsonSerializer |
| 1 | 119 | | .DeserializeAsync<TInstance>(stream, options); |
| | 120 | |
|
| 1 | 121 | | return instance; |
| 1 | 122 | | } |
| | 123 | | } |