| | | 1 | | using System.Text.Json; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 6 | | using TeleFlow.Telegram.Internal; |
| | | 7 | | using TeleFlow.Telegram.Internal.Options; |
| | | 8 | | |
| | | 9 | | namespace TeleFlow.Telegram; |
| | | 10 | | |
| | | 11 | | public static class TelegramClientServiceCollectionExtensions |
| | | 12 | | { |
| | | 13 | | public static IServiceCollection AddTelegramClient( |
| | | 14 | | this IServiceCollection services, |
| | | 15 | | Action<TelegramClientOptions> configure) |
| | | 16 | | { |
| | 484 | 17 | | ArgumentNullException.ThrowIfNull(services); |
| | 484 | 18 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 19 | | |
| | 484 | 20 | | EnsureNotRegistered<TelegramClientOptions>(services, nameof(AddTelegramClient)); |
| | | 21 | | |
| | 484 | 22 | | var options = new TelegramClientOptions(); |
| | 484 | 23 | | configure(options); |
| | 484 | 24 | | TelegramClientOptionsValidator.Validate(options); |
| | | 25 | | |
| | 474 | 26 | | services.AddSingleton(options); |
| | 474 | 27 | | AddTelegramClientDefaults(services); |
| | | 28 | | |
| | 474 | 29 | | return services; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public static IServiceCollection AddDeepLinkPayloadSerializer<TSerializer>( |
| | | 33 | | this IServiceCollection services) |
| | | 34 | | where TSerializer : class, IDeepLinkPayloadSerializer |
| | | 35 | | { |
| | 1 | 36 | | ArgumentNullException.ThrowIfNull(services); |
| | | 37 | | |
| | 1 | 38 | | services.RemoveAll<IDeepLinkPayloadSerializer>(); |
| | 1 | 39 | | services.AddSingleton<IDeepLinkPayloadSerializer, TSerializer>(); |
| | 1 | 40 | | return services; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public static IServiceCollection AddTelegramJsonOptions( |
| | | 44 | | this IServiceCollection services, |
| | | 45 | | JsonSerializerOptions options) |
| | | 46 | | { |
| | 0 | 47 | | ArgumentNullException.ThrowIfNull(services); |
| | 0 | 48 | | ArgumentNullException.ThrowIfNull(options); |
| | | 49 | | |
| | 0 | 50 | | services.RemoveAll<TelegramJsonOptions>(); |
| | 0 | 51 | | services.AddSingleton(new TelegramJsonOptions(options)); |
| | 0 | 52 | | return services; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public static IServiceCollection AddTelegramJsonOptions( |
| | | 56 | | this IServiceCollection services, |
| | | 57 | | Action<JsonSerializerOptions> configure) |
| | | 58 | | { |
| | 1 | 59 | | ArgumentNullException.ThrowIfNull(services); |
| | 1 | 60 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 61 | | |
| | 1 | 62 | | var options = TelegramJsonOptions.CreateDefault().SerializerOptions; |
| | 1 | 63 | | configure(options); |
| | | 64 | | |
| | 1 | 65 | | services.RemoveAll<TelegramJsonOptions>(); |
| | 1 | 66 | | services.AddSingleton(new TelegramJsonOptions(options)); |
| | 1 | 67 | | return services; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public static IServiceCollection AddTelegramHttpTransport( |
| | | 71 | | this IServiceCollection services, |
| | | 72 | | HttpClient httpClient) |
| | | 73 | | { |
| | 0 | 74 | | ArgumentNullException.ThrowIfNull(services); |
| | 0 | 75 | | ArgumentNullException.ThrowIfNull(httpClient); |
| | | 76 | | |
| | 0 | 77 | | services.RemoveAll<ITelegramTransport>(); |
| | 0 | 78 | | services.AddSingleton<ITelegramTransport>(_ => new HttpClientTelegramTransport(httpClient)); |
| | 0 | 79 | | return services; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public static IServiceCollection AddTelegramHttpTransport( |
| | | 83 | | this IServiceCollection services, |
| | | 84 | | Func<IServiceProvider, HttpClient> httpClientFactory) |
| | | 85 | | { |
| | 68 | 86 | | ArgumentNullException.ThrowIfNull(services); |
| | 68 | 87 | | ArgumentNullException.ThrowIfNull(httpClientFactory); |
| | | 88 | | |
| | 68 | 89 | | services.RemoveAll<ITelegramTransport>(); |
| | 68 | 90 | | services.AddSingleton<ITelegramTransport>(provider => |
| | 68 | 91 | | { |
| | 68 | 92 | | var httpClient = httpClientFactory(provider); |
| | 68 | 93 | | ArgumentNullException.ThrowIfNull(httpClient); |
| | 68 | 94 | | return HttpClientTelegramTransport.CreateOwned(httpClient); |
| | 68 | 95 | | }); |
| | 68 | 96 | | return services; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | public static IServiceCollection AddTelegramTransport<TTransport>( |
| | | 100 | | this IServiceCollection services) |
| | | 101 | | where TTransport : class, ITelegramTransport |
| | | 102 | | { |
| | 1 | 103 | | ArgumentNullException.ThrowIfNull(services); |
| | | 104 | | |
| | 1 | 105 | | services.RemoveAll<ITelegramTransport>(); |
| | 1 | 106 | | services.AddSingleton<ITelegramTransport, TTransport>(); |
| | 1 | 107 | | return services; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public static IServiceCollection AddTelegramHttpMessageHandler<THandler>( |
| | | 111 | | this IServiceCollection services) |
| | | 112 | | where THandler : HttpMessageHandler |
| | | 113 | | { |
| | 1 | 114 | | ArgumentNullException.ThrowIfNull(services); |
| | | 115 | | |
| | 1 | 116 | | services.RemoveAll<ITelegramTransport>(); |
| | 1 | 117 | | services.AddSingleton<ITelegramTransport>(provider => |
| | 2 | 118 | | HttpClientTelegramTransport.CreateOwned(new HttpClient( |
| | 2 | 119 | | provider.GetRequiredService<THandler>(), |
| | 2 | 120 | | disposeHandler: false))); |
| | 1 | 121 | | return services; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public static IServiceCollection AddTelegramClient<TClient>( |
| | | 125 | | this IServiceCollection services) |
| | | 126 | | where TClient : class, ITelegramClient |
| | | 127 | | { |
| | 1 | 128 | | ArgumentNullException.ThrowIfNull(services); |
| | | 129 | | |
| | 1 | 130 | | services.RemoveAll<ITelegramClient>(); |
| | 1 | 131 | | services.AddSingleton<ITelegramClient, TClient>(); |
| | 1 | 132 | | return services; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | public static IServiceCollection AddTelegramRequestExecutor<TExecutor>( |
| | | 136 | | this IServiceCollection services) |
| | | 137 | | where TExecutor : class, ITelegramRequestExecutor |
| | | 138 | | { |
| | 1 | 139 | | ArgumentNullException.ThrowIfNull(services); |
| | | 140 | | |
| | 1 | 141 | | services.RemoveAll<ITelegramRequestExecutor>(); |
| | 1 | 142 | | services.AddSingleton<ITelegramRequestExecutor, TExecutor>(); |
| | 1 | 143 | | return services; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | private static void AddTelegramClientDefaults(IServiceCollection services) |
| | | 147 | | { |
| | 474 | 148 | | services.TryAddSingleton(TelegramJsonOptions.CreateDefault()); |
| | 474 | 149 | | services.TryAddSingleton<IDeepLinkPayloadSerializer, Base64UrlJsonDeepLinkPayloadSerializer>(); |
| | 474 | 150 | | services.TryAddSingleton<TelegramDeepLinks>(static provider => |
| | 776 | 151 | | new TelegramDeepLinks( |
| | 776 | 152 | | provider.GetRequiredService<TelegramClientOptions>().BotUsername, |
| | 776 | 153 | | provider.GetRequiredService<IDeepLinkPayloadSerializer>())); |
| | 691 | 154 | | services.TryAddSingleton<ITelegramTransport>(static _ => HttpClientTelegramTransport.CreateOwned(new HttpClient( |
| | 474 | 155 | | services.TryAddSingleton(TimeProvider.System); |
| | 474 | 156 | | services.TryAddSingleton<ILoggerFactory>(NullLoggerFactory.Instance); |
| | 474 | 157 | | services.TryAddSingleton<ITelegramClient, TelegramClient>(); |
| | 474 | 158 | | services.TryAddSingleton<ITelegramRequestExecutor, TelegramRequestExecutor>(); |
| | 474 | 159 | | } |
| | | 160 | | |
| | | 161 | | private static void EnsureNotRegistered<TService>(IServiceCollection services, string apiName) |
| | | 162 | | { |
| | 1744 | 163 | | if (services.Any(descriptor => descriptor.ServiceType == typeof(TService))) |
| | | 164 | | { |
| | 0 | 165 | | throw new InvalidOperationException( |
| | 0 | 166 | | $"{apiName} cannot be called more than once for service '{typeof(TService).Name}'."); |
| | | 167 | | } |
| | 484 | 168 | | } |
| | | 169 | | } |