| | | 1 | | using System.Text.Json; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Builds the transport-level request body for Telegram API method payloads. |
| | | 7 | | /// It keeps the normal JSON path cheap and delegates multipart-specific work only when upload data is present. |
| | | 8 | | /// This is reached by the Telegram request executor for every outgoing Bot API call, including ctx.Bot.*Async helpers. |
| | | 9 | | /// </summary> |
| | | 10 | | internal sealed class TelegramRequestContentBuilder |
| | | 11 | | { |
| | | 12 | | private readonly JsonSerializerOptions _serializerOptions; |
| | | 13 | | |
| | | 14 | | public TelegramRequestContentBuilder(JsonSerializerOptions serializerOptions) |
| | | 15 | | { |
| | 302 | 16 | | ArgumentNullException.ThrowIfNull(serializerOptions); |
| | 302 | 17 | | _serializerOptions = serializerOptions; |
| | 302 | 18 | | } |
| | | 19 | | |
| | | 20 | | public TelegramTransportContent Build(object payload) |
| | | 21 | | { |
| | 92 | 22 | | ArgumentNullException.ThrowIfNull(payload); |
| | 92 | 23 | | var payloadType = payload.GetType(); |
| | | 24 | | |
| | | 25 | | // Telegram requests are JSON unless a concrete value contains an InputFile stream. |
| | | 26 | | // The type check is the cheap hot-path proof; the value scan only runs for upload-capable shapes. |
| | 92 | 27 | | if (!TelegramRequestUploadDetector.MayContainInputFile(payloadType) || |
| | 92 | 28 | | !TelegramRequestUploadDetector.ContainsInputFile(payload)) |
| | | 29 | | { |
| | 83 | 30 | | var json = JsonSerializer.Serialize(payload, payloadType, _serializerOptions); |
| | 83 | 31 | | return new TelegramJsonTransportContent(json); |
| | | 32 | | } |
| | | 33 | | |
| | 9 | 34 | | var builder = new TelegramMultipartContentBuilder(_serializerOptions); |
| | 9 | 35 | | return builder.Build(payload); |
| | | 36 | | } |
| | | 37 | | } |