Skip to content

Commit

Permalink
feat: Introduce TrybeHttpClientFactory for HTTP client creation inter…
Browse files Browse the repository at this point in the history
…ception
  • Loading branch information
Antaris committed Feb 15, 2024
1 parent fc520dc commit 74c97ef
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,22 @@ static void AddCoreServices(IServiceCollection services)
return settings;
});

services.AddScoped<ITrybeClientFactory, TrybeClientFactory>();
services.AddScoped<ITrybeHttpClientFactory, TrybeHttpClientFactory>();
services.AddScoped<ITrybeApiClientFactory, TrybeApiClientFactory>();
AddApiClient(
services,
TrybeApiConstants.DefaultTrybeApiClient,
(cf, settings) => cf.CreateApiClient(settings, TrybeApiConstants.DefaultTrybeApiClient));
AddApiClient(
services,
TrybeApiConstants.DefaultTrybeShopClient,
(cf, settings) => cf.CreateShopClient(settings, TrybeApiConstants.DefaultTrybeShopClient));
(cf, settings) => cf.CreateFrontendClient(settings, TrybeApiConstants.DefaultTrybeShopClient));
}

static void AddApiClient<TClient>(
IServiceCollection services,
string name,
Func<ITrybeClientFactory, TrybeSettings, TClient> factory)
Func<ITrybeApiClientFactory, TrybeSettings, TClient> factory)
where TClient : class
{
void ConfigureHttpDefaults(HttpClient http)
Expand All @@ -114,7 +115,7 @@ void ConfigureHttpDefaults(HttpClient http)
services.AddScoped(sp =>
{
var settings = sp.GetRequiredService<TrybeSettings>();
var clientFactory = sp.GetRequiredService<ITrybeClientFactory>();
var clientFactory = sp.GetRequiredService<ITrybeApiClientFactory>();
return factory(clientFactory, settings);
});
Expand Down
33 changes: 0 additions & 33 deletions libs/TrybeSDK/ITrybeClientFactory.cs

This file was deleted.

33 changes: 33 additions & 0 deletions libs/TrybeSDK/TrybeApiClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

using TrybeSDK.Api;
using TrybeSDK.Frontend;

namespace TrybeSDK;

public interface ITrybeApiClientFactory
{
ITrybeApiClient CreateApiClient(TrybeSettings settings, string name = TrybeApiConstants.DefaultTrybeApiClient);

ITrybeFrontendClient CreateFrontendClient(TrybeSettings settings, string name = TrybeApiConstants.DefaultTrybeShopClient);
}

/// <summary>
/// Provides factory services for creating Trybe client instances.
/// </summary>
public class TrybeApiClientFactory : ITrybeApiClientFactory
{
readonly ITrybeHttpClientFactory _httpClientFactory;

public TrybeApiClientFactory(ITrybeHttpClientFactory httpClientFactory)
{
_httpClientFactory = Ensure.IsNotNull(httpClientFactory, nameof(httpClientFactory));
}

public ITrybeApiClient CreateApiClient(TrybeSettings settings, string name = TrybeApiConstants.DefaultTrybeApiClient)
=> new TrybeApiClient(_httpClientFactory.CreateHttpClient(name), settings);

public ITrybeFrontendClient CreateFrontendClient(TrybeSettings settings, string name = TrybeApiConstants.DefaultTrybeShopClient)
=> new TrybeFrontendClient(_httpClientFactory.CreateHttpClient(name), settings);
}
23 changes: 23 additions & 0 deletions libs/TrybeSDK/TrybeHttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.

namespace TrybeSDK;

/// <summary>
/// Provides factory methods for creating a HTTP client.
/// </summary>
public interface ITrybeHttpClientFactory
{
/// <summary>
/// Creates a HTTP client.
/// </summary>
/// <param name="name">The HTTP client name.</param>
/// <returns>The HTTP client.</returns>
HttpClient CreateHttpClient(string name);
}

public class TrybeHttpClientFactory(IHttpClientFactory clientFactory) : ITrybeHttpClientFactory
{
public HttpClient CreateHttpClient(string name)
=> Ensure.IsNotNull(clientFactory, nameof(clientFactory)).CreateClient(name);
}

0 comments on commit 74c97ef

Please sign in to comment.