From 340c2ac24a91edd9bac8677a2128f570c930ec3a Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Wed, 9 Oct 2024 13:28:06 +0530 Subject: [PATCH 1/5] feat: :sparkles: Live Preview 2.0 Implementation --- .../Configuration/LivePreviewConfig.cs | 1 + Contentstack.Core/ContentstackClient.cs | 20 +++++++++++++++++-- Contentstack.Core/Models/Entry.cs | 18 ++++++++++++----- Contentstack.Core/Models/Query.cs | 10 +++++++++- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Contentstack.Core/Configuration/LivePreviewConfig.cs b/Contentstack.Core/Configuration/LivePreviewConfig.cs index 84510fe..240db8e 100644 --- a/Contentstack.Core/Configuration/LivePreviewConfig.cs +++ b/Contentstack.Core/Configuration/LivePreviewConfig.cs @@ -7,6 +7,7 @@ namespace Contentstack.Core.Configuration public class LivePreviewConfig { public string ManagementToken { get; set; } + public string PreviewToken { get; set; } public bool Enable { get; set; } public string Host { get; set; } internal string LivePreview { get; set; } diff --git a/Contentstack.Core/ContentstackClient.cs b/Contentstack.Core/ContentstackClient.cs index 436c82a..f22ffb8 100644 --- a/Contentstack.Core/ContentstackClient.cs +++ b/Contentstack.Core/ContentstackClient.cs @@ -123,7 +123,16 @@ public ContentstackClient(IOptions options) } if (this.LivePreviewConfig.Host == null) { - this.LivePreviewConfig.Host = "api.contentstack.io"; + if (this.LivePreviewConfig.ManagementToken != null) + { + this.LivePreviewConfig.Host = "api.contentstack.io"; + } + else if (this.LivePreviewConfig.PreviewToken != null) + { + this.LivePreviewConfig.Host = "rest-preview.contentstack.com"; + } else { + throw new InvalidOperationException("Add PreviewToken or ManagementToken in LivePreviewConfig"); + } } this.SerializerSettings.DateParseHandling = DateParseHandling.None; this.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat; @@ -347,7 +356,14 @@ private async Task GetLivePreviewData() } } mainJson.Add("live_preview", this.LivePreviewConfig.LivePreview ?? "init"); - headerAll["authorization"] = this.LivePreviewConfig.ManagementToken; + + if (!string.IsNullOrEmpty(this.LivePreviewConfig.ManagementToken)) { + headerAll["authorization"] = this.LivePreviewConfig.ManagementToken; + } else if (!string.IsNullOrEmpty(this.LivePreviewConfig.PreviewToken)) { + headerAll["preview_token"] = this.LivePreviewConfig.PreviewToken; + } else { + throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig"); + } try { diff --git a/Contentstack.Core/Models/Entry.cs b/Contentstack.Core/Models/Entry.cs index 945a7ea..c8a8788 100644 --- a/Contentstack.Core/Models/Entry.cs +++ b/Contentstack.Core/Models/Entry.cs @@ -48,7 +48,7 @@ private string _Url internal Dictionary _metadata = new Dictionary(); #endregion - + #region Public Properties /// /// Title of an entry @@ -123,9 +123,9 @@ private string _Url /// }); /// /// - public Dictionary _variant { get; set; } - - + public Dictionary _variant { get; set; } + + /// /// Set key/value attributes of an current entry instance. /// @@ -1393,7 +1393,15 @@ public async Task Fetch() if (this.ContentTypeInstance.StackInstance.LivePreviewConfig.Enable == true && this.ContentTypeInstance.StackInstance.LivePreviewConfig.ContentTypeUID == this.ContentTypeInstance.ContentTypeId) { mainJson.Add("live_preview", this.ContentTypeInstance.StackInstance.LivePreviewConfig.LivePreview ?? "init"); - headerAll["authorization"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken; + + if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken)) { + headerAll["authorization"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken; + } else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) { + headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken; + } else { + throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig"); + } + isLivePreview = true; } diff --git a/Contentstack.Core/Models/Query.cs b/Contentstack.Core/Models/Query.cs index 2710007..1c59087 100644 --- a/Contentstack.Core/Models/Query.cs +++ b/Contentstack.Core/Models/Query.cs @@ -1848,7 +1848,15 @@ private async Task Exec() && this.ContentTypeInstance.StackInstance.LivePreviewConfig.ContentTypeUID == this.ContentTypeInstance.ContentTypeId) { mainJson.Add("live_preview", this.ContentTypeInstance.StackInstance.LivePreviewConfig.LivePreview ?? "init"); - headerAll["authorization"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken; + + if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken)) { + headerAll["authorization"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.ManagementToken; + } else if (!string.IsNullOrEmpty(this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken)) { + headerAll["preview_token"] = this.ContentTypeInstance.StackInstance.LivePreviewConfig.PreviewToken; + } else { + throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig"); + } + isLivePreview = true; } From 172ac25903d02d77033ecf36130694c0ee31a8a4 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Fri, 11 Oct 2024 10:17:14 +0530 Subject: [PATCH 2/5] chore: version bump --- CHANGELOG.md | 6 ++++++ Contentstack.AspNetCore/Contentstack.AspNetCore.csproj | 2 +- Contentstack.Core/Internals/HttpRequestHandler.cs | 2 +- Directory.Build.props | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 711d3a0..e8f9e81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +### Version: 2.16.0 +#### Date: Oct-11-2024 + +##### Feat: +- Live Preview 2.0 Implementation + ### Version: 2.15.1 #### Date: Oct-08-2024 diff --git a/Contentstack.AspNetCore/Contentstack.AspNetCore.csproj b/Contentstack.AspNetCore/Contentstack.AspNetCore.csproj index 41f678c..dcd0c98 100644 --- a/Contentstack.AspNetCore/Contentstack.AspNetCore.csproj +++ b/Contentstack.AspNetCore/Contentstack.AspNetCore.csproj @@ -31,6 +31,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Contentstack.Core/Internals/HttpRequestHandler.cs b/Contentstack.Core/Internals/HttpRequestHandler.cs index 70410c6..095ee04 100644 --- a/Contentstack.Core/Internals/HttpRequestHandler.cs +++ b/Contentstack.Core/Internals/HttpRequestHandler.cs @@ -48,7 +48,7 @@ public async Task ProcessRequest(string Url, Dictionary var request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "GET"; request.ContentType = "application/json"; - request.Headers["x-user-agent"]="contentstack-delivery-dotnet/2.12.0"; + request.Headers["x-user-agent"]="contentstack-delivery-dotnet/2.16.0"; request.Timeout = timeout; if (proxy != null) diff --git a/Directory.Build.props b/Directory.Build.props index e18626e..0b3a5c9 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 2.15.1 + 2.16.0 From ba2065204867d7a7153f909c4c486c0f1a0ed867 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Fri, 11 Oct 2024 12:14:32 +0530 Subject: [PATCH 3/5] fix: Changelog update --- CHANGELOG.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8f9e81..ec2ed05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,6 @@ ##### Feat: - Live Preview 2.0 Implementation - -### Version: 2.15.1 -#### Date: Oct-08-2024 - ##### Fix: - Removed exclusion of env when adding headers From b0e06c6f3db43411cf424000307516908a3b5f76 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Fri, 11 Oct 2024 12:16:40 +0530 Subject: [PATCH 4/5] fix: workflow fix for check branch --- .github/workflows/check-branch.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check-branch.yml b/.github/workflows/check-branch.yml index e341da3..2332f0d 100644 --- a/.github/workflows/check-branch.yml +++ b/.github/workflows/check-branch.yml @@ -12,9 +12,9 @@ jobs: 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. + 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 staging 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 == 'master' && github.head_ref != 'next' + if: github.base_ref == 'master' && 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." + 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 staging branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch." exit 1 From f920979b4f66bfd11af2d931dd2679e721241954 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Fri, 11 Oct 2024 12:30:41 +0530 Subject: [PATCH 5/5] chore: update utils package version --- Contentstack.Core/Contentstack.Core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Contentstack.Core/Contentstack.Core.csproj b/Contentstack.Core/Contentstack.Core.csproj index 7583d3e..030a3a7 100644 --- a/Contentstack.Core/Contentstack.Core.csproj +++ b/Contentstack.Core/Contentstack.Core.csproj @@ -31,7 +31,7 @@ - +