| | | 1 | | using System.Text.Json; |
| | | 2 | | using TeleFlow.Telegram.Schema.Methods; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram.Internal; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Adapts getUpdates to a response contract that isolates schema failures to individual update elements. |
| | | 8 | | /// </summary> |
| | | 9 | | internal sealed class TelegramUpdateBatchRequest(GetUpdates request) : |
| | | 10 | | ITelegramExecutableRequest<TelegramUpdateBatchResponse> |
| | | 11 | | { |
| | 12 | 12 | | private readonly GetUpdates _request = request ?? throw new ArgumentNullException(nameof(request)); |
| | | 13 | | |
| | 51 | 14 | | public string MethodName => GetUpdates.MethodName; |
| | | 15 | | |
| | 12 | 16 | | public object Payload => _request; |
| | | 17 | | |
| | | 18 | | public TelegramUpdateBatchResponse DeserializeResponse( |
| | | 19 | | JsonSerializerOptions serializerOptions, |
| | | 20 | | JsonElement result) |
| | | 21 | | { |
| | 11 | 22 | | ArgumentNullException.ThrowIfNull(serializerOptions); |
| | | 23 | | |
| | 11 | 24 | | if (result.ValueKind != JsonValueKind.Array) |
| | | 25 | | { |
| | 0 | 26 | | throw new JsonException("Telegram getUpdates result must be an array."); |
| | | 27 | | } |
| | | 28 | | |
| | 11 | 29 | | var items = new List<TelegramUpdateDecodeResult>(result.GetArrayLength()); |
| | 52 | 30 | | foreach (var payload in result.EnumerateArray()) |
| | | 31 | | { |
| | 15 | 32 | | if (payload.ValueKind != JsonValueKind.Object || |
| | 15 | 33 | | !payload.TryGetProperty("update_id", out var updateIdElement) || |
| | 15 | 34 | | updateIdElement.ValueKind != JsonValueKind.Number || |
| | 15 | 35 | | !updateIdElement.TryGetInt64(out var updateId)) |
| | | 36 | | { |
| | 0 | 37 | | throw new JsonException("Telegram update payload does not contain a valid 'update_id'."); |
| | | 38 | | } |
| | | 39 | | |
| | 15 | 40 | | items.Add(TelegramUpdateDecoder.Decode( |
| | 15 | 41 | | payload, |
| | 15 | 42 | | updateId, |
| | 15 | 43 | | serializerOptions, |
| | 15 | 44 | | MethodName)); |
| | | 45 | | } |
| | | 46 | | |
| | 11 | 47 | | return new TelegramUpdateBatchResponse(items); |
| | | 48 | | } |
| | | 49 | | } |