< Summary

Information
Class: Orchestrator.Infrastructure.Factories.KicktippClientFactory
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Infrastructure/Factories/KicktippClientFactory.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 52
Coverable lines: 52
Total lines: 114
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%620%
CreateClient()100%210%
CreateAuthenticatedHttpClient()100%210%
CreateSnapshotClient()100%210%
LoadCredentials()0%2040%
InitializeClient()100%210%

File(s)

/home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Infrastructure/Factories/KicktippClientFactory.cs

#LineLine coverage
 1using KicktippIntegration;
 2using KicktippIntegration.Authentication;
 3using Microsoft.Extensions.Caching.Memory;
 4using Microsoft.Extensions.Logging;
 5using Microsoft.Extensions.Options;
 6using Orchestrator.Commands.Utility.Snapshots;
 7
 8namespace 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>
 16public 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
 023    public KicktippClientFactory(
 024        IMemoryCache memoryCache,
 025        ILoggerFactory loggerFactory)
 26    {
 027        _memoryCache = memoryCache;
 028        _loggerFactory = loggerFactory;
 029        _credentials = new Lazy<KicktippOptions>(LoadCredentials);
 030        _client = new Lazy<IKicktippClient>(InitializeClient);
 031    }
 32
 33    /// <inheritdoc />
 034    public IKicktippClient CreateClient() => _client.Value;
 35
 36    /// <inheritdoc />
 37    public HttpClient CreateAuthenticatedHttpClient()
 38    {
 039        var options = Options.Create(_credentials.Value);
 40
 41        // Create the authentication handler
 042        var authLogger = _loggerFactory.CreateLogger<KicktippAuthenticationHandler>();
 043        var authHandler = new KicktippAuthenticationHandler(options, authLogger)
 044        {
 045            InnerHandler = new HttpClientHandler()
 046        };
 47
 48        // Create HttpClient with the auth handler
 049        var httpClient = new HttpClient(authHandler)
 050        {
 051            BaseAddress = new Uri("https://www.kicktipp.de"),
 052            Timeout = TimeSpan.FromMinutes(2)
 053        };
 054        httpClient.DefaultRequestHeaders.Add("User-Agent",
 055            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safar
 56
 057        return httpClient;
 58    }
 59
 60    /// <inheritdoc />
 61    public SnapshotClient CreateSnapshotClient()
 62    {
 063        var httpClient = CreateAuthenticatedHttpClient();
 064        var logger = _loggerFactory.CreateLogger<SnapshotClient>();
 065        return new SnapshotClient(httpClient, logger);
 66    }
 67
 68    private static KicktippOptions LoadCredentials()
 69    {
 070        var username = Environment.GetEnvironmentVariable("KICKTIPP_USERNAME");
 071        var password = Environment.GetEnvironmentVariable("KICKTIPP_PASSWORD");
 72
 073        if (string.IsNullOrWhiteSpace(username))
 74        {
 075            throw new InvalidOperationException("KICKTIPP_USERNAME environment variable is required");
 76        }
 77
 078        if (string.IsNullOrWhiteSpace(password))
 79        {
 080            throw new InvalidOperationException("KICKTIPP_PASSWORD environment variable is required");
 81        }
 82
 083        return new KicktippOptions
 084        {
 085            Username = username,
 086            Password = password
 087        };
 88    }
 89
 90    private IKicktippClient InitializeClient()
 91    {
 092        var options = Options.Create(_credentials.Value);
 93
 94        // Create the authentication handler
 095        var authLogger = _loggerFactory.CreateLogger<KicktippAuthenticationHandler>();
 096        var authHandler = new KicktippAuthenticationHandler(options, authLogger)
 097        {
 098            InnerHandler = new HttpClientHandler()
 099        };
 100
 101        // Create HttpClient with the auth handler
 0102        var httpClient = new HttpClient(authHandler)
 0103        {
 0104            BaseAddress = new Uri("https://www.kicktipp.de"),
 0105            Timeout = TimeSpan.FromMinutes(2)
 0106        };
 0107        httpClient.DefaultRequestHeaders.Add("User-Agent",
 0108            "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
 0111        var clientLogger = _loggerFactory.CreateLogger<KicktippClient>();
 0112        return new KicktippClient(httpClient, clientLogger, _memoryCache);
 113    }
 114}