< Summary

Information
Class: TeleFlow.Telegram.Internal.TelegramTransportEnvelopeParser
Assembly: TeleFlow.Telegram.Client
File(s): /_/src/TeleFlow.Telegram.Client/Internal/TelegramTransportEnvelopeParser.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 84
Line coverage: 100%
Branch coverage
85%
Covered branches: 17
Total branches: 20
Branch coverage: 85%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
TryParse(...)85%2020100%

File(s)

/_/src/TeleFlow.Telegram.Client/Internal/TelegramTransportEnvelopeParser.cs

#LineLine coverage
 1using System.Text.Json;
 2using TeleFlow.Telegram.Schema.Types;
 3
 4namespace TeleFlow.Telegram.Internal;
 5
 6/// <summary>
 7/// Parses raw Telegram Bot API response bytes into an owned envelope used by the request executor.
 8/// It keeps the parsed JSON document alive so successful result payloads can be deserialized without rematerializing JS
 9/// </summary>
 10internal sealed class TelegramTransportEnvelopeParser
 11{
 12    private readonly JsonSerializerOptions _serializerOptions;
 13
 14    public TelegramTransportEnvelopeParser(JsonSerializerOptions serializerOptions)
 15    {
 30216        ArgumentNullException.ThrowIfNull(serializerOptions);
 30217        _serializerOptions = serializerOptions;
 30218    }
 19
 20    public bool TryParse(
 21        ReadOnlyMemory<byte> responseBody,
 22        out TelegramTransportEnvelope envelope,
 23        out JsonException? exception)
 24    {
 8725        JsonDocument? document = null;
 26
 27        try
 28        {
 8729            document = JsonDocument.Parse(responseBody);
 8230            var root = document.RootElement;
 31
 8232            var ok = root.TryGetProperty("ok", out var okElement) && okElement.ValueKind is JsonValueKind.True or JsonVa
 8233                ? okElement.GetBoolean()
 8234                : throw new JsonException("Telegram response does not contain a valid 'ok' property.");
 35
 8236            var hasResult = root.TryGetProperty("result", out var resultElement);
 37
 8238            string? description = null;
 8239            if (root.TryGetProperty("description", out var descriptionElement) &&
 8240                descriptionElement.ValueKind == JsonValueKind.String)
 41            {
 2842                description = descriptionElement.GetString();
 43            }
 44
 8245            int? errorCode = null;
 8246            if (root.TryGetProperty("error_code", out var errorCodeElement) &&
 8247                errorCodeElement.ValueKind == JsonValueKind.Number &&
 8248                errorCodeElement.TryGetInt32(out var parsedErrorCode))
 49            {
 2850                errorCode = parsedErrorCode;
 51            }
 52
 8253            ResponseParameters? responseParameters = null;
 8254            if (root.TryGetProperty("response_parameters", out var responseParametersElement))
 55            {
 956                responseParameters = JsonSerializer.Deserialize<ResponseParameters>(
 957                    responseParametersElement,
 958                    _serializerOptions);
 59            }
 60
 8261            envelope = new TelegramTransportEnvelope(
 8262                document,
 8263                ok,
 8264                hasResult,
 8265                resultElement,
 8266                description,
 8267                errorCode,
 8268                responseParameters);
 8269            document = null;
 8270            exception = null;
 8271            return true;
 72        }
 573        catch (JsonException jsonException)
 74        {
 575            envelope = null!;
 576            exception = jsonException;
 577            return false;
 78        }
 79        finally
 80        {
 8781            document?.Dispose();
 8782        }
 8783    }
 84}