Skip to content
Draft
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
8 changes: 3 additions & 5 deletions contentstack.model.generator/CMA/HTTPRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Newtonsoft.Json;
using System;
using System;
using System.Text.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -14,12 +14,10 @@ namespace contentstack.CMA
internal class HttpRequestHandler
{
private readonly HttpClient _httpClient;
private readonly JsonSerializer _serializer;

public HttpRequestHandler()
{
_httpClient = new HttpClient();
_serializer = new JsonSerializer();
}

public async Task<string> ProcessRequest(string Url, Dictionary<string, object> Headers, Dictionary<string, object> BodyJson, string FileName = null)
Expand All @@ -37,7 +35,7 @@ public async Task<string> ProcessRequest(string Url, Dictionary<string, object>
return value;
}
else if (kvp.Value is Dictionary<string, object>)
value = JsonConvert.SerializeObject(kvp.Value);
value = JsonSerializer.Serialize(kvp.Value);
else
return String.Format("{0}={1}", kvp.Key, kvp.Value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Generic;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using System.Text.Json;

namespace contentstack.CMA.Http
{
Expand All @@ -15,7 +15,7 @@ internal class ContentstackHttpRequest : IHttpRequest
private bool _disposed = false;
private readonly HttpClient _httpClient;
private readonly HttpRequestMessage _request;
private readonly JsonSerializer _serializer;
private readonly JsonSerializerOptions _options;
#endregion

#region Public
Expand Down Expand Up @@ -52,10 +52,10 @@ public HttpRequestMessage Request
#endregion

#region Constructor
internal ContentstackHttpRequest(HttpClient httpClient, JsonSerializer serializer)
internal ContentstackHttpRequest(HttpClient httpClient, JsonSerializerOptions options)
{
_httpClient = httpClient;
_serializer = serializer;
_options = options;
_request = new HttpRequestMessage();
}
#endregion
Expand Down Expand Up @@ -124,14 +124,14 @@ public async Task<IResponse> GetResponseAsync()

if (responseMessage.StatusCode >= HttpStatusCode.Ambiguous &&
responseMessage.StatusCode < HttpStatusCode.BadRequest)
return new ContentstackResponse(responseMessage, _serializer);
return new ContentstackResponse(responseMessage, _options);

if (!responseMessage.IsSuccessStatusCode)
{
throw new HttpRequestException($"HTTP request failed with status code: {responseMessage.StatusCode}");
}

return new ContentstackResponse(responseMessage, _serializer);
return new ContentstackResponse(responseMessage, _options);
}
catch (HttpRequestException httpException)
{
Expand Down
20 changes: 10 additions & 10 deletions contentstack.model.generator/CMA/Http/ContentstackResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace contentstack.CMA.Http
{
Expand All @@ -20,7 +20,7 @@ public class ContentstackResponse : IResponse, IDisposable
Dictionary<string, string> _headers;
HashSet<string> _headerNamesSet;
private readonly HttpResponseMessage _response;
private readonly JsonSerializer _serializer;
private readonly JsonSerializerOptions _options;

#region Public
/// <summary>
Expand Down Expand Up @@ -126,10 +126,10 @@ private void CopyHeaderValues(HttpResponseMessage response)
}
#endregion

internal ContentstackResponse(HttpResponseMessage response, JsonSerializer serializer)
internal ContentstackResponse(HttpResponseMessage response, JsonSerializerOptions options)
{
_response = response;
_serializer = serializer;
_options = options;

this.StatusCode = response.StatusCode;
this.IsSuccessStatusCode = response.IsSuccessStatusCode;
Expand All @@ -146,11 +146,11 @@ internal ContentstackResponse(HttpResponseMessage response, JsonSerializer seria
/// <summary>
/// Json Object format response.
/// </summary>
/// <returns>The JObject.</returns>
public JObject OpenJObjectResponse()
/// <returns>The JsonObject.</returns>
public JsonObject OpenJsonObjectResponse()
{
ThrowIfDisposed();
return JObject.Parse(OpenResponse());
return JsonNode.Parse(OpenResponse())!.AsObject();
}

/// <summary>
Expand All @@ -171,8 +171,8 @@ public string OpenResponse()
public TResponse OpenTResponse<TResponse>()
{
ThrowIfDisposed();
JObject jObject = OpenJObjectResponse();
return jObject.ToObject<TResponse>(_serializer);
JsonObject jObject = OpenJsonObjectResponse();
return JsonSerializer.Deserialize<TResponse>(jObject.ToJsonString(), _options)!;
}


Expand Down
4 changes: 2 additions & 2 deletions contentstack.model.generator/CMA/Http/IResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;

namespace contentstack.CMA.Http
{
Expand All @@ -19,7 +19,7 @@ public interface IResponse

string OpenResponse();

JObject OpenJObjectResponse();
JsonObject OpenJsonObjectResponse();

TResponse OpenTResponse<TResponse>();
}
Expand Down
Loading