| | | 1 | | using System.Text; |
| | | 2 | | |
| | | 3 | | namespace 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> |
| | | 9 | | public sealed class TelegramTransportResponse |
| | | 10 | | { |
| | | 11 | | public TelegramTransportResponse( |
| | | 12 | | int statusCode, |
| | | 13 | | string body, |
| | | 14 | | IReadOnlyDictionary<string, IReadOnlyList<string>>? headers = null) |
| | 18 | 15 | | : this(statusCode, EncodeBody(body), headers, copyBody: false) |
| | | 16 | | { |
| | 18 | 17 | | } |
| | | 18 | | |
| | | 19 | | public TelegramTransportResponse( |
| | | 20 | | int statusCode, |
| | | 21 | | byte[] body, |
| | | 22 | | IReadOnlyDictionary<string, IReadOnlyList<string>>? headers = null) |
| | 4 | 23 | | : this(statusCode, AsMemory(body), headers, copyBody: true) |
| | | 24 | | { |
| | 4 | 25 | | } |
| | | 26 | | |
| | | 27 | | public TelegramTransportResponse( |
| | | 28 | | int statusCode, |
| | | 29 | | ReadOnlyMemory<byte> body, |
| | | 30 | | IReadOnlyDictionary<string, IReadOnlyList<string>>? headers = null) |
| | 0 | 31 | | : this(statusCode, body, headers, copyBody: true) |
| | | 32 | | { |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | private TelegramTransportResponse( |
| | | 36 | | int statusCode, |
| | | 37 | | ReadOnlyMemory<byte> body, |
| | | 38 | | IReadOnlyDictionary<string, IReadOnlyList<string>>? headers, |
| | | 39 | | bool copyBody) |
| | | 40 | | { |
| | | 41 | | StatusCode = statusCode; |
| | 93 | 42 | | Body = copyBody ? body.ToArray() : body; |
| | 93 | 43 | | Headers = CopyHeaders(headers); |
| | 93 | 44 | | } |
| | | 45 | | |
| | | 46 | | internal static TelegramTransportResponse FromOwnedBytes( |
| | | 47 | | int statusCode, |
| | | 48 | | byte[] body, |
| | | 49 | | IReadOnlyDictionary<string, IReadOnlyList<string>>? headers = null) |
| | | 50 | | { |
| | 71 | 51 | | ArgumentNullException.ThrowIfNull(body); |
| | 71 | 52 | | 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 | | { |
| | 12 | 75 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | 12 | 76 | | return Headers.TryGetValue(name, out values!); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | private static Dictionary<string, IReadOnlyList<string>> CopyHeaders( |
| | | 80 | | IReadOnlyDictionary<string, IReadOnlyList<string>>? headers) |
| | | 81 | | { |
| | 93 | 82 | | if (headers is null || headers.Count == 0) |
| | | 83 | | { |
| | 21 | 84 | | return new Dictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase); |
| | | 85 | | } |
| | | 86 | | |
| | 72 | 87 | | return headers.ToDictionary( |
| | 147 | 88 | | static pair => pair.Key, |
| | 147 | 89 | | static pair => (IReadOnlyList<string>)pair.Value.ToArray(), |
| | 72 | 90 | | StringComparer.OrdinalIgnoreCase); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private static byte[] EncodeBody(string body) |
| | | 94 | | { |
| | 18 | 95 | | ArgumentNullException.ThrowIfNull(body); |
| | 18 | 96 | | return Encoding.UTF8.GetBytes(body); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static ReadOnlyMemory<byte> AsMemory(byte[] body) |
| | | 100 | | { |
| | 4 | 101 | | ArgumentNullException.ThrowIfNull(body); |
| | 4 | 102 | | return body.AsMemory(); |
| | | 103 | | } |
| | | 104 | | } |