| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using KicktippIntegration.Authentication; |
| | | 4 | | |
| | | 5 | | namespace KicktippIntegration; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for configuring Kicktipp services in dependency injection |
| | | 9 | | /// </summary> |
| | | 10 | | public static class ServiceCollectionExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Adds Kicktipp client services to the service collection |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="services">The service collection</param> |
| | | 16 | | /// <returns>The service collection for chaining</returns> |
| | | 17 | | public static IServiceCollection AddKicktippClient(this IServiceCollection services) |
| | | 18 | | { |
| | | 19 | | // Register memory cache for caching Kicktipp data |
| | 1 | 20 | | services.AddMemoryCache(); |
| | | 21 | | |
| | | 22 | | // Register the authentication handler as singleton to share cookies across all clients |
| | 1 | 23 | | services.TryAddSingleton<KicktippAuthenticationHandler>(); |
| | | 24 | | |
| | | 25 | | // Register the HTTP client with authentication |
| | 1 | 26 | | services |
| | 1 | 27 | | .AddHttpClient<IKicktippClient, KicktippClient>(client => |
| | 1 | 28 | | { |
| | 0 | 29 | | client.BaseAddress = new Uri("https://www.kicktipp.de"); |
| | 1 | 30 | | // Set a reasonable timeout for web scraping operations |
| | 0 | 31 | | client.Timeout = TimeSpan.FromMinutes(2); |
| | 1 | 32 | | // Add headers to mimic a real browser |
| | 0 | 33 | | client.DefaultRequestHeaders.Add("User-Agent", |
| | 0 | 34 | | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.1 |
| | 0 | 35 | | }) |
| | 1 | 36 | | .AddHttpMessageHandler<KicktippAuthenticationHandler>(); |
| | | 37 | | |
| | 1 | 38 | | return services; |
| | | 39 | | } |
| | | 40 | | } |