Skip to content

Commit

Permalink
feat: Add Trybe HTTP clients infra
Browse files Browse the repository at this point in the history
  • Loading branch information
Antaris committed Feb 9, 2024
1 parent 184c2a5 commit 35277d7
Show file tree
Hide file tree
Showing 18 changed files with 960 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# License header
file_header_template = Copyright (c) 2023 Ingenium Software Engineering Limited
file_header_template = This work is licensed under the terms of the MIT license.\nFor a copy, see <https://opensource.org/licenses/MIT>.
csharp_style_namespace_declarations = file_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
Expand Down
4 changes: 4 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<PublishReposityUrl>true</PublishReposityUrl>
<EmbedUntractSources>true</EmbedUntractSources>
</PropertyGroup>

<ItemGroup Label="Sources">
<Compile Include="$(MSBuildThisFileDirectory)build\src\**\*.cs" />
</ItemGroup>
</Project>
11 changes: 9 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
<PackageVersion Include="BenchmarkDotNet" Version="0.13.7" />
<PackageVersion Include="FluentValidation" Version="11.6.0" />
<PackageVersion Include="MinVer" Version="4.3.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="xunit" Version="2.6.5" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
<PackageVersion Include="System.Net.Http.Json" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.0" />
<PackageVersion Include="xunit" Version="2.6.5" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions TrybeSDK.sln
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
.github\workflows\release.yml = .github\workflows\release.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrybeSDK.Extensions.DependencyInjection", "libs\TrybeSDK.Extensions.DependencyInjection\TrybeSDK.Extensions.DependencyInjection.csproj", "{058204F4-0B67-42B4-9FEF-9AE2BCC8E7F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -70,6 +72,10 @@ Global
{F45BA32C-F402-4300-BA56-E19F30EA6FB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F45BA32C-F402-4300-BA56-E19F30EA6FB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F45BA32C-F402-4300-BA56-E19F30EA6FB7}.Release|Any CPU.Build.0 = Release|Any CPU
{058204F4-0B67-42B4-9FEF-9AE2BCC8E7F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{058204F4-0B67-42B4-9FEF-9AE2BCC8E7F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{058204F4-0B67-42B4-9FEF-9AE2BCC8E7F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{058204F4-0B67-42B4-9FEF-9AE2BCC8E7F6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +86,7 @@ Global
{F45BA32C-F402-4300-BA56-E19F30EA6FB7} = {F3DB67FB-0AED-46C2-9F74-A9C197443E68}
{B55E88E9-F535-4649-A4E4-CE8495D4DDC6} = {F3DB67FB-0AED-46C2-9F74-A9C197443E68}
{9E95ECB2-C721-4367-B515-25C735D6573B} = {71A838D8-527D-41D1-99B0-611404787F46}
{058204F4-0B67-42B4-9FEF-9AE2BCC8E7F6} = {3D0CADC5-4B71-455C-9CD0-BDB0B3A74B3F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {26041282-B1C0-4262-AE2F-DA9CF2480C37}
Expand Down
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);
});
}
}
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>
Loading

0 comments on commit 35277d7

Please sign in to comment.