| | | 1 | | namespace TeleFlow.Telegram.Internal; |
| | | 2 | | |
| | | 3 | | internal sealed class TelegramRequestSender |
| | | 4 | | { |
| | | 5 | | private readonly ITelegramTransport _transport; |
| | | 6 | | private readonly TelegramClientOptions _options; |
| | | 7 | | private readonly TelegramRequestContentBuilder _contentBuilder; |
| | | 8 | | |
| | | 9 | | public TelegramRequestSender( |
| | | 10 | | ITelegramTransport transport, |
| | | 11 | | TelegramClientOptions options, |
| | | 12 | | TelegramRequestContentBuilder contentBuilder) |
| | | 13 | | { |
| | 302 | 14 | | ArgumentNullException.ThrowIfNull(transport); |
| | 302 | 15 | | ArgumentNullException.ThrowIfNull(options); |
| | 302 | 16 | | ArgumentNullException.ThrowIfNull(contentBuilder); |
| | | 17 | | |
| | 302 | 18 | | _transport = transport; |
| | 302 | 19 | | _options = options; |
| | 302 | 20 | | _contentBuilder = contentBuilder; |
| | 302 | 21 | | } |
| | | 22 | | |
| | | 23 | | public TelegramTransportRequest CreateRequest<TResponse>( |
| | | 24 | | ITelegramExecutableRequest<TResponse> request) |
| | | 25 | | where TResponse : ITelegramResponse |
| | | 26 | | { |
| | 92 | 27 | | return new TelegramTransportRequest( |
| | 92 | 28 | | request.MethodName, |
| | 92 | 29 | | BuildMethodUri(request.MethodName), |
| | 92 | 30 | | _contentBuilder.Build(request.Payload)); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<TelegramTransportResponse> SendAsync( |
| | | 34 | | TelegramTransportRequest request, |
| | | 35 | | CancellationToken cancellationToken) |
| | | 36 | | { |
| | 91 | 37 | | ArgumentNullException.ThrowIfNull(request); |
| | | 38 | | |
| | | 39 | | try |
| | | 40 | | { |
| | 91 | 41 | | return await _transport.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| | | 42 | | } |
| | 1 | 43 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 44 | | { |
| | 1 | 45 | | throw; |
| | | 46 | | } |
| | 3 | 47 | | catch (TelegramRequestException) |
| | | 48 | | { |
| | 3 | 49 | | throw; |
| | | 50 | | } |
| | 0 | 51 | | catch (OperationCanceledException exception) |
| | | 52 | | { |
| | 0 | 53 | | throw new TelegramNetworkException( |
| | 0 | 54 | | $"Telegram request '{request.MethodName}' failed because the HTTP transport timed out or was interrupted |
| | 0 | 55 | | exception, |
| | 0 | 56 | | request.MethodName); |
| | | 57 | | } |
| | 0 | 58 | | catch (Exception exception) |
| | | 59 | | { |
| | 0 | 60 | | throw new TelegramNetworkException( |
| | 0 | 61 | | $"Telegram request '{request.MethodName}' failed because the Telegram transport failed.", |
| | 0 | 62 | | exception, |
| | 0 | 63 | | request.MethodName); |
| | | 64 | | } |
| | 87 | 65 | | } |
| | | 66 | | |
| | | 67 | | private Uri BuildMethodUri(string methodName) |
| | | 68 | | { |
| | 92 | 69 | | var baseUrl = _options.BaseUrl.TrimEnd('/'); |
| | 92 | 70 | | var environmentPath = _options.Environment == TelegramBotApiEnvironment.Test |
| | 92 | 71 | | ? "/test" |
| | 92 | 72 | | : string.Empty; |
| | | 73 | | |
| | 92 | 74 | | return new Uri($"{baseUrl}/bot{_options.Token}{environmentPath}/{methodName}", UriKind.Absolute); |
| | | 75 | | } |
| | | 76 | | } |