Skip to content

Commit

Permalink
Update options config
Browse files Browse the repository at this point in the history
  • Loading branch information
ductran95 committed Aug 6, 2022
1 parent b6592d6 commit 791a075
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 9 additions & 11 deletions src/JustCSharp.ShopeeSDK/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddShopeeSDK(this IServiceCollection services,
IConfigurationSection configurationSection, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
{
services.AddShopeeSDKCore(serviceLifetime);

services.Configure<ShopeeSDKOptions>(configurationSection);
services.AddShopeeSDK(configurationSection.Bind, serviceLifetime);

return services;
}

public static IServiceCollection AddShopeeSDK(this IServiceCollection services, Action<ShopeeSDKOptions> setupAction, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
public static IServiceCollection AddShopeeSDK(this IServiceCollection services, Action<ShopeeSDKOptions> configure = null, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
{
services.AddShopeeSDKCore(serviceLifetime);
var options = new ShopeeSDKOptions();

services.Configure(setupAction);
if (configure != null)
{
configure.Invoke(options);
}

services.TryAddSingleton(options);

return services;
}

internal static IServiceCollection AddShopeeSDKCore(this IServiceCollection services, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
{
services.TryAdd(new ServiceDescriptor(typeof(IShopeeClient), typeof(ShopeeClient), serviceLifetime));

return services;
Expand Down
4 changes: 2 additions & 2 deletions src/JustCSharp.ShopeeSDK/ShopeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class ShopeeClient : IShopeeClient, IDisposable
protected readonly ILogger _logger;
protected readonly ShopeeSDKOptions _shopeeSdkOptions;

public ShopeeClient(IOptions<ShopeeSDKOptions> shopeeOptions, ILogger<ShopeeClient> logger)
public ShopeeClient(ShopeeSDKOptions shopeeSdkOptions, ILogger<ShopeeClient> logger)
{
_httpClient = new RestClient(new RestClientOptions()).UseSystemTextJson();
_logger = logger;
_shopeeSdkOptions = shopeeOptions.Value;
_shopeeSdkOptions = shopeeSdkOptions;
}

public T Execute<T>(IShopeeRequest<T> request) where T : IShopeeResponse
Expand Down

0 comments on commit 791a075

Please sign in to comment.