Skip to content

Commit fc41202

Browse files
committed
Feat: Added Support for Global Field
1 parent 52b7d84 commit fc41202

File tree

7 files changed

+681
-13
lines changed

7 files changed

+681
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### Version: 2.22.0
2+
#### Date: March-03-2025
3+
4+
##### Feat:
5+
- Added Support for Global Fields
6+
17
### Version: 2.21.0
28
#### Date: March-03-2025
39

Contentstack.Core.Tests/ContentTypeTest.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Contentstack.Core.Models;
44
using System.Threading.Tasks;
55
using System.Collections.Generic;
6+
using Newtonsoft.Json.Linq;
67

78
namespace Contentstack.Core.Tests
89
{
@@ -79,5 +80,131 @@ public async Task GetContentTypesIncludeGlobalFields()
7980

8081
}
8182
}
83+
84+
[Fact]
85+
public async Task FetchGlobalFieldSchema()
86+
{
87+
string globalFieldUid = "global_field_uid";
88+
GlobalField globalField = client.GlobalField(globalFieldUid);
89+
90+
var result = await globalField.Fetch();
91+
Assert.NotNull(result);
92+
Assert.True(result.HasValues, "GlobalField.Fetch() did not return expected schema.");
93+
}
94+
95+
[Fact]
96+
public async Task FetchGlobalFieldSchema_InvalidUid_ThrowsOrReturnsNull()
97+
{
98+
string invalidUid = "invalid_uid";
99+
GlobalField globalField = client.GlobalField(invalidUid);
100+
await Assert.ThrowsAnyAsync<Exception>(async () => await globalField.Fetch());
101+
}
102+
103+
[Fact]
104+
public async Task FetchGlobalFieldSchema_WithParameters_ReturnsSchema()
105+
{
106+
string globalFieldUid = "global_field_uid";
107+
GlobalField globalField = client.GlobalField(globalFieldUid);
108+
var param = new Dictionary<string, object> { { "include_global_field_schema", true } };
109+
var result = await globalField.Fetch(param);
110+
Assert.NotNull(result);
111+
Assert.True(result.HasValues, "GlobalField.Fetch() with params did not return expected schema.");
112+
}
113+
114+
[Fact]
115+
public void SetAndRemoveHeader_WorksCorrectly()
116+
{
117+
string globalFieldUid = "global_field_uid";
118+
GlobalField globalField = client.GlobalField(globalFieldUid);
119+
globalField.SetHeader("custom_key", "custom_value");
120+
globalField.RemoveHeader("custom_key");
121+
Assert.True(true);
122+
}
123+
124+
[Fact]
125+
public async Task FetchGlobalFieldSchema_WithCustomHeader()
126+
{
127+
string globalFieldUid = "global_field_uid";
128+
GlobalField globalField = client.GlobalField(globalFieldUid);
129+
globalField.SetHeader("custom_key", "custom_value");
130+
var result = await globalField.Fetch();
131+
Assert.NotNull(result);
132+
}
133+
134+
[Fact]
135+
public async Task FetchGlobalFieldSchema_NullParameters_Succeeds()
136+
{
137+
string globalFieldUid = "global_field_uid";
138+
GlobalField globalField = client.GlobalField(globalFieldUid);
139+
var result = await globalField.Fetch(null);
140+
Assert.NotNull(result);
141+
}
142+
143+
[Fact]
144+
public void GlobalField_EmptyUid_Throws()
145+
{
146+
Assert.Throws<ArgumentNullException>(() => {
147+
GlobalField globalField = client.GlobalField("");
148+
});
149+
}
150+
151+
[Fact]
152+
public async Task GlobalFieldQuery_Find_ReturnsArray()
153+
{
154+
var query = client.GlobalFieldQuery();
155+
var result = await query.Find();
156+
157+
Assert.NotNull(result);
158+
}
159+
160+
[Fact]
161+
public async Task GlobalFieldQuery_Find_WithParameters_ReturnsArray()
162+
{
163+
var query = client.GlobalFieldQuery();
164+
var param = new Dictionary<string, object> { { "include_global_field_schema", true } };
165+
var result = await query.Find(param);
166+
Assert.NotNull(result);
167+
}
168+
169+
[Fact]
170+
public async Task GlobalFieldQuery_Find_WithSkipAndLimit_ReturnsArray()
171+
{
172+
var query = client.GlobalFieldQuery();
173+
var param = new Dictionary<string, object> { { "skip", 1 }, { "limit", 2 } };
174+
var result = await query.Find(param);
175+
Assert.Empty(result["global_fields"]);
176+
}
177+
178+
[Fact]
179+
public void GlobalFieldQuery_IncludeBranch_SetsQueryParam()
180+
{
181+
var query = client.GlobalFieldQuery();
182+
var result = query.IncludeBranch();
183+
Assert.NotNull(result);
184+
Assert.Equal(query, result);
185+
}
186+
187+
[Fact]
188+
public void GlobalFieldQuery_IncludeGlobalFieldSchema_SetsQueryParam()
189+
{
190+
var query = client.GlobalFieldQuery();
191+
var result = query.IncludeGlobalFieldSchema();
192+
Assert.NotNull(result);
193+
Assert.Equal(query, result);
194+
}
195+
196+
[Fact]
197+
public async Task GlobalFieldQuery_Find_InvalidParams_ThrowsOrReturnsEmpty()
198+
{
199+
var query = client.GlobalFieldQuery();
200+
var invalidParams = new Dictionary<string, object> { { "invalid_param", true } };
201+
202+
var result = await query.Find(invalidParams);
203+
204+
Assert.NotNull(result);
205+
Assert.IsType<JObject>(result);
206+
var globalFields = result["global_fields"] as JArray;
207+
Assert.NotNull(globalFields);
208+
}
82209
}
83210
}

Contentstack.Core/ContentstackClient.cs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
8282
if (_options.AccessToken != null)
8383
{
8484
this.SetHeader("access_token", _options.AccessToken);
85-
} else if (_options.DeliveryToken != null)
85+
}
86+
else if (_options.DeliveryToken != null)
8687
{
8788
this.SetHeader("access_token", _options.DeliveryToken);
8889
}
89-
if(_options.EarlyAccessHeader !=null)
90+
if (_options.EarlyAccessHeader != null)
9091
{
9192
this.SetHeader("x-header-ea", string.Join(",", _options.EarlyAccessHeader));
9293
}
@@ -128,7 +129,9 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
128129
else if (this.LivePreviewConfig.PreviewToken != null)
129130
{
130131
this.LivePreviewConfig.Host = "rest-preview.contentstack.com";
131-
} else {
132+
}
133+
else
134+
{
132135
throw new InvalidOperationException("Add PreviewToken or ManagementToken in LivePreviewConfig");
133136
}
134137
}
@@ -337,7 +340,7 @@ public async Task<IList> GetContentTypes(Dictionary<string, object> param = null
337340

338341
private async Task<JObject> GetLivePreviewData()
339342
{
340-
343+
341344
Dictionary<String, object> headerAll = new Dictionary<string, object>();
342345
Dictionary<string, object> mainJson = new Dictionary<string, object>();
343346
Dictionary<String, Object> headers = GetHeader(_LocalHeaders);
@@ -354,12 +357,17 @@ private async Task<JObject> GetLivePreviewData()
354357
}
355358
}
356359
mainJson.Add("live_preview", this.LivePreviewConfig.LivePreview ?? "init");
357-
358-
if (!string.IsNullOrEmpty(this.LivePreviewConfig.ManagementToken)) {
360+
361+
if (!string.IsNullOrEmpty(this.LivePreviewConfig.ManagementToken))
362+
{
359363
headerAll["authorization"] = this.LivePreviewConfig.ManagementToken;
360-
} else if (!string.IsNullOrEmpty(this.LivePreviewConfig.PreviewToken)) {
364+
}
365+
else if (!string.IsNullOrEmpty(this.LivePreviewConfig.PreviewToken))
366+
{
361367
headerAll["preview_token"] = this.LivePreviewConfig.PreviewToken;
362-
} else {
368+
}
369+
else
370+
{
363371
throw new InvalidOperationException("Either ManagementToken or PreviewToken is required in LivePreviewConfig");
364372
}
365373

@@ -405,6 +413,21 @@ public ContentType ContentType(String contentTypeName)
405413
return contentType;
406414
}
407415

416+
417+
public GlobalField GlobalField(String globalFieldName)
418+
{
419+
GlobalField globalField = new GlobalField(globalFieldName);
420+
globalField.SetStackInstance(this);
421+
return globalField;
422+
}
423+
424+
public GlobalFieldQuery GlobalFieldQuery()
425+
{
426+
GlobalFieldQuery globalField = new GlobalFieldQuery();
427+
globalField.SetStackInstance(this);
428+
return globalField;
429+
}
430+
408431
/// <summary>
409432
/// Represents a Asset. Creates Asset Instance.
410433
/// </summary>
@@ -592,13 +615,15 @@ public async Task LivePreviewQueryAsync(Dictionary<string, string> query)
592615
string hash = null;
593616
query.TryGetValue("live_preview", out hash);
594617
this.LivePreviewConfig.LivePreview = hash;
595-
}
596-
if (query.Keys.Contains("release_id")) {
618+
}
619+
if (query.Keys.Contains("release_id"))
620+
{
597621
string ReleaseId = null;
598622
query.TryGetValue("release_id", out ReleaseId);
599623
this.LivePreviewConfig.ReleaseId = ReleaseId;
600624
}
601-
if (query.Keys.Contains("preview_timestamp")) {
625+
if (query.Keys.Contains("preview_timestamp"))
626+
{
602627
string PreviewTimestamp = null;
603628
query.TryGetValue("preview_timestamp", out PreviewTimestamp);
604629
this.LivePreviewConfig.PreviewTimestamp = PreviewTimestamp;

Contentstack.Core/Internals/HttpRequestHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
4848
var request = (HttpWebRequest)WebRequest.Create(uri);
4949
request.Method = "GET";
5050
request.ContentType = "application/json";
51-
request.Headers["x-user-agent"]="contentstack-delivery-dotnet/2.21.0";
51+
request.Headers["x-user-agent"]="contentstack-delivery-dotnet/2.22.0";
5252
request.Timeout = timeout;
5353

5454
if (proxy != null)

0 commit comments

Comments
 (0)