Skip to content

Commit

Permalink
refactor: changing the DI methods to allow for configuration of the h…
Browse files Browse the repository at this point in the history
…ttp client (#66)

* refactor: changing the DI methods to allow for configuration of the http client

* test: adding test cases for the dependency injection methods
  • Loading branch information
JustinCanton authored Apr 7, 2023
1 parent c6e4688 commit 5369e85
Show file tree
Hide file tree
Showing 40 changed files with 677 additions and 98 deletions.
7 changes: 3 additions & 4 deletions Directory.build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
<PackageReference Include="StyleCop.CSharp.Async.Rules" Version="6.1.41">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Text.Analyzers" Version="2.6.4">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Analyzers" Version="2.2.0">
<PackageReference Include="Text.Analyzers" Version="3.3.4">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions Geo.NET.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<Rules AnalyzerId="System.Diagnostics.CodeAnalysis" RuleNamespace="System.Diagnostics.CodeAnalysis">
<Rule Id="CA1014" Action="None"/>
<Rule Id="CA1704" Action="None"/>
<Rule Id="CA1805" Action="None"/>
<Rule Id="CA2208" Action="None"/>
</Rules>
Expand Down
20 changes: 15 additions & 5 deletions src/Geo.ArcGIS/DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Geo.ArcGIS.DependencyInjection
{
using System;
using System.Net.Http;
using Geo.ArcGIS.Abstractions;
using Geo.ArcGIS.Models;
using Geo.ArcGIS.Services;
Expand All @@ -26,10 +27,15 @@ public static class ServiceCollectionExtensions
/// </list>
/// </para>
/// </summary>
/// <param name="services">A <see cref="IServiceCollection"/> to add the ArcGIS services to.</param>
/// <param name="services">An <see cref="IServiceCollection"/> to add the ArcGIS services to.</param>
/// <param name="optionsBuilder">A <see cref="Action{ArcGISOptionsBuilder}"/> with the options to add to the ArcGIS configuration.</param>
/// <returns>A <see cref="IServiceCollection"/> with the added services.</returns>
public static IServiceCollection AddArcGISServices(this IServiceCollection services, Action<ArcGISOptionsBuilder> optionsBuilder)
/// <param name="configureClient">Optional. A delegate that is used to configure the <see cref="HttpClient"/>.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> to configure the http client.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception>
public static IHttpClientBuilder AddArcGISServices(
this IServiceCollection services,
Action<ArcGISOptionsBuilder> optionsBuilder,
Action<HttpClient> configureClient = null)
{
services.AddCoreServices();

Expand All @@ -47,9 +53,13 @@ public static IServiceCollection AddArcGISServices(this IServiceCollection servi

services.AddHttpClient<IArcGISTokenRetrevial, ArcGISTokenRetrevial>();
services.AddSingleton<IArcGISTokenContainer, ArcGISTokenContainer>();
services.AddHttpClient<IArcGISGeocoding, ArcGISGeocoding>();

return services;
return services.AddHttpClient<IArcGISGeocoding, ArcGISGeocoding>(configureClient ?? DefaultHttpClientConfiguration);
}

private static void DefaultHttpClientConfiguration(HttpClient client)
{
// No-op
}
}
}
2 changes: 1 addition & 1 deletion src/Geo.ArcGIS/Services/ArcGISGeocoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ArcGISGeocoding : ClientExecutor, IArcGISGeocoding
/// Initializes a new instance of the <see cref="ArcGISGeocoding"/> class.
/// </summary>
/// <param name="client">A <see cref="HttpClient"/> used for making calls to the ArcGIS system.</param>
/// <param name="tokenContainer">A <see cref="IArcGISTokenContainer"/> used for retreiving the ArcGIS token.</param>
/// <param name="tokenContainer">An <see cref="IArcGISTokenContainer"/> used for retreiving the ArcGIS token.</param>
/// <param name="exceptionProvider">An <see cref="IGeoNETExceptionProvider"/> used to provide exceptions based on an exception type.</param>
/// <param name="resourceStringProviderFactory">An <see cref="IGeoNETResourceStringProviderFactory"/> used to create a resource string provider for log or exception messages.</param>
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> used to create a logger used for logging information.</param>
Expand Down
2 changes: 1 addition & 1 deletion src/Geo.ArcGIS/Services/ArcGISTokenContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ArcGISTokenContainer : IArcGISTokenContainer
/// <summary>
/// Initializes a new instance of the <see cref="ArcGISTokenContainer"/> class.
/// </summary>
/// <param name="tokenRetrevial">A <see cref="IArcGISTokenRetrevial"/> used to retrieve the token if expired of none existant.</param>
/// <param name="tokenRetrevial">An <see cref="IArcGISTokenRetrevial"/> used to retrieve the token if expired of none existant.</param>
public ArcGISTokenContainer(IArcGISTokenRetrevial tokenRetrevial)
{
_tokenRetrevial = tokenRetrevial ?? throw new ArgumentNullException(nameof(tokenRetrevial));
Expand Down
2 changes: 1 addition & 1 deletion src/Geo.ArcGIS/Services/ArcGISTokenRetrevial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ArcGISTokenRetrevial : IArcGISTokenRetrevial
/// Initializes a new instance of the <see cref="ArcGISTokenRetrevial"/> class.
/// </summary>
/// <param name="client">A <see cref="HttpClient"/> used for placing calls to the ArcGIS token generation API.</param>
/// <param name="credentialsContainer">A <see cref="IArcGISCredentialsContainer"/> used for fetching the ArcGIS credentials.</param>
/// <param name="credentialsContainer">An <see cref="IArcGISCredentialsContainer"/> used for fetching the ArcGIS credentials.</param>
public ArcGISTokenRetrevial(
HttpClient client,
IArcGISCredentialsContainer credentialsContainer)
Expand Down
19 changes: 14 additions & 5 deletions src/Geo.Bing/DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Geo.Bing.DependencyInjection
{
using System;
using System.Net.Http;
using Geo.Bing.Abstractions;
using Geo.Bing.Models;
using Geo.Bing.Services;
Expand All @@ -26,10 +27,15 @@ public static class ServiceCollectionExtensions
/// </list>
/// </para>
/// </summary>
/// <param name="services">A <see cref="IServiceCollection"/> to add the Bing services to.</param>
/// <param name="services">An <see cref="IServiceCollection"/> to add the Bing services to.</param>
/// <param name="optionsBuilder">A <see cref="Action{BingOptionsBuilder}"/> with the options to add to the Bing configuration.</param>
/// <returns>A <see cref="IServiceCollection"/> with the added services.</returns>
public static IServiceCollection AddBingServices(this IServiceCollection services, Action<BingOptionsBuilder> optionsBuilder)
/// <param name="configureClient">Optional. A delegate that is used to configure the <see cref="HttpClient"/>.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> to configure the http client.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception>
public static IHttpClientBuilder AddBingServices(
this IServiceCollection services,
Action<BingOptionsBuilder> optionsBuilder,
Action<HttpClient> configureClient = null)
{
services.AddCoreServices();

Expand All @@ -45,9 +51,12 @@ public static IServiceCollection AddBingServices(this IServiceCollection service
services.AddSingleton<IBingKeyContainer>(new BingKeyContainer(string.Empty));
}

services.AddHttpClient<IBingGeocoding, BingGeocoding>();
return services.AddHttpClient<IBingGeocoding, BingGeocoding>(configureClient ?? DefaultHttpClientConfiguration);
}

return services;
private static void DefaultHttpClientConfiguration(HttpClient client)
{
// No-op
}
}
}
2 changes: 1 addition & 1 deletion src/Geo.Bing/Services/BingGeocoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BingGeocoding : ClientExecutor, IBingGeocoding
/// Initializes a new instance of the <see cref="BingGeocoding"/> class.
/// </summary>
/// <param name="client">A <see cref="HttpClient"/> used for placing calls to the Bing Geocoding API.</param>
/// <param name="keyContainer">A <see cref="IBingKeyContainer"/> used for fetching the Bing key.</param>
/// <param name="keyContainer">An <see cref="IBingKeyContainer"/> used for fetching the Bing key.</param>
/// <param name="exceptionProvider">An <see cref="IGeoNETExceptionProvider"/> used to provide exceptions based on an exception type.</param>
/// <param name="resourceStringProviderFactory">An <see cref="IGeoNETResourceStringProviderFactory"/> used to create a resource string provider for log or exception messages.</param>
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> used to create a logger used for logging information.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ namespace Geo.Core
public interface IGeoNETResourceStringProviderFactory
{
/// <summary>
/// Creates a <see cref="IGeoNETResourceStringProvider"/> based on a type.
/// Creates an <see cref="IGeoNETResourceStringProvider"/> based on a type.
/// </summary>
/// <typeparam name="TResource">The type of the resource to create an <see cref="IGeoNETResourceStringProvider"/> for.</typeparam>
/// <returns>An <see cref="IGeoNETResourceStringProvider"/>.</returns>
IGeoNETResourceStringProvider CreateResourceStringProvider<TResource>();

/// <summary>
/// Creates a <see cref="IGeoNETResourceStringProvider"/> based on a type.
/// Creates an <see cref="IGeoNETResourceStringProvider"/> based on a type.
/// </summary>
/// <param name="resourceType">The type of the resource to create an <see cref="IGeoNETResourceStringProvider"/> for.</param>
/// <returns>An <see cref="IGeoNETResourceStringProvider"/>.</returns>
IGeoNETResourceStringProvider CreateResourceStringProvider(Type resourceType);

/// <summary>
/// Creates a <see cref="IGeoNETResourceStringProvider"/> based on the resouce file name and assembly.
/// Creates an <see cref="IGeoNETResourceStringProvider"/> based on the resouce file name and assembly.
/// </summary>
/// <param name="resourceFileName">The name of the resource file.</param>
/// <param name="assembly">Optional. The assembly the resource file is located in. If not passed in, the current assembly will be used.</param>
Expand Down
24 changes: 19 additions & 5 deletions src/Geo.Core/DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

namespace Geo.Core.DependencyInjection
{
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

/// <summary>
/// Extension methods for the <see cref="IServiceCollection"/> class.
Expand All @@ -14,14 +16,26 @@ public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the Core services to the service collection.
/// <para>
/// Adds the services:
/// <list type="bullet">
/// <item><see cref="IGeoNETResourceStringProviderFactory"/></item>
/// <item><see cref="IGeoNETExceptionProvider"/></item>
/// </list>
/// </para>
/// </summary>
/// <param name="services">A <see cref="IServiceCollection"/> to add the Core services to.</param>
/// <returns>A <see cref="IServiceCollection"/> with the added services.</returns>
/// <param name="services">An <see cref="IServiceCollection"/> to add the Core services to.</param>
/// <returns>The <see cref="IServiceCollection"/> for chaining.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception>
public static IServiceCollection AddCoreServices(this IServiceCollection services)
{
services
.AddTransient<IGeoNETResourceStringProviderFactory, GeoNETResourceStringProviderFactory>()
.AddTransient<IGeoNETExceptionProvider, GeoNETExceptionProvider>();
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

services.TryAddTransient<IGeoNETResourceStringProviderFactory, GeoNETResourceStringProviderFactory>();
services.TryAddTransient<IGeoNETExceptionProvider, GeoNETExceptionProvider>();

return services;
}
Expand Down
19 changes: 14 additions & 5 deletions src/Geo.Google/DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Geo.Google.DependencyInjection
{
using System;
using System.Net.Http;
using Geo.Core.DependencyInjection;
using Geo.Google.Abstractions;
using Geo.Google.Models;
Expand All @@ -26,10 +27,15 @@ public static class ServiceCollectionExtensions
/// </list>
/// </para>
/// </summary>
/// <param name="services">A <see cref="IServiceCollection"/> to add the Google services to.</param>
/// <param name="services">An <see cref="IServiceCollection"/> to add the Google services to.</param>
/// <param name="optionsBuilder">A <see cref="Action{GoogleOptionsBuilder}"/> with the options to add to the Google configuration.</param>
/// <returns>A <see cref="IServiceCollection"/> with the added services.</returns>
public static IServiceCollection AddGoogleServices(this IServiceCollection services, Action<GoogleOptionsBuilder> optionsBuilder)
/// <param name="configureClient">Optional. A delegate that is used to configure the <see cref="HttpClient"/>.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> to configure the http client.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception>
public static IHttpClientBuilder AddGoogleServices(
this IServiceCollection services,
Action<GoogleOptionsBuilder> optionsBuilder,
Action<HttpClient> configureClient = null)
{
services.AddCoreServices();

Expand All @@ -45,9 +51,12 @@ public static IServiceCollection AddGoogleServices(this IServiceCollection servi
services.AddSingleton<IGoogleKeyContainer>(new GoogleKeyContainer(string.Empty));
}

services.AddHttpClient<IGoogleGeocoding, GoogleGeocoding>();
return services.AddHttpClient<IGoogleGeocoding, GoogleGeocoding>(configureClient ?? DefaultHttpClientConfiguration);
}

return services;
private static void DefaultHttpClientConfiguration(HttpClient client)
{
// No-op
}
}
}
2 changes: 1 addition & 1 deletion src/Geo.Google/Services/GoogleGeocoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class GoogleGeocoding : ClientExecutor, IGoogleGeocoding
/// Initializes a new instance of the <see cref="GoogleGeocoding"/> class.
/// </summary>
/// <param name="client">A <see cref="HttpClient"/> used for placing calls to the Google Geocoding API.</param>
/// <param name="keyContainer">A <see cref="IGoogleKeyContainer"/> used for fetching the Google key.</param>
/// <param name="keyContainer">An <see cref="IGoogleKeyContainer"/> used for fetching the Google key.</param>
/// <param name="exceptionProvider">An <see cref="IGeoNETExceptionProvider"/> used to provide exceptions based on an exception type.</param>
/// <param name="resourceStringProviderFactory">An <see cref="IGeoNETResourceStringProviderFactory"/> used to create a resource string provider for log or exception messages.</param>
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> used to create a logger used for logging information.</param>
Expand Down
19 changes: 14 additions & 5 deletions src/Geo.Here/DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Geo.Here.DependencyInjection
{
using System;
using System.Net.Http;
using Geo.Core.DependencyInjection;
using Geo.Here.Abstractions;
using Geo.Here.Models;
Expand All @@ -26,10 +27,15 @@ public static class ServiceCollectionExtensions
/// </list>
/// </para>
/// </summary>
/// <param name="services">A <see cref="IServiceCollection"/> to add the HERE services to.</param>
/// <param name="services">An <see cref="IServiceCollection"/> to add the HERE services to.</param>
/// <param name="optionsBuilder">A <see cref="Action{HereOptionsBuilder}"/> with the options to add to the HERE configuration.</param>
/// <returns>A <see cref="IServiceCollection"/> with the added services.</returns>
public static IServiceCollection AddHereServices(this IServiceCollection services, Action<HereOptionsBuilder> optionsBuilder)
/// <param name="configureClient">Optional. A delegate that is used to configure the <see cref="HttpClient"/>.</param>
/// <returns>An <see cref="IHttpClientBuilder"/> to configure the http client.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="services"/> is null.</exception>
public static IHttpClientBuilder AddHereServices(
this IServiceCollection services,
Action<HereOptionsBuilder> optionsBuilder,
Action<HttpClient> configureClient = null)
{
services.AddCoreServices();

Expand All @@ -45,9 +51,12 @@ public static IServiceCollection AddHereServices(this IServiceCollection service
services.AddSingleton<IHereKeyContainer>(new HereKeyContainer(string.Empty));
}

services.AddHttpClient<IHereGeocoding, HereGeocoding>();
return services.AddHttpClient<IHereGeocoding, HereGeocoding>(configureClient ?? DefaultHttpClientConfiguration);
}

return services;
private static void DefaultHttpClientConfiguration(HttpClient client)
{
// No-op
}
}
}
2 changes: 1 addition & 1 deletion src/Geo.Here/Services/HereGeocoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class HereGeocoding : ClientExecutor, IHereGeocoding
/// Initializes a new instance of the <see cref="HereGeocoding"/> class.
/// </summary>
/// <param name="client">A <see cref="HttpClient"/> used for placing calls to the HERE Geocoding API.</param>
/// <param name="keyContainer">A <see cref="IHereKeyContainer"/> used for fetching the HERE key.</param>
/// <param name="keyContainer">An <see cref="IHereKeyContainer"/> used for fetching the HERE key.</param>
/// <param name="exceptionProvider">An <see cref="IGeoNETExceptionProvider"/> used to provide exceptions based on an exception type.</param>
/// <param name="resourceStringProviderFactory">An <see cref="IGeoNETResourceStringProviderFactory"/> used to create a resource string provider for log or exception messages.</param>
/// <param name="loggerFactory">An <see cref="ILoggerFactory"/> used to create a logger used for logging information.</param>
Expand Down
Loading

0 comments on commit 5369e85

Please sign in to comment.