< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
CreateClient()100%11100%
CreateAuthenticatedHttpClient()100%11100%
CreateSnapshotClient()100%11100%
LoadCredentials()75%4490.91%
InitializeClient()100%11100%

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
 123    public KicktippClientFactory(
 124        IMemoryCache memoryCache,
 125        ILoggerFactory loggerFactory)
 26    {
 127        _memoryCache = memoryCache;
 128        _loggerFactory = loggerFactory;
 129        _credentials = new Lazy<KicktippOptions>(LoadCredentials);
 130        _client = new Lazy<IKicktippClient>(InitializeClient);
 131    }
 32
 33    /// <inheritdoc />
 134    public IKicktippClient CreateClient() => _client.Value;
 35
 36    /// <inheritdoc />
 37    public HttpClient CreateAuthenticatedHttpClient()
 38    {
 139        var options = Options.Create(_credentials.Value);
 40
 41        // Create the authentication handler
 142        var authLogger = _loggerFactory.CreateLogger<KicktippAuthenticationHandler>();
 143        var authHandler = new KicktippAuthenticationHandler(options, authLogger)
 144        {
 145            InnerHandler = new HttpClientHandler()
 146        };
 47
 48        // Create HttpClient with the auth handler
 149        var httpClient = new HttpClient(authHandler)
 150        {
 151            BaseAddress = new Uri("https://www.kicktipp.de"),
 152            Timeout = TimeSpan.FromMinutes(2)
 153        };
 154        httpClient.DefaultRequestHeaders.Add("User-Agent",
 155            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safar
 56
 157        return httpClient;
 58    }
 59
 60    /// <inheritdoc />
 61    public ISnapshotClient CreateSnapshotClient()
 62    {
 163        var httpClient = CreateAuthenticatedHttpClient();
 164        var logger = _loggerFactory.CreateLogger<SnapshotClient>();
 165        return new SnapshotClient(httpClient, logger);
 66    }
 67
 68    private static KicktippOptions LoadCredentials()
 69    {
 170        var username = Environment.GetEnvironmentVariable("KICKTIPP_USERNAME");
 171        var password = Environment.GetEnvironmentVariable("KICKTIPP_PASSWORD");
 72
 173        if (string.IsNullOrWhiteSpace(username))
 74        {
 175            throw new InvalidOperationException("KICKTIPP_USERNAME environment variable is required");
 76        }
 77
 178        if (string.IsNullOrWhiteSpace(password))
 79        {
 080            throw new InvalidOperationException("KICKTIPP_PASSWORD environment variable is required");
 81        }
 82
 183        return new KicktippOptions
 184        {
 185            Username = username,
 186            Password = password
 187        };
 88    }
 89
 90    private IKicktippClient InitializeClient()
 91    {
 192        var options = Options.Create(_credentials.Value);
 93
 94        // Create the authentication handler
 195        var authLogger = _loggerFactory.CreateLogger<KicktippAuthenticationHandler>();
 196        var authHandler = new KicktippAuthenticationHandler(options, authLogger)
 197        {
 198            InnerHandler = new HttpClientHandler()
 199        };
 100
 101        // Create HttpClient with the auth handler
 1102        var httpClient = new HttpClient(authHandler)
 1103        {
 1104            BaseAddress = new Uri("https://www.kicktipp.de"),
 1105            Timeout = TimeSpan.FromMinutes(2)
 1106        };
 1107        httpClient.DefaultRequestHeaders.Add("User-Agent",
 1108            "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
 1111        var clientLogger = _loggerFactory.CreateLogger<KicktippClient>();
 1112        return new KicktippClient(httpClient, clientLogger, _memoryCache);
 113    }
 114}