< 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    {
 30716        ArgumentNullException.ThrowIfNull(serializerOptions);
 30717        _serializerOptions = serializerOptions;
 30718    }
 19
 20    public bool TryParse(
 21        ReadOnlyMemory<byte> responseBody,
 22        out TelegramTransportEnvelope envelope,
 23        out JsonException? exception)
 24    {
 9325        JsonDocument? document = null;
 26
 27        try
 28        {
 9329            document = JsonDocument.Parse(responseBody);
 8830            var root = document.RootElement;
 31
 8832            var ok = root.TryGetProperty("ok", out var okElement) && okElement.ValueKind is JsonValueKind.True or JsonVa
 8833                ? okElement.GetBoolean()
 8834                : throw new JsonException("Telegram response does not contain a valid 'ok' property.");
 35
 8836            var hasResult = root.TryGetProperty("result", out var resultElement);
 37
 8838            string? description = null;
 8839            if (root.TryGetProperty("description", out var descriptionElement) &&
 8840                descriptionElement.ValueKind == JsonValueKind.String)
 41            {
 2842                description = descriptionElement.GetString();
 43            }
 44
 8845            int? errorCode = null;
 8846            if (root.TryGetProperty("error_code", out var errorCodeElement) &&
 8847                errorCodeElement.ValueKind == JsonValueKind.Number &&
 8848                errorCodeElement.TryGetInt32(out var parsedErrorCode))
 49            {
 2850                errorCode = parsedErrorCode;
 51            }
 52
 8853            ResponseParameters? responseParameters = null;
 8854            if (root.TryGetProperty("response_parameters", out var responseParametersElement))
 55            {
 956                responseParameters = JsonSerializer.Deserialize<ResponseParameters>(
 957                    responseParametersElement,
 958                    _serializerOptions);
 59            }
 60
 8861            envelope = new TelegramTransportEnvelope(
 8862                document,
 8863                ok,
 8864                hasResult,
 8865                resultElement,
 8866                description,
 8867                errorCode,
 8868                responseParameters);
 8869            document = null;
 8870            exception = null;
 8871            return true;
 72        }
 573        catch (JsonException jsonException)
 74        {
 575            envelope = null!;
 576            exception = jsonException;
 577            return false;
 78        }
 79        finally
 80        {
 9381            document?.Dispose();
 9382        }
 9383    }
 84}