Skip to content

Commit

Permalink
moved Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
tl-Roberto-Mancinelli committed Dec 5, 2024
1 parent a572938 commit 4343239
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
44 changes: 0 additions & 44 deletions src/TrueLayer/TrueLayerClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.Extensions.Options;
using TrueLayer.Auth;
using TrueLayer.MerchantAccounts;
using TrueLayer.Payments;
Expand All @@ -9,49 +8,6 @@

namespace TrueLayer
{
internal class TrueLayerClientFactory
{
private readonly IApiClient _apiClient;
private readonly IOptions<TrueLayerOptions> _options;
private readonly IAuthTokenCache _authTokenCache;

public TrueLayerClientFactory(IApiClient apiClient, IOptions<TrueLayerOptions> options, IAuthTokenCache authTokenCache)
{
apiClient.NotNull(nameof(apiClient));
options.NotNull(nameof(options));

_apiClient = apiClient;
_options = options;
_authTokenCache = authTokenCache;
}

public ITrueLayerClient Create()
{
var auth = new AuthApi(_apiClient, _options.Value);

return new TrueLayerClient(
auth,
new Lazy<IPaymentsApi>(() => new PaymentsApi(_apiClient, auth, _options.Value)),
new Lazy<IPaymentsProvidersApi>(() => new PaymentsProvidersApi(_apiClient, auth, _options.Value)),
new Lazy<IPayoutsApi>(() => new PayoutsApi(_apiClient, auth, _options.Value)),
new Lazy<IMerchantAccountsApi>(() => new MerchantAccountsApi(_apiClient, auth, _options.Value)),
new Lazy<IMandatesApi>(() => new MandatesApi(_apiClient, auth, _options.Value)));
}

public ITrueLayerClient CreateWithCache()
{
var auth = new AuthApiCacheDecorator(new AuthApi(_apiClient, _options.Value), _authTokenCache);

return new TrueLayerClient(
auth,
new Lazy<IPaymentsApi>(() => new PaymentsApi(_apiClient, auth, _options.Value)),
new Lazy<IPaymentsProvidersApi>(() => new PaymentsProvidersApi(_apiClient, auth, _options.Value)),
new Lazy<IPayoutsApi>(() => new PayoutsApi(_apiClient, auth, _options.Value)),
new Lazy<IMerchantAccountsApi>(() => new MerchantAccountsApi(_apiClient, auth, _options.Value)),
new Lazy<IMandatesApi>(() => new MandatesApi(_apiClient, auth, _options.Value)));
}
}

internal class TrueLayerClient : ITrueLayerClient
{
// APIs that require specific configuration should be lazily initialised
Expand Down
54 changes: 54 additions & 0 deletions src/TrueLayer/TrueLayerClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Microsoft.Extensions.Options;
using TrueLayer.Auth;
using TrueLayer.Mandates;
using TrueLayer.MerchantAccounts;
using TrueLayer.Payments;
using TrueLayer.PaymentsProviders;
using TrueLayer.Payouts;

namespace TrueLayer
{
internal class TrueLayerClientFactory
{
private readonly IApiClient _apiClient;
private readonly IOptions<TrueLayerOptions> _options;
private readonly IAuthTokenCache _authTokenCache;

public TrueLayerClientFactory(IApiClient apiClient, IOptions<TrueLayerOptions> options, IAuthTokenCache authTokenCache)
{
apiClient.NotNull(nameof(apiClient));
options.NotNull(nameof(options));

_apiClient = apiClient;
_options = options;
_authTokenCache = authTokenCache;
}

public ITrueLayerClient Create()
{
var auth = new AuthApi(_apiClient, _options.Value);

return new TrueLayerClient(
auth,
new Lazy<IPaymentsApi>(() => new PaymentsApi(_apiClient, auth, _options.Value)),
new Lazy<IPaymentsProvidersApi>(() => new PaymentsProvidersApi(_apiClient, auth, _options.Value)),
new Lazy<IPayoutsApi>(() => new PayoutsApi(_apiClient, auth, _options.Value)),
new Lazy<IMerchantAccountsApi>(() => new MerchantAccountsApi(_apiClient, auth, _options.Value)),
new Lazy<IMandatesApi>(() => new MandatesApi(_apiClient, auth, _options.Value)));
}

public ITrueLayerClient CreateWithCache()
{
var auth = new AuthApiCacheDecorator(new AuthApi(_apiClient, _options.Value), _authTokenCache);

return new TrueLayerClient(
auth,
new Lazy<IPaymentsApi>(() => new PaymentsApi(_apiClient, auth, _options.Value)),
new Lazy<IPaymentsProvidersApi>(() => new PaymentsProvidersApi(_apiClient, auth, _options.Value)),
new Lazy<IPayoutsApi>(() => new PayoutsApi(_apiClient, auth, _options.Value)),
new Lazy<IMerchantAccountsApi>(() => new MerchantAccountsApi(_apiClient, auth, _options.Value)),
new Lazy<IMandatesApi>(() => new MandatesApi(_apiClient, auth, _options.Value)));
}
}
}

0 comments on commit 4343239

Please sign in to comment.