Skip to content

Commit

Permalink
Merge pull request #34 from contentstack/next
Browse files Browse the repository at this point in the history
Proxy support and Default Timeout setting
  • Loading branch information
nadeem-cs authored Apr 5, 2024
2 parents f6e3bc7 + d407d56 commit 7b76b9f
Show file tree
Hide file tree
Showing 14 changed files with 418 additions and 374 deletions.
8 changes: 5 additions & 3 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
fileignoreconfig:
- filename: Contentstack.Core/ContentstackClient.cs
checksum: f960fff17b452a71e39e51ae8b92759dd1e30638d379155185e3e8bd63f407a4
- filename: Contentstack.Core/Internals/HttpRequestHandler.cs
checksum: 93c1659f3bc7527956f0fd12db46441297fac3a4366d273bcbb3425d2351300e
checksum: 93c1659f3bc7527956f0fd12db46441297fac3a4366d273bcbb3425d2351300e
- filename: Contentstack.Core/Models/Entry.cs
checksum: bc0da69dde3bebecf09aa319aa961f82d7637e200bb8539821fa6116c0129f1b
- filename: Contentstack.Core/ContentstackClient.cs
checksum: 1cb7c9bd62881ae71406449c948b1e85aa865d0c7191013f77f9b9a60df700d9
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### Version: 2.13.0
#### Date: April-02-2024

##### New Feature:
- Proxy support added

### Version: 2.12.0
#### Date: Feb-01-2024

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.12.0</ReleaseVersion>
<ReleaseVersion>$(Version)</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Contentstack.Core.Tests/StackConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static ContentstackClient GetStack()
DeliveryToken = delivery_token,
Environment = environment,
Host = host,
Timeout = 4500
Timeout = 4500,
Proxy = new System.Net.WebProxy("http://example.com:8080")
};

ContentstackClient contentstackClient = new ContentstackClient(new OptionsWrapper<Configuration.ContentstackOptions>(contentstackOptions));
Expand Down
87 changes: 46 additions & 41 deletions Contentstack.Core/Configuration/Config.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Contentstack.Core.Internals;

using System.Net;
using Contentstack.Core.Internals;

namespace Contentstack.Core.Configuration
{
internal class Config
Expand All @@ -17,6 +15,7 @@ internal class Config
private string _Environment;
private string _Branch;
private int _Timeout;
private WebProxy _proxy;
#endregion

#region Public Properties
Expand All @@ -32,7 +31,7 @@ public string Port {
}

public string Protocol {
get { return this._Protocol ?? "https"; }
get { return this._Protocol ?? "https"; }
set { this._Protocol = value; }
}

Expand Down Expand Up @@ -67,56 +66,62 @@ public int Timeout
set { this._Timeout = value; }
}

public WebProxy Proxy
{
get { return this._proxy; }
set { this._proxy = value; }
}

public string BaseUrl
{
get
{
string BaseURL = string.Format("{0}://{1}{2}/{3}",
this.Protocol.Trim('/').Trim('\\'),
regionCode(),
this.Host.Trim('/').Trim('\\'),
string BaseURL = string.Format("{0}://{1}{2}/{3}",
this.Protocol.Trim('/').Trim('\\'),
regionCode(),
this.Host.Trim('/').Trim('\\'),
this.Version.Trim('/').Trim('\\'));
return BaseURL;
}
}

}

#endregion


#region Internal
internal string getLivePreviewUrl(LivePreviewConfig livePreviewConfig)
{

return string.Format("{0}://{1}/{2}",
this.Protocol.Trim('/').Trim('\\'),
livePreviewConfig.Host.Trim('/').Trim('\\'),
this.Version.Trim('/').Trim('\\'));
internal string getLivePreviewUrl(LivePreviewConfig livePreviewConfig)
{

return string.Format("{0}://{1}/{2}",
this.Protocol.Trim('/').Trim('\\'),
livePreviewConfig.Host.Trim('/').Trim('\\'),
this.Version.Trim('/').Trim('\\'));
}

internal string getBaseUrl (LivePreviewConfig livePreviewConfig, string contentTypeUID)
{
if (livePreviewConfig != null && livePreviewConfig.Enable && livePreviewConfig.ContentTypeUID == contentTypeUID)
{
return getLivePreviewUrl(livePreviewConfig);
}
return BaseUrl;
internal string getBaseUrl (LivePreviewConfig livePreviewConfig, string contentTypeUID)
{
if (livePreviewConfig != null && livePreviewConfig.Enable && livePreviewConfig.ContentTypeUID == contentTypeUID)
{
return getLivePreviewUrl(livePreviewConfig);
}
return BaseUrl;
}

internal string regionCode()
{
if (Region == ContentstackRegion.US) return "";
ContentstackRegionCode[] regionCodes = Enum.GetValues(typeof(ContentstackRegionCode)).Cast<ContentstackRegionCode>().ToArray();
return string.Format("{0}-", regionCodes[(int)Region].ToString().Replace("_", "-"));
internal string regionCode()
{
if (Region == ContentstackRegion.US) return "";
ContentstackRegionCode[] regionCodes = Enum.GetValues(typeof(ContentstackRegionCode)).Cast<ContentstackRegionCode>().ToArray();
return string.Format("{0}-", regionCodes[(int)Region].ToString().Replace("_", "-"));
}

internal string HostURL
{
get
{
if (Region == ContentstackRegion.EU || Region == ContentstackRegion.AZURE_EU || Region == ContentstackRegion.AZURE_NA)
return "cdn.contentstack.com";
return "cdn.contentstack.io";
}
}
internal string HostURL
{
get
{
if (Region == ContentstackRegion.EU || Region == ContentstackRegion.AZURE_EU || Region == ContentstackRegion.AZURE_NA)
return "cdn.contentstack.com";
return "cdn.contentstack.io";
}
}
#endregion
}
}
11 changes: 11 additions & 0 deletions Contentstack.Core/Configuration/ContentstackOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using Contentstack.Core.Internals;

namespace Contentstack.Core.Configuration
Expand Down Expand Up @@ -38,6 +39,11 @@ public class ContentstackOptions
/// </summary>
public string Host { get; set; }

/// <summary>
/// The Proxy used when communicating with the Contentstack API.
/// </summary>
public WebProxy Proxy { get; set; }

/// <summary>
/// The Region used to set region for the Contentstack API.
/// </summary>
Expand All @@ -63,6 +69,11 @@ public class ContentstackOptions
/// The Timeout used to set Timeout for the Contentstack API.
/// </summary>
public int Timeout { get; set; }

public ContentstackOptions()
{
Timeout = 30000; // Set default value
}
}

internal class ContentstackRegionConverter : TypeConverter
Expand Down
12 changes: 9 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.12.0</PackageVersion>
<PackageVersion>$(Version)</PackageVersion>
<Owners>Contentstack</Owners>
<PackageReleaseNotes>Reference in entry Live preview support added</PackageReleaseNotes>
<Copyright>Copyright © 2012-2024 Contentstack. All Rights Reserved</Copyright>
<PackOnBuild>true</PackOnBuild>
<PackageTags>v2.12.0</PackageTags>
<PackageTags>v$(Version)</PackageTags>
<PackageProjectUrl>https://github.com/contentstack/contentstack-dotnet</PackageProjectUrl>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<ReleaseVersion>2.12.0</ReleaseVersion>
<ReleaseVersion>$(Version)</ReleaseVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand All @@ -36,6 +36,12 @@
</ItemGroup>
<ItemGroup>
<None Include="LICENSE.txt" Pack="true" PackagePath="LICENSE.txt" />
<None Include="..\CHANGELOG.md">
<Link>CHANGELOG.md</Link>
</None>
<None Include="..\README.md">
<Link>README.md</Link>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="Internals\" />
Expand Down
26 changes: 14 additions & 12 deletions Contentstack.Core/ContentstackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
{
cnfig.Version = _options.Version;
}
cnfig.Region = _options.Region;
cnfig.Branch = _options.Branch;
if (_options.Timeout != null)
if (_options.Proxy != null)
{
cnfig.Timeout = _options.Timeout;
cnfig.Proxy = _options.Proxy;
}
cnfig.Region = _options.Region;
cnfig.Branch = _options.Branch;
cnfig.Timeout = _options.Timeout;
this.SetConfig(cnfig);
if (_options.LivePreview != null)
{
Expand Down Expand Up @@ -150,7 +151,7 @@ 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, int timeout = 100000) :
public ContentstackClient(string apiKey, string deliveryToken, string environment, string host = null, ContentstackRegion region = ContentstackRegion.US, string version = null, int? timeout = null, WebProxy proxy = null) :
this(new OptionsWrapper<ContentstackOptions>(new ContentstackOptions()
{
ApiKey = apiKey,
Expand All @@ -159,7 +160,8 @@ public ContentstackClient(string apiKey, string deliveryToken, string environmen
Host = host,
Region = region,
Version = version,
Timeout = timeout
Timeout = timeout ?? 30000,
Proxy = proxy
}
))
{
Expand Down Expand Up @@ -270,7 +272,7 @@ internal Asset Asset()
/// <example>
/// <code>
/// ContentstackClient stack = new ContentstackClinet(&quot;api_key&quot;, &quot;delivery_token&quot;, &quot;environment&quot;);
/// var param = new Dictionary&lt;string, object&gt;();
/// var param = new Dictionary&lt;string, object&gt;();
/// param.Add("include_global_field_schema",true);
/// param.Add("limit", 10);
/// param.Add("skip", 10);
Expand Down Expand Up @@ -304,14 +306,14 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
if (param != null && param.Count() > 0)
{
foreach (var kvp in param)
{
mainJson.Add(kvp.Key, kvp.Value);
{
mainJson.Add(kvp.Key, kvp.Value);
}
}
try
{
HttpRequestHandler RequestHandler = new HttpRequestHandler(this);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout);
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout, proxy: this.Config.Proxy);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
IList contentTypes = (IList)data["content_types"];
return contentTypes;
Expand Down Expand Up @@ -346,7 +348,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, timeout: this.Config.Timeout);
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, proxy: this.Config.Proxy);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
return (JObject)data["entry"];
}
Expand Down Expand Up @@ -744,7 +746,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, timeout: this.Config.Timeout);
string js = await requestHandler.ProcessRequest(_SyncUrl, _LocalHeaders, mainJson, Branch: this.Config.Branch, timeout: this.Config.Timeout, proxy: this.Config.Proxy);
SyncStack stackSyncOutput = JsonConvert.DeserializeObject<SyncStack>(js);
return stackSyncOutput;
}
Expand Down
16 changes: 11 additions & 5 deletions Contentstack.Core/Internals/HttpRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Contentstack.Core.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Contentstack.Core.Internals
{
Expand All @@ -21,7 +20,8 @@ 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, int timeout = 30000) {
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, WebProxy proxy = null)
{

String queryParam = String.Join("&", BodyJson.Select(kvp => {
var value = "";
Expand Down Expand Up @@ -50,6 +50,12 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
request.ContentType = "application/json";
request.Headers["x-user-agent"]="contentstack-delivery-dotnet/2.12.0";
request.Timeout = timeout;

if (proxy != null)
{
request.Proxy = proxy;
}

if (Branch != null)
{
request.Headers["branch"] = Branch;
Expand Down
Loading

0 comments on commit 7b76b9f

Please sign in to comment.