| | | 1 | | using Chronicis.Client.Services; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Client.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extension methods for configuring HTTP clients. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class HttpClientServiceExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Adds all Chronicis HTTP clients including authenticated API client, |
| | | 12 | | /// public API client, and external service clients. |
| | | 13 | | /// </summary> |
| | | 14 | | public static IServiceCollection AddChronicisHttpClients( |
| | | 15 | | this IServiceCollection services, |
| | | 16 | | IConfiguration configuration) |
| | | 17 | | { |
| | 0 | 18 | | var apiBaseUrl = configuration["ApiBaseUrl"] ?? "http://localhost:7071"; |
| | | 19 | | |
| | | 20 | | // Register the auth handler |
| | 0 | 21 | | services.AddScoped<ChronicisAuthHandler>(); |
| | | 22 | | |
| | | 23 | | // Chronicis API client (with auth) |
| | 0 | 24 | | services.AddHttpClient("ChronicisApi", client => |
| | 0 | 25 | | { |
| | 0 | 26 | | client.BaseAddress = new Uri(apiBaseUrl); |
| | 0 | 27 | | }) |
| | 0 | 28 | | .AddHttpMessageHandler<ChronicisAuthHandler>() |
| | 0 | 29 | | .RemoveAllLoggers(); |
| | | 30 | | |
| | | 31 | | // Public API client (no auth) |
| | 0 | 32 | | services.AddHttpClient("ChronicisPublicApi", client => |
| | 0 | 33 | | { |
| | 0 | 34 | | client.BaseAddress = new Uri(apiBaseUrl); |
| | 0 | 35 | | }).RemoveAllLoggers(); |
| | | 36 | | |
| | | 37 | | // Quote service (external API) |
| | 0 | 38 | | services.AddHttpClient<IQuoteService, QuoteService>(client => |
| | 0 | 39 | | { |
| | 0 | 40 | | client.BaseAddress = new Uri("https://api.quotable.io/"); |
| | 0 | 41 | | client.Timeout = TimeSpan.FromSeconds(10); |
| | 0 | 42 | | }).RemoveAllLoggers(); |
| | | 43 | | |
| | 0 | 44 | | return services; |
| | | 45 | | } |
| | | 46 | | } |