Skip to content

Commit 612cef8

Browse files
authored
Merge pull request #63 from contentstack/feat/DX-1615
feat: addcontentstackclient method overload
2 parents f36a93f + 0f9ae2f commit 612cef8

File tree

6 files changed

+97
-20
lines changed

6 files changed

+97
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v0.1.11](https://github.com/contentstack/contentstack-management-dotnet/tree/v0.1.11)
4+
- Feat
5+
- Add support for custom Http client and IHttpClientFactory
6+
37
## [v0.1.10](https://github.com/contentstack/contentstack-management-dotnet/tree/v0.1.10)
48
- Feat
59
- Add support for apiVersion in bulk publish unpublish methods

Contentstack.Management.ASPNETCore/ServiceCollectionExtensions.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-

2-
using System.Collections.Generic;
3-
using System.Text;
4-
using Microsoft.Extensions.DependencyInjection;
5-
using Microsoft.Extensions.DependencyInjection.Extensions;
6-
using Microsoft.Extensions.Configuration;
7-
using Contentstack.Management.Core;
1+
using Contentstack.Management.Core;
82
using System.Net.Http;
3+
using System;
4+
95
namespace Microsoft.Extensions.DependencyInjection
106
{
117
public static class ServiceCollectionExtensions
@@ -17,7 +13,7 @@ public static class ServiceCollectionExtensions
1713
/// <param name="services">The IServiceCollection.</param>
1814
/// <param name="configuration">The ContentstackClientOptions used to retrieve configuration from.</param>
1915
/// <returns>The IServiceCollection.</returns>
20-
public static IServiceCollection AddContentstackClien(this IServiceCollection services, ContentstackClientOptions configuration)
16+
public static IServiceCollection AddContentstackClient(this IServiceCollection services, ContentstackClientOptions configuration)
2117
{
2218

2319
return services;
@@ -34,5 +30,12 @@ public static IServiceCollection TryAddContentstackClient(this IServiceCollectio
3430

3531
return services;
3632
}
33+
34+
public static IServiceCollection AddContentstackClient(this IServiceCollection services, Action<HttpClient> configureClient)
35+
{
36+
services.AddHttpClient<ContentstackClient>(configureClient);
37+
38+
return services;
39+
}
3740
}
3841
}

Contentstack.Management.ASPNETCore/contentstack.management.aspnetcore.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<Description>.NET SDK for the Contentstack Content Management API.</Description>
1616
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
1717
<PackageTags>v$(Version)</PackageTags>
18-
<ReleaseVersion>$(Version)</ReleaseVersion>
18+
<ReleaseVersion>0.1.3</ReleaseVersion>
1919
<RootNamespace>Contentstack.Management.ASPNETCore</RootNamespace>
2020
</PropertyGroup>
2121

@@ -26,8 +26,9 @@
2626
<None Include="LICENSE.txt" Pack="true" PackagePath="LICENSE.txt" />
2727
</ItemGroup>
2828
<ItemGroup>
29-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
30-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
31-
<PackageReference Include="contentstack.management.csharp" Version="0.1.8" />
29+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
30+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.1" />
31+
<PackageReference Include="contentstack.management.csharp" Version="0.1.11" />
32+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.1" />
3233
</ItemGroup>
3334
</Project>

Contentstack.Management.Core.Unit.Tests/Core/ContentstackClientTest.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net.Http;
23
using Contentstack.Management.Core.Http;
34
using Contentstack.Management.Core.Models;
45
using Contentstack.Management.Core.Runtime.Contexts;
@@ -62,7 +63,7 @@ public void Initialize_Contentstack_With_All_Options()
6263
}
6364

6465
[TestMethod]
65-
public void Initialize_Contentstack_With_Clientptions()
66+
public void Initialize_Contentstack_With_ClientOptions()
6667
{
6768
var contentstackClient = new ContentstackClient(new ContentstackClientOptions()
6869
{
@@ -94,6 +95,43 @@ public void Initialize_Contentstack_With_Clientptions()
9495
CollectionAssert.AreEqual(new string[] {"ea1", "ea2"}, contentstackClient.contentstackOptions.EarlyAccess);
9596
}
9697

98+
[TestMethod]
99+
public void Initialize_Contentstack_With_CustomHTTPClient()
100+
{
101+
// create a custom HttpClient
102+
var httpClient = new HttpClient();
103+
httpClient.DefaultRequestHeaders.Add("CustomHeader", "CustomValue");
104+
105+
var contentstackClient = new ContentstackClient(httpClient, new ContentstackClientOptions()
106+
{
107+
Authtoken = "token",
108+
Host= "host",
109+
Port= 445,
110+
Version= "v4",
111+
DisableLogging= true,
112+
MaxResponseContentBufferSize= 1234,
113+
Timeout= TimeSpan.FromSeconds(20),
114+
RetryOnError= false,
115+
ProxyHost= "proxyHost",
116+
ProxyPort= 22,
117+
EarlyAccess = new string[] { "ea1", "ea2" }
118+
});
119+
120+
Assert.AreEqual("token", contentstackClient.contentstackOptions.Authtoken);
121+
Assert.AreEqual("host", contentstackClient.contentstackOptions.Host);
122+
Assert.AreEqual(445, contentstackClient.contentstackOptions.Port);
123+
Assert.AreEqual("v4", contentstackClient.contentstackOptions.Version);
124+
Assert.AreEqual("proxyHost", contentstackClient.contentstackOptions.ProxyHost);
125+
Assert.AreEqual(22, contentstackClient.contentstackOptions.ProxyPort);
126+
Assert.IsNull(contentstackClient.contentstackOptions.ProxyCredentials);
127+
Assert.IsNotNull(contentstackClient.contentstackOptions.GetWebProxy());
128+
Assert.IsFalse(contentstackClient.contentstackOptions.RetryOnError);
129+
Assert.IsTrue(contentstackClient.contentstackOptions.DisableLogging);
130+
Assert.AreEqual(1234, contentstackClient.contentstackOptions.MaxResponseContentBufferSize);
131+
Assert.AreEqual(20, contentstackClient.contentstackOptions.Timeout.Seconds);
132+
CollectionAssert.AreEqual(new string[] {"ea1", "ea2"}, contentstackClient.contentstackOptions.EarlyAccess);
133+
}
134+
97135
[TestMethod]
98136
public void Should_Dispose_ContentstackClientAsync()
99137
{

Contentstack.Management.Core/ContentstackClient.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ContentstackClient : IContentstackClient
3535
private HttpClient _httpClient;
3636
private bool _disposed = false;
3737

38-
private string Version => "0.1.10";
38+
private string Version => "0.1.11";
3939
private string xUserAgent => $"contentstack-management-dotnet/{Version}";
4040
#endregion
4141

@@ -72,6 +72,30 @@ public ContentstackClient(IOptions<ContentstackClientOptions> contentstackOption
7272
BuildPipeline();
7373
}
7474

75+
76+
/// <summary>
77+
/// Initializes new instance of the <see cref="ContentstackClient"/> class with custom HttpClient.
78+
/// </summary>
79+
/// <param name="httpClient">The <see cref="HttpClient"/> user can pass.</param>
80+
/// <example>
81+
/// <pre><code>
82+
/// HttpClientHandler httpClientHandler = new HttpClientHandler
83+
/// {
84+
/// Proxy = contentstackOptions.GetWebProxy()
85+
/// };
86+
87+
/// _httpClient = new HttpClient(httpClientHandler);
88+
/// ContentstackClient client = new ContentstackClient(_httpClient);
89+
/// </code></pre>
90+
/// </example>
91+
public ContentstackClient(HttpClient httpClient, ContentstackClientOptions contentstackOptions)
92+
{
93+
this.contentstackOptions = contentstackOptions;
94+
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
95+
Initialize(_httpClient);
96+
BuildPipeline();
97+
}
98+
7599
public ContentstackClient(ContentstackClientOptions contentstackOptions) :
76100
this(new OptionsWrapper<ContentstackClientOptions>(contentstackOptions))
77101
{ }
@@ -126,14 +150,21 @@ public ContentstackClient(
126150
{ }
127151
#endregion
128152

129-
protected void Initialize()
153+
protected void Initialize(HttpClient httpClient = null)
130154
{
131-
HttpClientHandler httpClientHandler = new HttpClientHandler
155+
if (httpClient != null)
132156
{
133-
Proxy = contentstackOptions.GetWebProxy()
134-
};
157+
_httpClient = httpClient;
158+
}
159+
else
160+
{
161+
HttpClientHandler httpClientHandler = new HttpClientHandler
162+
{
163+
Proxy = contentstackOptions.GetWebProxy()
164+
};
135165

136-
_httpClient = new HttpClient(httpClientHandler);
166+
_httpClient = new HttpClient(httpClientHandler);
167+
}
137168

138169
_httpClient.DefaultRequestHeaders.Add(HeadersKey.XUserAgentHeader, $"{xUserAgent}");
139170

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>0.1.10</Version>
3+
<Version>0.1.11</Version>
44
</PropertyGroup>
55
</Project>

0 commit comments

Comments
 (0)