diff --git a/.github/workflows/check-branch.yml b/.github/workflows/check-branch.yml index 0690dbc..00a6a8a 100644 --- a/.github/workflows/check-branch.yml +++ b/.github/workflows/check-branch.yml @@ -8,13 +8,13 @@ jobs: runs-on: ubuntu-latest steps: - name: Comment PR - if: github.base_ref == 'main' && github.head_ref != 'next' + if: github.base_ref == 'main' && github.head_ref != 'staging' uses: thollander/actions-comment-pull-request@v2 with: message: | We regret to inform you that you are currently not able to merge your changes into the master branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the next branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch. - name: Check branch - if: github.base_ref == 'main' && github.head_ref != 'next' + if: github.base_ref == 'main' && github.head_ref != 'staging' run: | echo "ERROR: We regret to inform you that you are currently not able to merge your changes into the master branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the next branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch." exit 1 diff --git a/.github/workflows/nuget-publish.yml b/.github/workflows/nuget-publish.yml index 466d715..a355c16 100644 --- a/.github/workflows/nuget-publish.yml +++ b/.github/workflows/nuget-publish.yml @@ -10,9 +10,9 @@ jobs: runs-on: windows-latest steps: - name: Checkout repository - uses: actions/checkout@v1 + uses: actions/checkout@v4.2.2 - name: Setup .NET Core @ Latest - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4.3.0 env: NUGET_AUTH_TOKEN: ${{ secrets.NUGET_API_KEY }} - name: Build solution and generate NuGet package @@ -28,9 +28,9 @@ jobs: runs-on: windows-latest steps: - name: Checkout repository - uses: actions/checkout@v1 + uses: actions/checkout@v4.2.2 - name: Setup .NET Core @ Latest - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4.3.0 with: source-url: https://nuget.pkg.github.com/Contentstack/index.json env: diff --git a/.github/workflows/sast-scan.yml b/.github/workflows/sast-scan.yml index 3b9521a..3d2096e 100644 --- a/.github/workflows/sast-scan.yml +++ b/.github/workflows/sast-scan.yml @@ -6,6 +6,6 @@ jobs: security-sast: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4.2.2 - name: Semgrep Scan run: docker run -v /var/run/docker.sock:/var/run/docker.sock -v "${PWD}:/src" returntocorp/semgrep semgrep scan --config auto \ No newline at end of file diff --git a/.github/workflows/sca-scan.yml b/.github/workflows/sca-scan.yml index 4fa4560..c1519f9 100644 --- a/.github/workflows/sca-scan.yml +++ b/.github/workflows/sca-scan.yml @@ -6,10 +6,14 @@ jobs: security-sca: runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - name: Checkout repository + uses: actions/checkout@master + - name: Run Dotnet Restore + run: | + dotnet restore - name: Run Snyk to check for vulnerabilities uses: snyk/actions/dotnet@master env: SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} with: - args: --fail-on=all + args: --file=Contentstack.Management.Core/obj/project.assets.json --fail-on=all diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 5bce498..bfa8945 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -8,9 +8,9 @@ jobs: runs-on: windows-latest steps: - name: Checkout repository - uses: actions/checkout@v1 + uses: actions/checkout@v4.2.2 - name: Setup .NET Core @ Latest - uses: actions/setup-dotnet@v1 + uses: actions/setup-dotnet@v4.3.0 - name: Build solution and run unit test run: sh ./Scripts/run-unit-test-case.sh - name: Test Report diff --git a/CHANGELOG.md b/CHANGELOG.md index ecab8a0..72557e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [v0.1.11](https://github.com/contentstack/contentstack-management-dotnet/tree/v0.1.11) + - Feat + - Add support for custom Http client and IHttpClientFactory + ## [v0.1.10](https://github.com/contentstack/contentstack-management-dotnet/tree/v0.1.10) - Feat - Add support for apiVersion in bulk publish unpublish methods diff --git a/Contentstack.Management.ASPNETCore/ServiceCollectionExtensions.cs b/Contentstack.Management.ASPNETCore/ServiceCollectionExtensions.cs index 3798ed8..75ac0dd 100644 --- a/Contentstack.Management.ASPNETCore/ServiceCollectionExtensions.cs +++ b/Contentstack.Management.ASPNETCore/ServiceCollectionExtensions.cs @@ -1,11 +1,7 @@ - -using System.Collections.Generic; -using System.Text; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Configuration; -using Contentstack.Management.Core; +using Contentstack.Management.Core; using System.Net.Http; +using System; + namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions @@ -17,7 +13,7 @@ public static class ServiceCollectionExtensions /// The IServiceCollection. /// The ContentstackClientOptions used to retrieve configuration from. /// The IServiceCollection. - public static IServiceCollection AddContentstackClien(this IServiceCollection services, ContentstackClientOptions configuration) + public static IServiceCollection AddContentstackClient(this IServiceCollection services, ContentstackClientOptions configuration) { return services; @@ -34,5 +30,12 @@ public static IServiceCollection TryAddContentstackClient(this IServiceCollectio return services; } + + public static IServiceCollection AddContentstackClient(this IServiceCollection services, Action configureClient) + { + services.AddHttpClient(configureClient); + + return services; + } } } \ No newline at end of file diff --git a/Contentstack.Management.ASPNETCore/contentstack.management.aspnetcore.csproj b/Contentstack.Management.ASPNETCore/contentstack.management.aspnetcore.csproj index a4880ce..1d31ba9 100644 --- a/Contentstack.Management.ASPNETCore/contentstack.management.aspnetcore.csproj +++ b/Contentstack.Management.ASPNETCore/contentstack.management.aspnetcore.csproj @@ -15,7 +15,7 @@ .NET SDK for the Contentstack Content Management API. LICENSE.txt v$(Version) - $(Version) + 0.1.3 Contentstack.Management.ASPNETCore @@ -26,8 +26,9 @@ - - - + + + + diff --git a/Contentstack.Management.Core.Tests/Contentstack.Management.Core.Tests.csproj b/Contentstack.Management.Core.Tests/Contentstack.Management.Core.Tests.csproj index 4450c78..f8be953 100644 --- a/Contentstack.Management.Core.Tests/Contentstack.Management.Core.Tests.csproj +++ b/Contentstack.Management.Core.Tests/Contentstack.Management.Core.Tests.csproj @@ -4,26 +4,26 @@ net7.0 false - 0.1.10 + $(Version) true ../CSManagementSDK.snk - - - - runtime; build; native; contentfiles; analyzers; buildtransitive + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - - - - + + + + + + + diff --git a/Contentstack.Management.Core.Unit.Tests/Contentstack.Management.Core.Unit.Tests.csproj b/Contentstack.Management.Core.Unit.Tests/Contentstack.Management.Core.Unit.Tests.csproj index 8c53ce9..e351e54 100644 --- a/Contentstack.Management.Core.Unit.Tests/Contentstack.Management.Core.Unit.Tests.csproj +++ b/Contentstack.Management.Core.Unit.Tests/Contentstack.Management.Core.Unit.Tests.csproj @@ -4,22 +4,22 @@ net7.0 false - $(Version) + 0.1.3 true ../CSManagementSDK.snk - - - - runtime; build; native; contentfiles; analyzers; buildtransitive + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - + + + + diff --git a/Contentstack.Management.Core.Unit.Tests/Core/ContentstackClientTest.cs b/Contentstack.Management.Core.Unit.Tests/Core/ContentstackClientTest.cs index 6a32846..868c91d 100644 --- a/Contentstack.Management.Core.Unit.Tests/Core/ContentstackClientTest.cs +++ b/Contentstack.Management.Core.Unit.Tests/Core/ContentstackClientTest.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using Contentstack.Management.Core.Http; using Contentstack.Management.Core.Models; using Contentstack.Management.Core.Runtime.Contexts; @@ -62,7 +63,7 @@ public void Initialize_Contentstack_With_All_Options() } [TestMethod] - public void Initialize_Contentstack_With_Clientptions() + public void Initialize_Contentstack_With_ClientOptions() { var contentstackClient = new ContentstackClient(new ContentstackClientOptions() { @@ -94,6 +95,43 @@ public void Initialize_Contentstack_With_Clientptions() CollectionAssert.AreEqual(new string[] {"ea1", "ea2"}, contentstackClient.contentstackOptions.EarlyAccess); } + [TestMethod] + public void Initialize_Contentstack_With_CustomHTTPClient() + { + // create a custom HttpClient + var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Add("CustomHeader", "CustomValue"); + + var contentstackClient = new ContentstackClient(httpClient, new ContentstackClientOptions() + { + Authtoken = "token", + Host= "host", + Port= 445, + Version= "v4", + DisableLogging= true, + MaxResponseContentBufferSize= 1234, + Timeout= TimeSpan.FromSeconds(20), + RetryOnError= false, + ProxyHost= "proxyHost", + ProxyPort= 22, + EarlyAccess = new string[] { "ea1", "ea2" } + }); + + Assert.AreEqual("token", contentstackClient.contentstackOptions.Authtoken); + Assert.AreEqual("host", contentstackClient.contentstackOptions.Host); + Assert.AreEqual(445, contentstackClient.contentstackOptions.Port); + Assert.AreEqual("v4", contentstackClient.contentstackOptions.Version); + Assert.AreEqual("proxyHost", contentstackClient.contentstackOptions.ProxyHost); + Assert.AreEqual(22, contentstackClient.contentstackOptions.ProxyPort); + Assert.IsNull(contentstackClient.contentstackOptions.ProxyCredentials); + Assert.IsNotNull(contentstackClient.contentstackOptions.GetWebProxy()); + Assert.IsFalse(contentstackClient.contentstackOptions.RetryOnError); + Assert.IsTrue(contentstackClient.contentstackOptions.DisableLogging); + Assert.AreEqual(1234, contentstackClient.contentstackOptions.MaxResponseContentBufferSize); + Assert.AreEqual(20, contentstackClient.contentstackOptions.Timeout.Seconds); + CollectionAssert.AreEqual(new string[] {"ea1", "ea2"}, contentstackClient.contentstackOptions.EarlyAccess); + } + [TestMethod] public void Should_Dispose_ContentstackClientAsync() { diff --git a/Contentstack.Management.Core/ContentstackClient.cs b/Contentstack.Management.Core/ContentstackClient.cs index 221c319..5e4bb42 100644 --- a/Contentstack.Management.Core/ContentstackClient.cs +++ b/Contentstack.Management.Core/ContentstackClient.cs @@ -35,7 +35,7 @@ public class ContentstackClient : IContentstackClient private HttpClient _httpClient; private bool _disposed = false; - private string Version => "0.1.10"; + private string Version => "0.1.11"; private string xUserAgent => $"contentstack-management-dotnet/{Version}"; #endregion @@ -72,6 +72,30 @@ public ContentstackClient(IOptions contentstackOption BuildPipeline(); } + + /// + /// Initializes new instance of the class with custom HttpClient. + /// + /// The user can pass. + /// + ///

+        /// HttpClientHandler httpClientHandler = new HttpClientHandler
+        /// {
+        ///     Proxy = contentstackOptions.GetWebProxy()
+        /// };
+
+        /// _httpClient = new HttpClient(httpClientHandler);
+        /// ContentstackClient client = new ContentstackClient(_httpClient);
+        /// 
+ ///
+ public ContentstackClient(HttpClient httpClient, ContentstackClientOptions contentstackOptions) + { + this.contentstackOptions = contentstackOptions; + _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); + Initialize(_httpClient); + BuildPipeline(); + } + public ContentstackClient(ContentstackClientOptions contentstackOptions) : this(new OptionsWrapper(contentstackOptions)) { } @@ -126,14 +150,21 @@ public ContentstackClient( { } #endregion - protected void Initialize() + protected void Initialize(HttpClient httpClient = null) { - HttpClientHandler httpClientHandler = new HttpClientHandler + if (httpClient != null) { - Proxy = contentstackOptions.GetWebProxy() - }; + _httpClient = httpClient; + } + else + { + HttpClientHandler httpClientHandler = new HttpClientHandler + { + Proxy = contentstackOptions.GetWebProxy() + }; - _httpClient = new HttpClient(httpClientHandler); + _httpClient = new HttpClient(httpClientHandler); + } _httpClient.DefaultRequestHeaders.Add(HeadersKey.XUserAgentHeader, $"{xUserAgent}"); diff --git a/Contentstack.Management.Core/contentstack.management.core.csproj b/Contentstack.Management.Core/contentstack.management.core.csproj index 94b1097..f1f29bc 100644 --- a/Contentstack.Management.Core/contentstack.management.core.csproj +++ b/Contentstack.Management.Core/contentstack.management.core.csproj @@ -64,7 +64,7 @@
- + diff --git a/Directory.Build.props b/Directory.Build.props index 6d5fa21..b5e0738 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 0.1.10 + 0.1.11 diff --git a/LICENSE b/LICENSE index e17cc92..3851325 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2012-2024 Contentstack +Copyright (c) 2012-2025 Contentstack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal