Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Localazy/LocalazyInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static IServiceCollection AddLocalazySdk(this IServiceCollection services
})
.AddHttpClient<HttpWrapper>(c => c.BaseAddress = new Uri("https://api.localazy.com"))
.Services
.AddScoped<ILocalazyService, LocalazyService>();
.AddScoped<ILocalazyService, LocalazyService>()
.AddScoped<ILocalazyFactory, LocalazyFactory>();

return services;
}
Expand Down
6 changes: 6 additions & 0 deletions Localazy/Service/ILocalazyFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Localazy.Service;

public interface ILocalazyFactory
{
ILocalazyService CreateService(string apiKey);
}
25 changes: 25 additions & 0 deletions Localazy/Service/LocalazyFactory.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 18 additions & 0 deletions Tests/FactoryTests.cs
Original file line number Diff line number Diff line change
@@ -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<ILocalazyFactory>();
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");
}
}
4 changes: 2 additions & 2 deletions Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down