< Summary - SonghayCore

Information
Class: Songhay.Extensions.HttpClientExtensions
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/HttpClientExtensions.cs
Line coverage
37%
Covered lines: 37
Uncovered lines: 61
Coverable lines: 98
Total lines: 330
Line coverage: 37.7%
Branch coverage
34%
Covered branches: 9
Total branches: 26
Branch coverage: 34.6%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
DeleteAsync()100%10%
DownloadToFileAsync()75%8100%
DownloadToFileAsync()100%1100%
DownloadToStringAsync()50%2100%
DownloadToStringAsync()50%4100%
GetAsync()100%10%
PostFormAsync()100%10%
PostFormAsync()100%10%
PostJsonAsync()100%10%
PostJsonAsync()100%10%
PostXmlAsync()100%10%
PostXmlAsync()100%10%
PutJsonAsync()100%10%
PutJsonAsync()100%10%
PutXmlAsync()100%10%
PutXmlAsync()100%10%
SendAsync()0%40%
SendBodyAsync()0%40%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/HttpClientExtensions.cs

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3/// <summary>
 4/// Extensions of <see cref="HttpClient"/>
 5/// </summary>
 6public 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) =>
 017        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)
 128    {
 129        if (client == null) return;
 30
 131        var buffer = new byte[32768];
 32        int bytesRead;
 33
 134        using var request = new HttpRequestMessage(HttpMethod.Get, uri);
 35
 136        requestMessageAction?.Invoke(request);
 37
 138        using var response = await client
 139            .SendAsync(request)
 140            .ConfigureAwait(continueOnCapturedContext: false);
 41
 142        await using var stream = await response.Content
 143            .ReadAsStreamAsync()
 144            .ConfigureAwait(continueOnCapturedContext: false);
 45
 146        await using var fs = File.Create(path);
 47
 648        while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
 549        {
 550            fs.Write(buffer, 0, bytesRead);
 551        }
 152    }
 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) =>
 161        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)
 169    {
 170        if (client == null) return null;
 71
 172        var content = await client
 173            .GetStringAsync(uri)
 174            .ConfigureAwait(continueOnCapturedContext: false);
 75
 176        return content;
 177    }
 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)
 187    {
 188        if (client == null) return null;
 89
 190        using var request = new HttpRequestMessage(HttpMethod.Get, uri);
 91
 192        requestMessageAction?.Invoke(request);
 93
 94        string? content;
 95
 196        using var response = await client
 197            .SendAsync(request)
 198            .ConfigureAwait(continueOnCapturedContext: false);
 99
 1100        content = await response.Content
 1101            .ReadAsStringAsync()
 1102            .ConfigureAwait(continueOnCapturedContext: false);
 103
 1104        return content;
 1105    }
 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) =>
 0116        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,
 0128        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)
 0142    {
 143        string GetPostData(Hashtable postData)
 0144        {
 0145            var sb = new StringBuilder();
 0146            var s = sb.ToString();
 147
 0148            foreach (DictionaryEntry entry in postData)
 0149            {
 0150                s = (string.IsNullOrWhiteSpace(s))
 0151                    ? string.Format(CultureInfo.InvariantCulture, "{0}={1}", entry.Key, entry.Value)
 0152                    : string.Format(CultureInfo.InvariantCulture, "&{0}={1}", entry.Key, entry.Value);
 0153                sb.Append(s);
 0154            }
 155
 0156            return sb.ToString();
 0157        }
 158
 0159        var postParams = GetPostData(formData);
 160
 0161        return await client.SendBodyAsync(uri, HttpMethod.Post, postParams, Encoding.UTF8,
 0162            MimeTypes.ApplicationFormUrlEncoded, requestMessageAction: null);
 0163    }
 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?>
 0175        PostJsonAsync(this HttpClient? client, Uri? uri, string requestBody) => await client.SendBodyAsync(uri,
 0176        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,
 0189        string requestBody, Action<HttpRequestMessage>? requestMessageAction) => await client.SendBodyAsync(uri,
 0190        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?>
 0202        PostXmlAsync(this HttpClient? client, Uri? uri, string requestBody) => await client.SendBodyAsync(uri,
 0203        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,
 0216        string requestBody, Action<HttpRequestMessage>? requestMessageAction) => await client.SendBodyAsync(uri,
 0217        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?>
 0229        PutJsonAsync(this HttpClient? client, Uri? uri, string requestBody) => await client.SendBodyAsync(uri,
 0230        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,
 0243        string requestBody, Action<HttpRequestMessage>? requestMessageAction) => await client.SendBodyAsync(uri,
 0244        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?>
 0256        PutXmlAsync(this HttpClient? client, Uri? uri, string requestBody) => await client.SendBodyAsync(uri,
 0257        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,
 0270        string requestBody, Action<HttpRequestMessage>? requestMessageAction) => await client.SendBodyAsync(uri,
 0271        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)
 0282    {
 0283        if (client == null) return null;
 0284        ArgumentNullException.ThrowIfNull(uri);
 285
 0286        using var request = new HttpRequestMessage(method, uri);
 287
 0288        requestMessageAction?.Invoke(request);
 289
 0290        var response = await client
 0291            .SendAsync(request)
 0292            .ConfigureAwait(continueOnCapturedContext: false);
 293
 0294        return response;
 0295    }
 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)
 0310    {
 0311        if (client == null) return null;
 312
 0313        ArgumentNullException.ThrowIfNull(uri);
 0314        requestBody.ThrowWhenNullOrWhiteSpace();
 0315        mediaType.ThrowWhenNullOrWhiteSpace();
 316
 0317        using var request = new HttpRequestMessage(method, uri)
 0318        {
 0319            Content = new StringContent(requestBody, encoding, mediaType)
 0320        };
 321
 0322        requestMessageAction?.Invoke(request);
 323
 0324        var response = await client
 0325            .SendAsync(request)
 0326            .ConfigureAwait(continueOnCapturedContext: false);
 327
 0328        return response;
 0329    }
 330}