Skip to content

Commit

Permalink
feat: ✨ adds Timeout option in ContentstackOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeem-cs committed Feb 1, 2024
1 parent cf1c774 commit 78450ef
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 78 deletions.
4 changes: 3 additions & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fileignoreconfig:
- filename: Contentstack.Core/ContentstackClient.cs
checksum: 2dd2ef59ca68dcc7c26d1136d6504272ac04b1840d242a57f9fabd603555225b
checksum: f960fff17b452a71e39e51ae8b92759dd1e30638d379155185e3e8bd63f407a4
- filename: Contentstack.Core/Internals/HttpRequestHandler.cs
checksum: 93c1659f3bc7527956f0fd12db46441297fac3a4366d273bcbb3425d2351300e
6 changes: 3 additions & 3 deletions Contentstack.AspNetCore/Contentstack.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<PackageId>contentstack.aspnetcore</PackageId>
<Authors>Contentstack</Authors>
<Owners>Contentstack</Owners>
<PackageVersion>2.11.0</PackageVersion>
<PackageVersion>2.12.0</PackageVersion>
<Description>Main release</Description>
<Copyright>Copyright (c) 2012-2023 Contentstack (http://app.contentstack.com). All Rights Reserved</Copyright>
<PackageProjectUrl>https://github.com/contentstack/contentstack-dotnet</PackageProjectUrl>
<PackageTags>v2.11.0</PackageTags>
<ReleaseVersion>2.11.0</ReleaseVersion>
<PackageTags>v2.12.0</PackageTags>
<ReleaseVersion>2.12.0</ReleaseVersion>
<Configurations>Release;Debug</Configurations>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion Contentstack.Core.Tests/Contentstack.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
<ReleaseVersion>2.11.0</ReleaseVersion>
<ReleaseVersion>2.12.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Contentstack.Core.Tests/StackConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static ContentstackClient GetStack()
DeliveryToken = delivery_token,
Environment = environment,
Host = host,

Timeout = 4500
};

ContentstackClient contentstackClient = new ContentstackClient(new OptionsWrapper<Configuration.ContentstackOptions>(contentstackOptions));
Expand Down
7 changes: 7 additions & 0 deletions Contentstack.Core/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class Config
private string _Version;
private string _Environment;
private string _Branch;
private int _Timeout;
#endregion

#region Public Properties
Expand Down Expand Up @@ -60,6 +61,12 @@ public string Branch
set { this._Branch = value; }
}

public int Timeout
{
get { return this._Timeout; }
set { this._Timeout = value; }
}

public string BaseUrl
{
get
Expand Down
5 changes: 5 additions & 0 deletions Contentstack.Core/Configuration/ContentstackOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public class ContentstackOptions
/// The Branch used to set Branch for the Contentstack API.
/// </summary>
public string Branch { get; set; }

/// <summary>
/// The Timeout used to set Timeout for the Contentstack API.
/// </summary>
public int Timeout { get; set; }
}

internal class ContentstackRegionConverter : TypeConverter
Expand Down
6 changes: 3 additions & 3 deletions Contentstack.Core/Contentstack.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<PackageId>contentstack.csharp</PackageId>
<Authors>Contentstack</Authors>
<Description>.NET SDK for the Contentstack Content Delivery API.</Description>
<PackageVersion>2.11.0</PackageVersion>
<PackageVersion>2.12.0</PackageVersion>
<Owners>Contentstack</Owners>
<PackageReleaseNotes>Reference in entry Live preview support added</PackageReleaseNotes>
<Copyright>Copyright © 2012-2023 Contentstack. All Rights Reserved</Copyright>
<PackOnBuild>true</PackOnBuild>
<PackageTags>v2.11.0</PackageTags>
<PackageTags>v2.12.0</PackageTags>
<PackageProjectUrl>https://github.com/contentstack/contentstack-dotnet</PackageProjectUrl>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<ReleaseVersion>2.11.0</ReleaseVersion>
<ReleaseVersion>2.12.0</ReleaseVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
15 changes: 10 additions & 5 deletions Contentstack.Core/ContentstackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
}
cnfig.Region = _options.Region;
cnfig.Branch = _options.Branch;
if (_options.Timeout != null)
{
cnfig.Timeout = _options.Timeout;
}
this.SetConfig(cnfig);
if (_options.LivePreview != null)
{
Expand Down Expand Up @@ -146,15 +150,16 @@ public ContentstackClient(ContentstackOptions options) :
/// ContentType contentType = stack.ContentType(&quot;contentType_name&quot;);
/// </code>
/// </example>
public ContentstackClient(string apiKey, string deliveryToken, string environment, string host = null, ContentstackRegion region = ContentstackRegion.US, string version = null) :
public ContentstackClient(string apiKey, string deliveryToken, string environment, string host = null, ContentstackRegion region = ContentstackRegion.US, string version = null, int timeout = 100000) :
this(new OptionsWrapper<ContentstackOptions>(new ContentstackOptions()
{
ApiKey = apiKey,
DeliveryToken = deliveryToken,
Environment = environment,
Host = host,
Region = region,
Version = version
Version = version,
Timeout = timeout
}
))
{
Expand Down Expand Up @@ -306,7 +311,7 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
try
{
HttpRequestHandler RequestHandler = new HttpRequestHandler(this);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Config.Branch);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
IList contentTypes = (IList)data["content_types"];
return contentTypes;
Expand Down Expand Up @@ -341,7 +346,7 @@ private async Task<JObject> GetLivePreviewData()
try
{
HttpRequestHandler RequestHandler = new HttpRequestHandler(this);
var outputResult = await RequestHandler.ProcessRequest(String.Format("{0}/content_types/{1}/entries/{2}", this.Config.getLivePreviewUrl(this.LivePreviewConfig), this.LivePreviewConfig.ContentTypeUID, this.LivePreviewConfig.EntryUID), headerAll, mainJson, Branch: this.Config.Branch, isLivePreview: true);
var outputResult = await RequestHandler.ProcessRequest(String.Format("{0}/content_types/{1}/entries/{2}", this.Config.getLivePreviewUrl(this.LivePreviewConfig), this.LivePreviewConfig.ContentTypeUID, this.LivePreviewConfig.EntryUID), headerAll, mainJson, Branch: this.Config.Branch, isLivePreview: true, timeout: this.Config.Timeout);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
return (JObject)data["entry"];
}
Expand Down Expand Up @@ -739,7 +744,7 @@ private async Task<SyncStack> GetResultAsync(string Init = "false", SyncType Syn
try
{
HttpRequestHandler requestHandler = new HttpRequestHandler(this);
string js = await requestHandler.ProcessRequest(_SyncUrl, _LocalHeaders, mainJson, Branch: this.Config.Branch);
string js = await requestHandler.ProcessRequest(_SyncUrl, _LocalHeaders, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout);
SyncStack stackSyncOutput = JsonConvert.DeserializeObject<SyncStack>(js);
return stackSyncOutput;
}
Expand Down
117 changes: 59 additions & 58 deletions Contentstack.Core/Internals/HttpRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Contentstack.Core.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -13,15 +13,15 @@ namespace Contentstack.Core.Internals
{
internal class HttpRequestHandler
{
ContentstackClient client
{
get; set;
ContentstackClient client
{
get; set;
}
internal HttpRequestHandler(ContentstackClient contentstackClient)
{
client = contentstackClient;
internal HttpRequestHandler(ContentstackClient contentstackClient)
{
client = contentstackClient;
}
public async Task<string> ProcessRequest(string Url, Dictionary<string, object> Headers, Dictionary<string, object> BodyJson, string FileName = null, string Branch = null, bool isLivePreview = false) {
public async Task<string> ProcessRequest(string Url, Dictionary<string, object> Headers, Dictionary<string, object> BodyJson, string FileName = null, string Branch = null, bool isLivePreview = false, int timeout = 30000) {

String queryParam = String.Join("&", BodyJson.Select(kvp => {
var value = "";
Expand All @@ -48,7 +48,8 @@ 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-dotnet/2.11.0";
request.Headers["x-user-agent"]="contentstack-delivery-dotnet/2.12.0";
request.Timeout = timeout;
if (Branch != null)
{
request.Headers["branch"] = Branch;
Expand All @@ -61,11 +62,11 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>

}
}
}

foreach (var plugin in client.Plugins)
{
request = await plugin.OnRequest(client, request);
}

foreach (var plugin in client.Plugins)
{
request = await plugin.OnRequest(client, request);
};

var serializedresult = JsonConvert.SerializeObject(BodyJson);
Expand All @@ -79,16 +80,16 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
reader = new StreamReader(response.GetResponseStream());

string responseString = await reader.ReadToEndAsync();
foreach (var plugin in client.Plugins)
{
responseString = await plugin.OnResponse(client, request, response, responseString);
foreach (var plugin in client.Plugins)
{
responseString = await plugin.OnResponse(client, request, response, responseString);
}

if (isLivePreview == false && this.client.LivePreviewConfig.Enable == true)
{
JObject data = JsonConvert.DeserializeObject<JObject>(responseString.Replace("\r\n", ""), this.client.SerializerSettings);
updateLivePreviewContent(data);
responseString = JsonConvert.SerializeObject(data);
if (isLivePreview == false && this.client.LivePreviewConfig.Enable == true)
{
JObject data = JsonConvert.DeserializeObject<JObject>(responseString.Replace("\r\n", ""), this.client.SerializerSettings);
updateLivePreviewContent(data);
responseString = JsonConvert.SerializeObject(data);
}
return responseString;
} else {
Expand All @@ -108,44 +109,44 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>

}

internal void updateLivePreviewContent(JObject response)
{
if (response.ContainsKey("uid") && response["uid"].ToString() == this.client.LivePreviewConfig.EntryUID)
{
response.Merge(this.client.LivePreviewConfig.PreviewResponse, new JsonMergeSettings()
{
MergeArrayHandling = MergeArrayHandling.Replace
});
}
else
{
foreach (var content in response)
{
if (content.Value.Type == JTokenType.Array)
{
updateArray((JArray)response[content.Key]);
}
else if (content.Value.Type == JTokenType.Object)
{
updateLivePreviewContent((JObject)response[content.Key]);
}
}
}
internal void updateLivePreviewContent(JObject response)
{
if (response.ContainsKey("uid") && response["uid"].ToString() == this.client.LivePreviewConfig.EntryUID)
{
response.Merge(this.client.LivePreviewConfig.PreviewResponse, new JsonMergeSettings()
{
MergeArrayHandling = MergeArrayHandling.Replace
});
}
else
{
foreach (var content in response)
{
if (content.Value.Type == JTokenType.Array)
{
updateArray((JArray)response[content.Key]);
}
else if (content.Value.Type == JTokenType.Object)
{
updateLivePreviewContent((JObject)response[content.Key]);
}
}
}
}

internal void updateArray(JArray array)
{
for (var i = 0; i < array.Count(); i++)
{
if (array[i].Type == JTokenType.Array)
{
updateArray((JArray)array[i]);
}
else if (array[i].Type == JTokenType.Object)
{
updateLivePreviewContent((JObject)array[i]);
}
}
internal void updateArray(JArray array)
{
for (var i = 0; i < array.Count(); i++)
{
if (array[i].Type == JTokenType.Array)
{
updateArray((JArray)array[i]);
}
else if (array[i].Type == JTokenType.Object)
{
updateLivePreviewContent((JObject)array[i]);
}
}
}
}
}
2 changes: 1 addition & 1 deletion Contentstack.Core/Models/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public async Task<Asset> Fetch()
try
{
HttpRequestHandler RequestHandler = new HttpRequestHandler(this.StackInstance);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.StackInstance.Config.Branch);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.StackInstance.Config.Branch, timeout: this.StackInstance.Config.Timeout);
JObject obj = JObject.Parse(ContentstackConvert.ToString(outputResult, "{}"));
return obj.SelectToken("$.asset").ToObject<Asset>(this.StackInstance.Serializer);
}
Expand Down
2 changes: 1 addition & 1 deletion Contentstack.Core/Models/AssetLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ private async Task<JObject> Exec()
try
{
HttpRequestHandler RequestHandler = new HttpRequestHandler(this.Stack);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Stack.Config.Branch);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Stack.Config.Branch, timeout: this.Stack.Config.Timeout);
return JObject.Parse(ContentstackConvert.ToString(outputResult, "{}"));

}
Expand Down
2 changes: 1 addition & 1 deletion Contentstack.Core/Models/ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public async Task<JObject> Fetch(Dictionary<string, object> param = null)
try
{
HttpRequestHandler RequestHandler = new HttpRequestHandler(this.StackInstance);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.StackInstance.Config.Branch);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.StackInstance.Config.Branch, timeout: this.StackInstance.Config.Timeout);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.StackInstance.SerializerSettings);
JObject contentTypes = (Newtonsoft.Json.Linq.JObject)data["content_type"];
return contentTypes;
Expand Down
2 changes: 1 addition & 1 deletion Contentstack.Core/Models/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ public async Task<T> Fetch<T>()
}

HttpRequestHandler RequestHandler = new HttpRequestHandler(this.ContentTypeInstance.StackInstance);
var outputResult = await RequestHandler.ProcessRequest(_Url, headerAll, mainJson, Branch: this.ContentTypeInstance.StackInstance.Config.Branch, isLivePreview: isLivePreview);
var outputResult = await RequestHandler.ProcessRequest(_Url, headerAll, mainJson, Branch: this.ContentTypeInstance.StackInstance.Config.Branch, isLivePreview: isLivePreview, timeout: this.ContentTypeInstance.StackInstance.Config.Timeout);
JObject obj = JObject.Parse(ContentstackConvert.ToString(outputResult, "{}"));
var serializedObject = obj.SelectToken("$.entry").ToObject<T>(this.ContentTypeInstance.StackInstance.Serializer);
if (serializedObject.GetType() == typeof(Entry))
Expand Down
2 changes: 1 addition & 1 deletion Contentstack.Core/Models/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,7 @@ private async Task<JObject> Exec()
try
{
HttpRequestHandler requestHandler = new HttpRequestHandler(this.ContentTypeInstance.StackInstance);
var outputResult = await requestHandler.ProcessRequest(_Url, headerAll, mainJson, Branch: this.ContentTypeInstance.StackInstance.Config.Branch, isLivePreview: isLivePreview);
var outputResult = await requestHandler.ProcessRequest(_Url, headerAll, mainJson, Branch: this.ContentTypeInstance.StackInstance.Config.Branch, isLivePreview: isLivePreview, timeout: this.ContentTypeInstance.StackInstance.Config.Timeout);
return JObject.Parse(ContentstackConvert.ToString(outputResult, "{}"));
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion Contentstack.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ Global
$0.XmlFormattingPolicy = $9
$9.scope = application/xml
$0.StandardHeader = $10
version = 2.11.0
version = 2.12.0
EndGlobalSection
EndGlobal

0 comments on commit 78450ef

Please sign in to comment.