| | | 1 | | using KicktippIntegration; |
| | | 2 | | using KicktippIntegration.Authentication; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | using Orchestrator.Commands.Utility.Snapshots; |
| | | 7 | | |
| | | 8 | | namespace Orchestrator.Infrastructure.Factories; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Default implementation of <see cref="IKicktippClientFactory"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Reads credentials from KICKTIPP_USERNAME and KICKTIPP_PASSWORD environment variables. |
| | | 15 | | /// </remarks> |
| | | 16 | | public sealed class KicktippClientFactory : IKicktippClientFactory |
| | | 17 | | { |
| | | 18 | | private readonly IMemoryCache _memoryCache; |
| | | 19 | | private readonly ILoggerFactory _loggerFactory; |
| | | 20 | | private readonly Lazy<IKicktippClient> _client; |
| | | 21 | | private readonly Lazy<KicktippOptions> _credentials; |
| | | 22 | | |
| | 0 | 23 | | public KicktippClientFactory( |
| | 0 | 24 | | IMemoryCache memoryCache, |
| | 0 | 25 | | ILoggerFactory loggerFactory) |
| | | 26 | | { |
| | 0 | 27 | | _memoryCache = memoryCache; |
| | 0 | 28 | | _loggerFactory = loggerFactory; |
| | 0 | 29 | | _credentials = new Lazy<KicktippOptions>(LoadCredentials); |
| | 0 | 30 | | _client = new Lazy<IKicktippClient>(InitializeClient); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | 0 | 34 | | public IKicktippClient CreateClient() => _client.Value; |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public HttpClient CreateAuthenticatedHttpClient() |
| | | 38 | | { |
| | 0 | 39 | | var options = Options.Create(_credentials.Value); |
| | | 40 | | |
| | | 41 | | // Create the authentication handler |
| | 0 | 42 | | var authLogger = _loggerFactory.CreateLogger<KicktippAuthenticationHandler>(); |
| | 0 | 43 | | var authHandler = new KicktippAuthenticationHandler(options, authLogger) |
| | 0 | 44 | | { |
| | 0 | 45 | | InnerHandler = new HttpClientHandler() |
| | 0 | 46 | | }; |
| | | 47 | | |
| | | 48 | | // Create HttpClient with the auth handler |
| | 0 | 49 | | var httpClient = new HttpClient(authHandler) |
| | 0 | 50 | | { |
| | 0 | 51 | | BaseAddress = new Uri("https://www.kicktipp.de"), |
| | 0 | 52 | | Timeout = TimeSpan.FromMinutes(2) |
| | 0 | 53 | | }; |
| | 0 | 54 | | httpClient.DefaultRequestHeaders.Add("User-Agent", |
| | 0 | 55 | | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safar |
| | | 56 | | |
| | 0 | 57 | | return httpClient; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public SnapshotClient CreateSnapshotClient() |
| | | 62 | | { |
| | 0 | 63 | | var httpClient = CreateAuthenticatedHttpClient(); |
| | 0 | 64 | | var logger = _loggerFactory.CreateLogger<SnapshotClient>(); |
| | 0 | 65 | | return new SnapshotClient(httpClient, logger); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private static KicktippOptions LoadCredentials() |
| | | 69 | | { |
| | 0 | 70 | | var username = Environment.GetEnvironmentVariable("KICKTIPP_USERNAME"); |
| | 0 | 71 | | var password = Environment.GetEnvironmentVariable("KICKTIPP_PASSWORD"); |
| | | 72 | | |
| | 0 | 73 | | if (string.IsNullOrWhiteSpace(username)) |
| | | 74 | | { |
| | 0 | 75 | | throw new InvalidOperationException("KICKTIPP_USERNAME environment variable is required"); |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | if (string.IsNullOrWhiteSpace(password)) |
| | | 79 | | { |
| | 0 | 80 | | throw new InvalidOperationException("KICKTIPP_PASSWORD environment variable is required"); |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | return new KicktippOptions |
| | 0 | 84 | | { |
| | 0 | 85 | | Username = username, |
| | 0 | 86 | | Password = password |
| | 0 | 87 | | }; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private IKicktippClient InitializeClient() |
| | | 91 | | { |
| | 0 | 92 | | var options = Options.Create(_credentials.Value); |
| | | 93 | | |
| | | 94 | | // Create the authentication handler |
| | 0 | 95 | | var authLogger = _loggerFactory.CreateLogger<KicktippAuthenticationHandler>(); |
| | 0 | 96 | | var authHandler = new KicktippAuthenticationHandler(options, authLogger) |
| | 0 | 97 | | { |
| | 0 | 98 | | InnerHandler = new HttpClientHandler() |
| | 0 | 99 | | }; |
| | | 100 | | |
| | | 101 | | // Create HttpClient with the auth handler |
| | 0 | 102 | | var httpClient = new HttpClient(authHandler) |
| | 0 | 103 | | { |
| | 0 | 104 | | BaseAddress = new Uri("https://www.kicktipp.de"), |
| | 0 | 105 | | Timeout = TimeSpan.FromMinutes(2) |
| | 0 | 106 | | }; |
| | 0 | 107 | | httpClient.DefaultRequestHeaders.Add("User-Agent", |
| | 0 | 108 | | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safar |
| | | 109 | | |
| | | 110 | | // Create and return the client |
| | 0 | 111 | | var clientLogger = _loggerFactory.CreateLogger<KicktippClient>(); |
| | 0 | 112 | | return new KicktippClient(httpClient, clientLogger, _memoryCache); |
| | | 113 | | } |
| | | 114 | | } |