< Summary - SonghayCore

Information
Class: Songhay.Extensions.HttpResponseMessageExtensions
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/HttpResponseMessageExtensions.cs
Line coverage
80%
Covered lines: 32
Uncovered lines: 8
Coverable lines: 40
Total lines: 123
Line coverage: 80%
Branch coverage
40%
Covered branches: 8
Total branches: 20
Branch coverage: 40%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

File(s)

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

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3/// <summary>
 4/// Extensions of <see cref="HttpResponseMessage"/>.
 5/// </summary>
 6public 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)
 115    {
 116        ArgumentNullException.ThrowIfNull(fileInfo);
 17
 118        await response.DownloadByteArrayToFileAsync(fileInfo.FullName);
 119    }
 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)
 128    {
 129        if (response == null) return;
 30
 131        var data = await response.Content
 132            .ReadAsByteArrayAsync()
 133            .ConfigureAwait(continueOnCapturedContext: false);
 34
 135        await File.WriteAllBytesAsync(target, data);
 136    }
 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)
 145    {
 146        ArgumentNullException.ThrowIfNull(fileInfo);
 47
 148        await response.DownloadStringToFileAsync(fileInfo.FullName);
 149    }
 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)
 158    {
 159        if (response == null) return;
 60
 161        target.ThrowWhenNullOrWhiteSpace();
 62
 163        var data = await response.Content
 164            .ReadAsStringAsync()
 165            .ConfigureAwait(continueOnCapturedContext: false);
 66
 167        await File.WriteAllTextAsync(target, data);
 168    }
 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) =>
 077        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) =>
 089        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)
 1103    {
 1104        if (response == null)
 0105            return await Task
 0106                .FromResult(default(TInstance))
 0107                .ConfigureAwait(continueOnCapturedContext: false);
 108
 1109        await using var stream = await response.Content.ReadAsStreamAsync();
 110
 1111        if (stream.CanRead == false)
 0112            return await Task
 0113                .FromResult(default(TInstance))
 0114                .ConfigureAwait(continueOnCapturedContext: false);
 115
 1116        using var streamReader = new StreamReader(stream);
 117
 1118        var instance = await JsonSerializer
 1119            .DeserializeAsync<TInstance>(stream, options);
 120
 1121        return instance;
 1122    }
 123}