< Summary

Information
Class: TeleFlow.Telegram.Internal.SchemaTelegramRequest<T>
Assembly: TeleFlow.Telegram.Client
File(s): /_/src/TeleFlow.Telegram.Client/Internal/SchemaTelegramRequest.cs
Line coverage
78%
Covered lines: 15
Uncovered lines: 4
Coverable lines: 19
Total lines: 59
Line coverage: 78.9%
Branch coverage
60%
Covered branches: 6
Total branches: 10
Branch coverage: 60%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
get_MethodName()100%22100%
get_Payload()100%11100%
DeserializeResponse(...)50%2266.66%
ResolveMethodName(...)50%7671.42%

File(s)

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

#LineLine coverage
 1using System.Collections.Concurrent;
 2using System.Reflection;
 3using System.Text.Json;
 4using TeleFlow.Telegram.Schema.Abstractions;
 5
 6namespace TeleFlow.Telegram.Internal;
 7
 8/// <summary>
 9/// Adapts a generated schema method object to the executable request contract used by the Telegram client runtime.
 10/// It resolves the generated method name and maps a parsed Telegram result element into the strongly typed method resul
 11/// </summary>
 12internal sealed class SchemaTelegramRequest<TResult> :
 13    ITelegramExecutableRequest<TelegramRequestResult<TResult>>
 14{
 615    private static readonly ConcurrentDictionary<Type, string> MethodNameCache = [];
 16    private readonly ITelegramApiMethod<TResult> _method;
 17
 18    public SchemaTelegramRequest(ITelegramApiMethod<TResult> method)
 19    {
 8520        ArgumentNullException.ThrowIfNull(method);
 8521        _method = method;
 8522    }
 23
 26924    public string MethodName => MethodNameCache.GetOrAdd(_method.GetType(), ResolveMethodName);
 25
 9226    public object Payload => _method;
 27
 28    public TelegramRequestResult<TResult> DeserializeResponse(
 29        JsonSerializerOptions serializerOptions,
 30        JsonElement result)
 31    {
 5432        ArgumentNullException.ThrowIfNull(serializerOptions);
 33
 5434        var value = result.Deserialize<TResult>(serializerOptions);
 35
 5336        if (value is null)
 37        {
 038            throw new TelegramRequestException(
 039                $"Telegram response for method '{MethodName}' did not contain a deserializable result payload.");
 40        }
 41
 5342        return new TelegramRequestResult<TResult>(value);
 43    }
 44
 45    private static string ResolveMethodName(Type methodType)
 46    {
 947        var property = methodType.GetProperty(
 948            "MethodName",
 949            BindingFlags.Public | BindingFlags.Static);
 50
 951        if (property?.GetValue(null) is string methodName && !string.IsNullOrWhiteSpace(methodName))
 52        {
 953            return methodName;
 54        }
 55
 056        throw new InvalidOperationException(
 057            $"Telegram schema method type '{methodType.FullName}' does not expose a public static MethodName property.")
 58    }
 59}