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 2 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: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

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