-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
960 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
libs/TrybeSDK.Extensions.DependencyInjection/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// This work is licensed under the terms of the MIT license. | ||
// For a copy, see <https://opensource.org/licenses/MIT>. | ||
|
||
using System.Net.Http.Headers; | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
using TrybeSDK; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Provides extensions for the <see cref="IServiceCollection"/> type. | ||
/// </summary> | ||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds Sailthru services to the given services collection. | ||
/// </summary> | ||
/// <param name="services">The services collection.</param> | ||
/// <param name="configure">The configure delegate.</param> | ||
/// <returns>The services collection.</returns> | ||
public static IServiceCollection AddTrybe( | ||
this IServiceCollection services, | ||
Action<TrybeSettings> configure) | ||
{ | ||
Ensure.IsNotNull(services, nameof(services)); | ||
Ensure.IsNotNull(configure, nameof(configure)); | ||
|
||
services.Configure(configure); | ||
|
||
AddCoreServices(services); | ||
|
||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Adds Sailthru services to the given services collection. | ||
/// </summary> | ||
/// <param name="services">The services collection.</param> | ||
/// <param name="settings">The Sailthru settings.</param> | ||
/// <returns>The services collection.</returns> | ||
public static IServiceCollection AddTrybe( | ||
this IServiceCollection services, | ||
TrybeSettings settings) | ||
{ | ||
Ensure.IsNotNull(services, nameof(services)); | ||
Ensure.IsNotNull(settings, nameof(settings)); | ||
|
||
services.AddSingleton(settings.AsOptions()); | ||
|
||
AddCoreServices(services); | ||
|
||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Adds Sailthru services to the given services collection. | ||
/// </summary> | ||
/// <param name="services">The services collection.</param> | ||
/// <param name="configuration">The configuration.</param> | ||
/// <returns>The services collection.</returns> | ||
public static IServiceCollection AddTrybe( | ||
this IServiceCollection services, | ||
IConfiguration configuration) | ||
{ | ||
Ensure.IsNotNull(services, nameof(services)); | ||
Ensure.IsNotNull(configuration, nameof(configuration)); | ||
|
||
services.Configure<TrybeSettings>( | ||
configuration.GetSection(TrybeSettings.ConfigurationSection)); | ||
|
||
AddCoreServices(services); | ||
|
||
return services; | ||
} | ||
|
||
static void AddCoreServices(IServiceCollection services) | ||
{ | ||
services.AddSingleton(sp => | ||
{ | ||
var settings = sp.GetRequiredService<IOptions<TrybeSettings>>().Value; | ||
settings.Validate(); | ||
return settings; | ||
}); | ||
|
||
services.AddScoped<ITrybeClientFactory, TrybeClientFactory>(); | ||
AddApiClient( | ||
services, | ||
TrybeApiConstants.DefaultTrybeApiClient, | ||
(cf, settings) => cf.CreateApiClient(settings, TrybeApiConstants.DefaultTrybeApiClient)); | ||
AddApiClient( | ||
services, | ||
TrybeApiConstants.DefaultTrybeShopClient, | ||
(cf, settings) => cf.CreateShopClient(settings, TrybeApiConstants.DefaultTrybeShopClient)); | ||
} | ||
|
||
static void AddApiClient<TClient>( | ||
IServiceCollection services, | ||
string name, | ||
Func<ITrybeClientFactory, TrybeSettings, TClient> factory) | ||
where TClient: class | ||
{ | ||
void ConfigureHttpDefaults(HttpClient http) | ||
{ | ||
http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
} | ||
|
||
services.AddHttpClient(name, ConfigureHttpDefaults); | ||
|
||
services.AddScoped(sp => | ||
{ | ||
var settings = sp.GetRequiredService<TrybeSettings>(); | ||
var clientFactory = sp.GetRequiredService<ITrybeClientFactory>(); | ||
return factory(clientFactory, settings); | ||
}); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
libs/TrybeSDK.Extensions.DependencyInjection/TrybeSDK.Extensions.DependencyInjection.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>latest</LangVersion> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>Microsoft.Extensions.DependencyInjection</RootNamespace> | ||
<ImplicitPackageReferences>false</ImplicitPackageReferences> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentValidation" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" /> | ||
<PackageReference Include="Microsoft.Extensions.Options" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" /> | ||
<PackageReference Include="Microsoft.Extensions.Http" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TrybeSDK\TrybeSDK.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.