Skip to content
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
10 changes: 5 additions & 5 deletions contentstack.model.generator.tests/ContentstackClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ public void Constructor_ShouldHandleNullOptions()
}

[Fact]
public void Constructor_ShouldSetSerializerSettings()
public void Constructor_ShouldSetSerializerOptions()
{

var options = new ContentstackOptions
{
ApiKey = "test_api_key",
Host = "api.contentstack.io"
};


var client = new ContentstackClient(options);

Assert.NotNull(client.SerializerSettings);

Assert.NotNull(client.SerializerOptions);
}

[Theory]
Expand Down
62 changes: 30 additions & 32 deletions contentstack.model.generator/CMA/ContentstackClient.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Collections;
using Contentstack.Model.Generator.Model;
using contentstack.model.generator.Model;

namespace contentstack.CMA
{
public class ContentstackClient
{
public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings();
public JsonSerializerOptions SerializerOptions { get; set; } = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
AllowTrailingCommas = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
};

#region Internal Variables

Expand Down Expand Up @@ -131,19 +137,18 @@ internal static ContentstackException GetContentstackException(Exception ex)
using (var reader = new StreamReader(stream))
{
errorMessage = reader.ReadToEnd();
JObject data = JObject.Parse(errorMessage.Replace("\r\n", ""));

JToken token = data["error_code"];
if (token != null)
errorCode = token.Value<int>();
var node = JsonNode.Parse(errorMessage.Replace("\r\n", ""));
if (node is JsonObject data)
{
if (data["error_code"] is JsonValue ec)
errorCode = ec.GetValue<int>();

token = data["error_message"];
if (token != null)
errorMessage = token.Value<string>();
if (data["error_message"] is JsonValue em)
errorMessage = em.GetValue<string>();

token = data["errors"];
if (token != null)
errors = token.ToObject<Dictionary<string, object>>();
if (data["errors"] is JsonObject errs)
errors = JsonSerializer.Deserialize<Dictionary<string, object>>(errs.ToJsonString());
}

var response = exResp as HttpWebResponse;
if (response != null)
Expand Down Expand Up @@ -206,11 +211,8 @@ public async Task<StackResponse> GetStack()
{
HttpRequestHandler RequestHandler = new HttpRequestHandler();
var outputResult = await RequestHandler.ProcessRequest(_StackUrl, headers, mainJson);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
SerializerSettings.DateFormatString = "yyyy-MM-dd";
var stack = data["stack"];
var stackJson = JsonConvert.SerializeObject(stack);
return JsonConvert.DeserializeObject<StackResponse>(stackJson, this.SerializerSettings);
var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject();
return JsonSerializer.Deserialize<StackResponse>(root["stack"]!.ToJsonString(), this.SerializerOptions);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -245,13 +247,11 @@ public async Task<ContentstackResponse> GetContentTypes(int skip = 0)
{
HttpRequestHandler RequestHandler = new HttpRequestHandler();
var outputResult = await RequestHandler.ProcessRequest(_Url, headers, mainJson);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
IList contentTypes = (IList)data["content_types"];
var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject();
ContentstackResponse contentstackResponse = new ContentstackResponse();
var ContentTypeJson = JsonConvert.SerializeObject(contentTypes);
contentstackResponse.listContentTypes = JsonConvert.DeserializeObject<List<Contenttype>>(ContentTypeJson);
if (data["count"] != null) {
contentstackResponse.Count = (int)data["count"];
contentstackResponse.listContentTypes = JsonSerializer.Deserialize<List<Contenttype>>(root["content_types"]!.ToJsonString(), this.SerializerOptions);
if (root["count"] is JsonValue count) {
contentstackResponse.Count = count.GetValue<int>();
}
return contentstackResponse;
}
Expand Down Expand Up @@ -286,13 +286,11 @@ public async Task<ContentstackResponse> GetGlobalFields(int skip = 0)
{
HttpRequestHandler RequestHandler = new HttpRequestHandler();
var outputResult = await RequestHandler.ProcessRequest(_GlobalFieldsUrl, headers, mainJson);
JObject data = JsonConvert.DeserializeObject<JObject>(outputResult.Replace("\r\n", ""), this.SerializerSettings);
IList globalFields = (IList)data["global_fields"];
var root = JsonNode.Parse(outputResult.Replace("\r\n", ""))!.AsObject();
ContentstackResponse contentstackResponse = new ContentstackResponse();
var ContentTypeJson = JsonConvert.SerializeObject(globalFields);
contentstackResponse.listContentTypes = JsonConvert.DeserializeObject<List<Contenttype>>(ContentTypeJson);
if (data["count"] != null) {
contentstackResponse.Count = (int)data["count"];
contentstackResponse.listContentTypes = JsonSerializer.Deserialize<List<Contenttype>>(root["global_fields"]!.ToJsonString(), this.SerializerOptions);
if (root["count"] is JsonValue count) {
contentstackResponse.Count = count.GetValue<int>();
}
return contentstackResponse;
}
Expand Down
Loading