diff --git a/Localazy/LocalazyInitializer.cs b/Localazy/LocalazyInitializer.cs index 85d2fc2..b1a2bfe 100644 --- a/Localazy/LocalazyInitializer.cs +++ b/Localazy/LocalazyInitializer.cs @@ -15,7 +15,8 @@ public static IServiceCollection AddLocalazySdk(this IServiceCollection services }) .AddHttpClient(c => c.BaseAddress = new Uri("https://api.localazy.com")) .Services - .AddScoped(); + .AddScoped() + .AddScoped(); return services; } diff --git a/Localazy/Service/ILocalazyFactory.cs b/Localazy/Service/ILocalazyFactory.cs new file mode 100644 index 0000000..16080c1 --- /dev/null +++ b/Localazy/Service/ILocalazyFactory.cs @@ -0,0 +1,6 @@ +namespace Localazy.Service; + +public interface ILocalazyFactory +{ + ILocalazyService CreateService(string apiKey); +} \ No newline at end of file diff --git a/Localazy/Service/LocalazyFactory.cs b/Localazy/Service/LocalazyFactory.cs new file mode 100644 index 0000000..7c12cd9 --- /dev/null +++ b/Localazy/Service/LocalazyFactory.cs @@ -0,0 +1,25 @@ +using Localazy.Model; + +namespace Localazy.Service; + +public class LocalazyFactory : ILocalazyFactory +{ + private readonly IHttpClientFactory _httpClientFactory; + + public LocalazyFactory(IHttpClientFactory httpClientFactory) + { + _httpClientFactory = httpClientFactory; + } + + public ILocalazyService CreateService(string apiKey) + { + var config = new LocalazyConfig + { + ApiKey = apiKey + }; + var client = _httpClientFactory.CreateClient(nameof(LocalazyFactory)); + client.BaseAddress = new Uri("https://api.localazy.com/"); + var httpWrapper = new HttpWrapper(client, config); + return new LocalazyService(httpWrapper); + } +} \ No newline at end of file diff --git a/Tests/FactoryTests.cs b/Tests/FactoryTests.cs new file mode 100644 index 0000000..ab5ecd0 --- /dev/null +++ b/Tests/FactoryTests.cs @@ -0,0 +1,18 @@ +using Localazy.Service; +using Microsoft.Extensions.DependencyInjection; + +namespace Tests; + +public class FactoryTests : TestBase +{ + [Test] + public async Task CanLoadProjects() + { + var factory = ServiceProvider.GetRequiredService(); + var apiKey = Environment.GetEnvironmentVariable("LOCALAZY_API_KEY"); + var service = factory.CreateService(apiKey!); + var projects = await service.ListProjects(); + + Assert.That(projects, Is.Not.Empty, "No projects found"); + } +} \ No newline at end of file diff --git a/Tests/TestBase.cs b/Tests/TestBase.cs index 00ca8d4..02a64c5 100644 --- a/Tests/TestBase.cs +++ b/Tests/TestBase.cs @@ -7,8 +7,8 @@ namespace Tests; public abstract class TestBase { - public ServiceProvider ServiceProvider { get; private set; } = null!; - public ILocalazyService LocalazyService { get; private set; } = null!; + protected ServiceProvider ServiceProvider { get; private set; } = null!; + protected ILocalazyService LocalazyService { get; private set; } = null!; [SetUp] public async Task Setup()