Skip to content

Commit

Permalink
Add support for global IHttpClientFactory and changing instance-speci…
Browse files Browse the repository at this point in the history
…fic HttpClients

Closes #369
  • Loading branch information
nozzlegear committed Nov 17, 2021
1 parent b84bf3e commit c5400c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
18 changes: 18 additions & 0 deletions ShopifySharp/Infrastructure/DefaultHttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Net.Http;
using Microsoft.Extensions.Http;

namespace ShopifySharp.Infrastructure
{
/// <summary>
/// ShopifySharp's default <see cref="IHttpClientFactory" />, which uses a static <see cref="HttpClient" />.
/// </summary>
public class DefaultHttpClientFactory : IHttpClientFactory
{
private static readonly HttpClient _Client = new HttpClient();

public HttpClient CreateClient(string name)
{
return _Client;
}
}
}
31 changes: 27 additions & 4 deletions ShopifySharp/Services/ShopifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@
using System.Threading;
using ShopifySharp.Lists;
using ShopifySharp.Filters;
using Microsoft.Extensions.Http;

namespace ShopifySharp
{
public abstract class ShopifyService
{
public virtual string APIVersion => "2021-10";

private static JsonSerializer _Serializer = Serializer.JsonSerializer;

private static IRequestExecutionPolicy _GlobalExecutionPolicy = new DefaultRequestExecutionPolicy();

private IRequestExecutionPolicy _ExecutionPolicy;
private static IHttpClientFactory _HttpClientFactory = new DefaultHttpClientFactory();

private static JsonSerializer _Serializer = Serializer.JsonSerializer;
private HttpClient _Client;

private static HttpClient _Client { get; } = new HttpClient();
private IRequestExecutionPolicy _ExecutionPolicy;

protected Uri _ShopUri { get; set; }

Expand All @@ -40,6 +43,7 @@ protected ShopifyService(string myShopifyUrl, string shopAccessToken)
{
_ShopUri = BuildShopUri(myShopifyUrl, false);
_AccessToken = shopAccessToken;
_Client = _HttpClientFactory.CreateClient();

// If there's a global execution policy it should be set as this instance's policy.
// User can override it with instance-specific execution policy.
Expand Down Expand Up @@ -78,7 +82,7 @@ public static Uri BuildShopUri(string myShopifyUrl, bool withAdminPath)

/// <summary>
/// Sets the execution policy for this instance only. This policy will always be used over the global execution policy.
/// The instance will revert back to the global execution policy if you pass null to this method.
/// This instance will revert back to the global execution policy if you pass null to this method.
/// </summary>
public void SetExecutionPolicy(IRequestExecutionPolicy executionPolicy)
{
Expand All @@ -94,6 +98,25 @@ public static void SetGlobalExecutionPolicy(IRequestExecutionPolicy globalExecut
_GlobalExecutionPolicy = globalExecutionPolicy;
}

/// <summary>
/// Sets the <see cref="HttpClient" /> for this instance only. This client will always be used over the one from the global <see cref="IHttpClientFactory" />.
/// This instance will revert back to the global execution policy if you pass null to this method.
/// </summary>
public void SetHttpClient(HttpClient client)
{
// If the user passes null, revert to the client factory's client.
_Client = client ?? _HttpClientFactory.CreateClient();
}

/// <summary>
/// Sets the global <see cref="IHttpClientFactory" /> for all *new* instances. Current instances are unaffected, but you can call <see cref="SetHttpClient(HttpClient)" /> on them.
/// The factory will revert back to the <see cref="DefaultHttpClientFactory" /> if you pass null to this method.
/// </summary>
public static void SetGlobalHttpClientFactory(IHttpClientFactory factory)
{
_HttpClientFactory = factory ?? new DefaultHttpClientFactory();
}

protected RequestUri PrepareRequest(string path)
{
var ub = new UriBuilder(_ShopUri)
Expand Down
1 change: 1 addition & 0 deletions ShopifySharp/ShopifySharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageTags>shopify,ecommerce</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="microsoft.extensions.primitives" Version="6.0.0" />
<PackageReference Include="newtonsoft.json" Version="13.0.1" />
Expand Down

0 comments on commit c5400c4

Please sign in to comment.