< Summary

Information
Class: TeleFlow.Telegram.TelegramTransportResponse
Assembly: TeleFlow.Telegram.Client
File(s): /_/src/TeleFlow.Telegram.Client/TelegramTransportResponse.cs
Line coverage
91%
Covered lines: 21
Uncovered lines: 2
Coverable lines: 23
Total lines: 104
Line coverage: 91.3%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%210%
.ctor(...)100%22100%
FromOwnedBytes(...)100%11100%
TryGetHeaderValues(...)100%11100%
CopyHeaders(...)100%44100%
EncodeBody(...)100%11100%
AsMemory(...)100%11100%

File(s)

/_/src/TeleFlow.Telegram.Client/TelegramTransportResponse.cs

#LineLine coverage
 1using System.Text;
 2
 3namespace TeleFlow.Telegram;
 4
 5/// <summary>
 6/// Represents one raw Telegram Bot API HTTP response returned by an <see cref="ITelegramTransport"/>.
 7/// The request executor parses the UTF-8 response body into a Telegram envelope before mapping it to typed API results 
 8/// </summary>
 9public sealed class TelegramTransportResponse
 10{
 11    public TelegramTransportResponse(
 12        int statusCode,
 13        string body,
 14        IReadOnlyDictionary<string, IReadOnlyList<string>>? headers = null)
 1815        : this(statusCode, EncodeBody(body), headers, copyBody: false)
 16    {
 1817    }
 18
 19    public TelegramTransportResponse(
 20        int statusCode,
 21        byte[] body,
 22        IReadOnlyDictionary<string, IReadOnlyList<string>>? headers = null)
 423        : this(statusCode, AsMemory(body), headers, copyBody: true)
 24    {
 425    }
 26
 27    public TelegramTransportResponse(
 28        int statusCode,
 29        ReadOnlyMemory<byte> body,
 30        IReadOnlyDictionary<string, IReadOnlyList<string>>? headers = null)
 031        : this(statusCode, body, headers, copyBody: true)
 32    {
 033    }
 34
 35    private TelegramTransportResponse(
 36        int statusCode,
 37        ReadOnlyMemory<byte> body,
 38        IReadOnlyDictionary<string, IReadOnlyList<string>>? headers,
 39        bool copyBody)
 40    {
 41        StatusCode = statusCode;
 9342        Body = copyBody ? body.ToArray() : body;
 9343        Headers = CopyHeaders(headers);
 9344    }
 45
 46    internal static TelegramTransportResponse FromOwnedBytes(
 47        int statusCode,
 48        byte[] body,
 49        IReadOnlyDictionary<string, IReadOnlyList<string>>? headers = null)
 50    {
 7151        ArgumentNullException.ThrowIfNull(body);
 7152        return new TelegramTransportResponse(statusCode, body, headers, copyBody: false);
 53    }
 54
 55    /// <summary>
 56    /// Gets the HTTP status code returned by Telegram.
 57    /// </summary>
 58    public int StatusCode { get; }
 59
 60    /// <summary>
 61    /// Gets the raw UTF-8 response body returned by Telegram.
 62    /// </summary>
 63    public ReadOnlyMemory<byte> Body { get; }
 64
 65    /// <summary>
 66    /// Gets response headers copied from the underlying transport.
 67    /// </summary>
 68    public IReadOnlyDictionary<string, IReadOnlyList<string>> Headers { get; }
 69
 70    /// <summary>
 71    /// Tries to read response header values by name using case-insensitive lookup.
 72    /// </summary>
 73    public bool TryGetHeaderValues(string name, out IReadOnlyList<string> values)
 74    {
 1275        ArgumentException.ThrowIfNullOrWhiteSpace(name);
 1276        return Headers.TryGetValue(name, out values!);
 77    }
 78
 79    private static Dictionary<string, IReadOnlyList<string>> CopyHeaders(
 80        IReadOnlyDictionary<string, IReadOnlyList<string>>? headers)
 81    {
 9382        if (headers is null || headers.Count == 0)
 83        {
 2184            return new Dictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase);
 85        }
 86
 7287        return headers.ToDictionary(
 14788            static pair => pair.Key,
 14789            static pair => (IReadOnlyList<string>)pair.Value.ToArray(),
 7290            StringComparer.OrdinalIgnoreCase);
 91    }
 92
 93    private static byte[] EncodeBody(string body)
 94    {
 1895        ArgumentNullException.ThrowIfNull(body);
 1896        return Encoding.UTF8.GetBytes(body);
 97    }
 98
 99    private static ReadOnlyMemory<byte> AsMemory(byte[] body)
 100    {
 4101        ArgumentNullException.ThrowIfNull(body);
 4102        return body.AsMemory();
 103    }
 104}