Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Live Preview 2.0 Implementation #61

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/check-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
### Version: 2.15.1
#### Date: Oct-08-2024
### Version: 2.16.0
#### Date: Oct-11-2024

##### Feat:
- Live Preview 2.0 Implementation
##### Fix:
- Removed exclusion of env when adding headers

Expand Down
2 changes: 1 addition & 1 deletion Contentstack.AspNetCore/Contentstack.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
<PackageReference Include="NuGet.Build.Packaging" Version="0.2.2"><PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="contentstack.csharp" Version="2.13.0" />
<PackageReference Include="contentstack.csharp" Version="2.15.0" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Contentstack.Core/Configuration/LivePreviewConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
20 changes: 18 additions & 2 deletions Contentstack.Core/ContentstackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ public ContentstackClient(IOptions<ContentstackOptions> 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;
Expand Down Expand Up @@ -347,7 +356,14 @@ private async Task<JObject> 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
{
Expand Down
2 changes: 1 addition & 1 deletion Contentstack.Core/Internals/HttpRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
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)
Expand Down
18 changes: 13 additions & 5 deletions Contentstack.Core/Models/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private string _Url

internal Dictionary<string, object> _metadata = new Dictionary<string, object>();
#endregion

#region Public Properties
/// <summary>
/// Title of an entry
Expand Down Expand Up @@ -123,9 +123,9 @@ private string _Url
/// });
/// </code>
/// </example>
public Dictionary<string, object> _variant { get; set; }


public Dictionary<string, object> _variant { get; set; }
/// <summary>
/// Set key/value attributes of an current entry instance.
/// </summary>
Expand Down Expand Up @@ -1393,7 +1393,15 @@ public async Task<T> Fetch<T>()
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;
}

Expand Down
10 changes: 9 additions & 1 deletion Contentstack.Core/Models/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,15 @@ private async Task<JObject> 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;
}

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>2.15.1</Version>
<Version>2.16.0</Version>
</PropertyGroup>
</Project>
Loading