From 0ecb4e3c2f8789542fcb6d7b71a370028ce88ef9 Mon Sep 17 00:00:00 2001 From: Srdjan Rudic Date: Mon, 18 Nov 2024 15:26:32 +0100 Subject: [PATCH 1/4] Upgraded target framework to .NET 8.0. Updated NuGet packages. --- Mailjet.Client/Mailjet.Client.csproj | 4 ++-- .../Mailjet.ConsoleApplication.csproj | 4 ++-- Mailjet.Tests/Mailjet.Tests.csproj | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Mailjet.Client/Mailjet.Client.csproj b/Mailjet.Client/Mailjet.Client.csproj index 4b37ff8..b4215a7 100644 --- a/Mailjet.Client/Mailjet.Client.csproj +++ b/Mailjet.Client/Mailjet.Client.csproj @@ -1,7 +1,7 @@  - netstandard1.1 + net8.0 False 3.0.0 @@ -36,7 +36,7 @@ - + diff --git a/Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj b/Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj index dff07fa..c8594eb 100644 --- a/Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj +++ b/Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj @@ -2,11 +2,11 @@ Exe - netcoreapp3.1 + net8.0 - + diff --git a/Mailjet.Tests/Mailjet.Tests.csproj b/Mailjet.Tests/Mailjet.Tests.csproj index d85bdca..92b4d07 100644 --- a/Mailjet.Tests/Mailjet.Tests.csproj +++ b/Mailjet.Tests/Mailjet.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net8.0 @@ -16,11 +16,11 @@ - - - - - + + + + + From ac8c86899b54edcc69d51026c36f53bdef8ab88b Mon Sep 17 00:00:00 2001 From: Srdjan Rudic Date: Mon, 18 Nov 2024 15:28:39 +0100 Subject: [PATCH 2/4] Replaced Newtonsoft.Json with System.Text.Json --- .../Exceptions/MailjetServerException.cs | 4 +-- Mailjet.Client/Helpers/HttpContentHelper.cs | 20 +++++------ Mailjet.Client/Mailjet.Client.csproj | 4 --- Mailjet.Client/MailjetClient.cs | 36 +++++++++---------- Mailjet.Client/MailjetRequest.cs | 17 ++++----- Mailjet.Client/MailjetResponse.cs | 33 +++++++++++------ .../Mailjet.ConsoleApplication.csproj | 4 --- Mailjet.Tests/HttpContentHelperTests.cs | 11 +++--- .../Integration/ContactsIntegrationTests.cs | 16 ++++----- .../SendTransactionalEmailIntegrationTests.cs | 7 ++-- .../Integration/TemplateIntegrationTests.cs | 18 +++++----- Mailjet.Tests/Mailjet.Tests.csproj | 1 - Mailjet.Tests/MailjetClientTests.cs | 36 +++++++++---------- .../SendTransactionalEmailAsyncTests.cs | 8 ++--- 14 files changed, 107 insertions(+), 108 deletions(-) diff --git a/Mailjet.Client/Exceptions/MailjetServerException.cs b/Mailjet.Client/Exceptions/MailjetServerException.cs index 271cf1d..1f9fc46 100644 --- a/Mailjet.Client/Exceptions/MailjetServerException.cs +++ b/Mailjet.Client/Exceptions/MailjetServerException.cs @@ -1,6 +1,4 @@ -using System; - -namespace Mailjet.Client.Exceptions +namespace Mailjet.Client.Exceptions { public class MailjetServerException : MailjetException { diff --git a/Mailjet.Client/Helpers/HttpContentHelper.cs b/Mailjet.Client/Helpers/HttpContentHelper.cs index 4ba285c..a93cc8a 100644 --- a/Mailjet.Client/Helpers/HttpContentHelper.cs +++ b/Mailjet.Client/Helpers/HttpContentHelper.cs @@ -1,13 +1,13 @@ -using Newtonsoft.Json.Linq; -using System.Net; +using System.Net; using System.Net.Http; +using System.Text.Json.Nodes; using System.Threading.Tasks; namespace Mailjet.Client.Helpers { public static class HttpContentHelper { - public static async Task GetContentAsync(HttpResponseMessage responseMessage) + public static async Task GetContentAsync(HttpResponseMessage responseMessage) { string cnt = null; @@ -16,30 +16,30 @@ public static async Task GetContentAsync(HttpResponseMessage responseMe cnt = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); } - JObject content; + JsonObject content; if (!string.IsNullOrEmpty(cnt) && responseMessage.Content.Headers.ContentType.MediaType == MailjetConstants.JsonMediaType) { - content = JObject.Parse(cnt); + content = JsonObject.Parse(cnt).AsObject(); } else { - content = new JObject(); - content.Add("StatusCode", new JValue((int)responseMessage.StatusCode)); + content = new JsonObject(); + content.Add("StatusCode", JsonValue.Create((int)responseMessage.StatusCode)); } if (!responseMessage.IsSuccessStatusCode && !content.ContainsKey(MailjetConstants.ErrorInfo)) { if (responseMessage.StatusCode == ((HttpStatusCode)429)) { - content.Add(MailjetConstants.ErrorInfo, new JValue(MailjetConstants.TooManyRequestsMessage)); + content.Add(MailjetConstants.ErrorInfo, JsonValue.Create(MailjetConstants.TooManyRequestsMessage)); } else if (responseMessage.StatusCode == HttpStatusCode.InternalServerError) { - content.Add(MailjetConstants.ErrorInfo, new JValue(MailjetConstants.InternalServerErrorGeneralMessage)); + content.Add(MailjetConstants.ErrorInfo, JsonValue.Create(MailjetConstants.InternalServerErrorGeneralMessage)); } else { - content.Add(MailjetConstants.ErrorInfo, new JValue(responseMessage.ReasonPhrase)); + content.Add(MailjetConstants.ErrorInfo, JsonValue.Create(responseMessage.ReasonPhrase)); } } diff --git a/Mailjet.Client/Mailjet.Client.csproj b/Mailjet.Client/Mailjet.Client.csproj index b4215a7..0cdf081 100644 --- a/Mailjet.Client/Mailjet.Client.csproj +++ b/Mailjet.Client/Mailjet.Client.csproj @@ -35,8 +35,4 @@ true - - - - diff --git a/Mailjet.Client/MailjetClient.cs b/Mailjet.Client/MailjetClient.cs index 147e32a..b8c6f0b 100644 --- a/Mailjet.Client/MailjetClient.cs +++ b/Mailjet.Client/MailjetClient.cs @@ -3,13 +3,14 @@ using Mailjet.Client.Resources; using Mailjet.Client.TransactionalEmails; using Mailjet.Client.TransactionalEmails.Response; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace Mailjet.Client @@ -19,15 +20,6 @@ namespace Mailjet.Client /// public class MailjetClient : IMailjetClient { - private static readonly JsonSerializer Serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings - { - DefaultValueHandling = DefaultValueHandling.Ignore, - Converters = new List - { - new Newtonsoft.Json.Converters.StringEnumConverter() - } - }); - private HttpClient _httpClient; public MailjetClient(string apiKey, string apiSecret, HttpMessageHandler httpMessageHandler = null) @@ -73,7 +65,7 @@ public async Task GetAsync(MailjetRequest request) var responseMessage = await _httpClient.GetAsync(url).ConfigureAwait(false); - JObject content = await HttpContentHelper.GetContentAsync(responseMessage).ConfigureAwait(false); + JsonObject content = await HttpContentHelper.GetContentAsync(responseMessage).ConfigureAwait(false); return new MailjetResponse(responseMessage.IsSuccessStatusCode, (int)responseMessage.StatusCode, content); } @@ -81,11 +73,11 @@ public async Task PostAsync(MailjetRequest request) { string url = request.BuildUrl(); - var output = request.Body.ToString(Formatting.None); + var output = request.Body.ToString(); HttpContent contentPost = new StringContent(output, Encoding.UTF8, MailjetConstants.JsonMediaType); var responseMessage = await _httpClient.PostAsync(url, contentPost).ConfigureAwait(false); - JObject content = await HttpContentHelper.GetContentAsync(responseMessage).ConfigureAwait(false); + JsonObject content = await HttpContentHelper.GetContentAsync(responseMessage).ConfigureAwait(false); return new MailjetResponse(responseMessage.IsSuccessStatusCode, (int)responseMessage.StatusCode, content); } @@ -93,11 +85,11 @@ public async Task PutAsync(MailjetRequest request) { string url = request.BuildUrl(); - var output = request.Body.ToString(Formatting.None); + var output = request.Body.ToString(); HttpContent contentPut = new StringContent(output, Encoding.UTF8, MailjetConstants.JsonMediaType); var responseMessage = await _httpClient.PutAsync(url, contentPut).ConfigureAwait(false); - JObject content = await HttpContentHelper.GetContentAsync(responseMessage).ConfigureAwait(false); + JsonObject content = await HttpContentHelper.GetContentAsync(responseMessage).ConfigureAwait(false); MailjetResponse mailjetResponse = new MailjetResponse(responseMessage.IsSuccessStatusCode, (int)responseMessage.StatusCode, content); return mailjetResponse; } @@ -108,7 +100,7 @@ public async Task DeleteAsync(MailjetRequest request) var responseMessage = await _httpClient.DeleteAsync(url).ConfigureAwait(false); - JObject content = await HttpContentHelper.GetContentAsync(responseMessage).ConfigureAwait(false); + JsonObject content = await HttpContentHelper.GetContentAsync(responseMessage).ConfigureAwait(false); return new MailjetResponse(responseMessage.IsSuccessStatusCode, (int)responseMessage.StatusCode, content); } @@ -147,16 +139,22 @@ public async Task SendTransactionalEmailsAsync(IEnum AdvanceErrorHandling = advanceErrorHandling }; + var serializerOptions = new JsonSerializerOptions() + { + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault + }; + serializerOptions.Converters.Add(new JsonStringEnumConverter()); + var clientRequest = new MailjetRequest { Resource = SendV31.Resource, - Body = JObject.FromObject(request, Serializer) + Body = JsonSerializer.SerializeToNode(request, serializerOptions).AsObject() }; MailjetResponse clientResponse = await PostAsync(clientRequest) .ConfigureAwait(false); - TransactionalEmailResponse result = clientResponse.Content.ToObject(); + TransactionalEmailResponse result = clientResponse.Content.Deserialize(); if (result.Messages == null && !clientResponse.IsSuccessStatusCode) { diff --git a/Mailjet.Client/MailjetRequest.cs b/Mailjet.Client/MailjetRequest.cs index 3826fed..950792f 100644 --- a/Mailjet.Client/MailjetRequest.cs +++ b/Mailjet.Client/MailjetRequest.cs @@ -1,6 +1,7 @@ -using Newtonsoft.Json.Linq; -using System; +using System; using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Nodes; namespace Mailjet.Client { @@ -19,7 +20,7 @@ public class MailjetRequest public IDictionary Filters { get; set; } = new Dictionary(); // The request body is a JObject that will be cast into a String before the call - public JObject Body { get; set; } = new JObject(); + public JsonObject Body { get; set; } = new JsonObject(); public MailjetRequest Filter(string key, string value) { @@ -34,11 +35,11 @@ public MailjetRequest Filter(string key, int value) public MailjetRequest Property(string key, Object value) { - Body.Add(key, new JValue(value)); + Body.Add(key, JsonValue.Create(value)); return this; } - public MailjetRequest Property(string key, JToken value) + public MailjetRequest Property(string key, JsonNode value) { Body.Add(key, value); return this; @@ -54,11 +55,11 @@ public string BuildUrl() public override string ToString() { - dynamic jObject = new JObject(); - jObject.Resource = JObject.FromObject(Resource); + dynamic jObject = new JsonObject(); + jObject.Resource = JsonSerializer.SerializeToNode(Resource); jObject.ResourceId = ResourceId != null ? ResourceId.Id : null; jObject.ActionID = ActionId.HasValue ? ActionId.Value.ToString() : null; - jObject.Filters = JObject.FromObject(Filters); + jObject.Filters = JsonSerializer.SerializeToNode(Filters); jObject.Body = Body; return jObject.ToString(); diff --git a/Mailjet.Client/MailjetResponse.cs b/Mailjet.Client/MailjetResponse.cs index 2c69c1b..5a3b32f 100644 --- a/Mailjet.Client/MailjetResponse.cs +++ b/Mailjet.Client/MailjetResponse.cs @@ -1,15 +1,15 @@ -using Newtonsoft.Json.Linq; -using System; +using System; +using System.Text.Json.Nodes; namespace Mailjet.Client { public class MailjetResponse { - public JObject Content { get; private set; } + public JsonObject Content { get; private set; } public bool IsSuccessStatusCode { get; private set; } public int StatusCode { get; private set; } - public MailjetResponse(bool isSuccessStatusCode, int statusCode, JObject content) + public MailjetResponse(bool isSuccessStatusCode, int statusCode, JsonObject content) { IsSuccessStatusCode = isSuccessStatusCode; StatusCode = statusCode; @@ -27,9 +27,9 @@ public int GetTotal() return total; } - public JArray GetData() + public JsonArray GetData() { - JArray result; + JsonArray result; if (TryGetValue("Data", out result)) { return result; @@ -46,7 +46,7 @@ public JArray GetData() return result; } - result = new JArray(Content); + result = new JsonArray(Content.DeepClone()); return result; } @@ -83,14 +83,27 @@ public string GetErrorMessage() public bool TryGetValue(string key, out T value) { - JToken token; - if (!Content.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out token)) + JsonNode node; + if (!Content.TryGetPropertyValue(key, out node)) { value = default(T); return false; } - value = token.Value(); + value = node.GetValue(); + return true; + } + + public bool TryGetValue(string key, out JsonArray value) + { + JsonNode node; + if (!Content.TryGetPropertyValue(key, out node)) + { + value = null; + return false; + } + + value = node.AsArray(); return true; } diff --git a/Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj b/Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj index c8594eb..d006382 100644 --- a/Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj +++ b/Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj @@ -5,10 +5,6 @@ net8.0 - - - - diff --git a/Mailjet.Tests/HttpContentHelperTests.cs b/Mailjet.Tests/HttpContentHelperTests.cs index 35baae7..9d44759 100644 --- a/Mailjet.Tests/HttpContentHelperTests.cs +++ b/Mailjet.Tests/HttpContentHelperTests.cs @@ -2,7 +2,6 @@ using Mailjet.Client.Helpers; using Mailjet.Client.TransactionalEmails.Response; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json.Linq; using System; using System.Linq; using System.Net; @@ -22,7 +21,7 @@ public async Task GetContentAsync_WhenContentIsNull_ReturnsStatusCode() // act var result = await HttpContentHelper.GetContentAsync(new HttpResponseMessage { StatusCode = expectedStatusCode }); - HttpStatusCode statusCode = (HttpStatusCode) Enum.Parse(typeof(HttpStatusCode), result.Value("StatusCode")); + HttpStatusCode statusCode = (HttpStatusCode) Enum.Parse(typeof(HttpStatusCode), result["StatusCode"].GetValue().ToString()); // assert Assert.AreEqual(expectedStatusCode, statusCode); @@ -39,7 +38,7 @@ public async Task GetContentAsync_WhenContentNotNull_ParsesMessagesCorrectly() var result = await HttpContentHelper.GetContentAsync(response); // assert - Assert.IsTrue(result.Value(nameof(TransactionalEmailResponse.Messages)).Any()); + Assert.IsTrue(result[nameof(TransactionalEmailResponse.Messages)].AsArray().Any()); } [TestMethod] @@ -51,7 +50,7 @@ public async Task GetContentAsync_WhenContentIsGenericError_ReturnsErrorInfo() // act var result = await HttpContentHelper.GetContentAsync(response); - string erroInfo = result.Value(MailjetConstants.ErrorInfo); + string erroInfo = result[MailjetConstants.ErrorInfo].GetValue(); // assert Assert.IsNotNull(erroInfo); @@ -65,7 +64,7 @@ public async Task GetContentAsync_WhenContentIsTooManyRequests_ReturnsCorrectErr // act var result = await HttpContentHelper.GetContentAsync(response); - string erroInfo = result.Value(MailjetConstants.ErrorInfo); + string erroInfo = result[MailjetConstants.ErrorInfo].GetValue(); // assert Assert.AreEqual(MailjetConstants.TooManyRequestsMessage, erroInfo); @@ -79,7 +78,7 @@ public async Task GetContentAsync_WhenContentIsInternalServerError_ReturnsCorrec // act var result = await HttpContentHelper.GetContentAsync(response); - string erroInfo = result.Value(MailjetConstants.ErrorInfo); + string erroInfo = result[MailjetConstants.ErrorInfo].GetValue(); // assert Assert.AreEqual(MailjetConstants.InternalServerErrorGeneralMessage, erroInfo); diff --git a/Mailjet.Tests/Integration/ContactsIntegrationTests.cs b/Mailjet.Tests/Integration/ContactsIntegrationTests.cs index dc8be2a..7862ff7 100644 --- a/Mailjet.Tests/Integration/ContactsIntegrationTests.cs +++ b/Mailjet.Tests/Integration/ContactsIntegrationTests.cs @@ -54,11 +54,11 @@ public async Task AssertCreateContact() var firstObject = response.GetData()[0]; - Assert.AreEqual(true, firstObject.Value("IsExcludedFromCampaigns")); - Assert.AreEqual(_contactName, firstObject.Value("Name")); - Assert.AreEqual(_contactEmail, firstObject.Value("Email")); + Assert.AreEqual(true, firstObject["IsExcludedFromCampaigns"].GetValue()); + Assert.AreEqual(_contactName, firstObject["Name"].GetValue()); + Assert.AreEqual(_contactEmail, firstObject["Email"].GetValue()); - var id = firstObject.Value("ID"); + var id = firstObject["ID"].GetValue(); return id; } @@ -82,10 +82,10 @@ private async Task AssertGetContact(long contactId) var firstObject = response.GetData()[0]; - Assert.AreEqual(true, firstObject.Value("IsExcludedFromCampaigns")); - Assert.AreEqual(_contactName, firstObject.Value("Name")); - Assert.AreEqual(_contactEmail, firstObject.Value("Email")); - Assert.AreEqual(contactId, firstObject.Value("ID")); + Assert.AreEqual(true, firstObject["IsExcludedFromCampaigns"].GetValue()); + Assert.AreEqual(_contactName, firstObject["Name"].GetValue()); + Assert.AreEqual(_contactEmail, firstObject["Email"].GetValue()); + Assert.AreEqual(contactId, firstObject["ID"].GetValue()); } private async Task AssertDeleteContact(long contactId) diff --git a/Mailjet.Tests/Integration/SendTransactionalEmailIntegrationTests.cs b/Mailjet.Tests/Integration/SendTransactionalEmailIntegrationTests.cs index 770b393..5b34869 100644 --- a/Mailjet.Tests/Integration/SendTransactionalEmailIntegrationTests.cs +++ b/Mailjet.Tests/Integration/SendTransactionalEmailIntegrationTests.cs @@ -6,7 +6,6 @@ using Mailjet.Client.Resources; using Mailjet.Client.TransactionalEmails; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json.Linq; namespace Mailjet.Tests.Integration { @@ -169,11 +168,11 @@ public static async Task GetValidSenderEmail(IMailjetClient client) foreach (var emailObject in response.GetData()) { - if (emailObject.Type != JTokenType.Object) + if (emailObject.GetValueKind() != System.Text.Json.JsonValueKind.Object) continue; - if (emailObject.Value("Status") == "Active") - return emailObject.Value("Email"); + if (emailObject["Status"].GetValue() == "Active") + return emailObject["Email"].GetValue(); } Assert.Fail("Cannot find Active sender address under given account"); diff --git a/Mailjet.Tests/Integration/TemplateIntegrationTests.cs b/Mailjet.Tests/Integration/TemplateIntegrationTests.cs index 33079c7..ec51a22 100644 --- a/Mailjet.Tests/Integration/TemplateIntegrationTests.cs +++ b/Mailjet.Tests/Integration/TemplateIntegrationTests.cs @@ -2,12 +2,12 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.Json; using System.Threading.Tasks; using Mailjet.Client; using Mailjet.Client.Resources; using Mailjet.Client.TransactionalEmails; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json.Linq; namespace Mailjet.Tests.Integration { @@ -67,7 +67,7 @@ private async Task FillTemplateContent(long templateId) ResourceId = ResourceId.Numeric(templateId) } .Property(TemplateDetailcontent.MJMLContent, content) - .Property(TemplateDetailcontent.Headers, JObject.FromObject(new Dictionary() + .Property(TemplateDetailcontent.Headers, JsonSerializer.Serialize(new Dictionary() { {"Subject", "Test transactional template subject " + DateTime.UtcNow}, {"SenderName", "Test transactional template"}, @@ -81,7 +81,7 @@ private async Task FillTemplateContent(long templateId) // assert Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(1, response.GetTotal()); - Assert.AreEqual(content, response.GetData().Single().Value("MJMLContent")); + Assert.AreEqual(content, response.GetData().Single()["MJMLContent"].GetValue()); } private async Task CreateTemplate() @@ -101,7 +101,7 @@ private async Task CreateTemplate() .Property(Template.Locale, "en_US") .Property(Template.Name, templateName) .Property(Template.OwnerType, Template.OwnerTypeValue_Apikey) - .Property(Template.Purposes, JArray.FromObject(new[]{ "transactional" })); + .Property(Template.Purposes, JsonSerializer.Serialize(new[]{ "transactional" })); // act MailjetResponse response = await _client.PostAsync(request); @@ -110,9 +110,9 @@ private async Task CreateTemplate() Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(1, response.GetTotal()); - Assert.AreEqual(templateName, response.GetData().Single().Value("Name")); + Assert.AreEqual(templateName, response.GetData().Single()["Name"].GetValue()); - long templateId = response.GetData().Single().Value("ID"); + long templateId = response.GetData().Single()["ID"].GetValue(); return templateId; } @@ -166,11 +166,11 @@ public static async Task GetValidSenderEmail(MailjetClient client) foreach (var emailObject in response.GetData()) { - if (emailObject.Type != JTokenType.Object) + if (emailObject.GetValueKind() != JsonValueKind.Object) continue; - if (emailObject.Value("Status") == "Active") - return emailObject.Value("Email"); + if (emailObject["Status"].GetValue() == "Active") + return emailObject["Email"].GetValue(); } Assert.Fail("Cannot find Active sender address under given account"); diff --git a/Mailjet.Tests/Mailjet.Tests.csproj b/Mailjet.Tests/Mailjet.Tests.csproj index 92b4d07..e4c5aad 100644 --- a/Mailjet.Tests/Mailjet.Tests.csproj +++ b/Mailjet.Tests/Mailjet.Tests.csproj @@ -19,7 +19,6 @@ - diff --git a/Mailjet.Tests/MailjetClientTests.cs b/Mailjet.Tests/MailjetClientTests.cs index a7cbf83..22ae2d7 100644 --- a/Mailjet.Tests/MailjetClientTests.cs +++ b/Mailjet.Tests/MailjetClientTests.cs @@ -1,10 +1,10 @@ using Mailjet.Client; using Mailjet.Client.Resources; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json.Linq; using RichardSzalay.MockHttp; using System; using System.Net; +using System.Text.Json.Nodes; using sms = Mailjet.Client.Resources.SMS; namespace Mailjet.Tests @@ -38,9 +38,9 @@ public void TestGetAsync() int expectedTotal = 1; int expectedCount = 1; - var expectedData = new JArray + var expectedData = new JsonArray { - new JObject + new JsonObject { { Apikey.APIKey, "ApiKeyTest" }, }, @@ -63,11 +63,11 @@ public void TestGetAsync() }; MailjetResponse response = client.GetAsync(request).Result; - + Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(expectedTotal, response.GetTotal()); Assert.AreEqual(expectedCount, response.GetCount()); - Assert.IsTrue(JToken.DeepEquals(expectedData, response.GetData())); + Assert.IsTrue(JsonValue.DeepEquals(expectedData, response.GetData())); } [TestMethod] @@ -119,7 +119,7 @@ public void TestSmsCountAsync() { int expectedTotal = 1; int expectedCount = 1; - var expectedData = new JArray(); + var expectedData = new JsonArray(); var mockHttp = new MockHttpMessageHandler(); @@ -144,7 +144,7 @@ public void TestSmsCountAsync() Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(expectedTotal, response.GetTotal()); Assert.AreEqual(expectedCount, response.GetCount()); - Assert.IsTrue(JToken.DeepEquals(expectedData, response.GetData())); + Assert.IsTrue(JsonArray.DeepEquals(expectedData, response.GetData())); } [TestMethod] @@ -154,14 +154,14 @@ public void TestSmsExportAsync() string expectedName = "PENDING"; string expectedDescription = "The request is accepted."; - var status = new JObject + var status = new JsonObject { { Code, expectedCode}, { Name, expectedName}, { Description, expectedDescription} }; - var smsExportResponse = new JObject + var smsExportResponse = new JsonObject { { Status, status } }; @@ -187,15 +187,15 @@ public void TestSmsExportAsync() MailjetResponse response = client.PostAsync(request).Result; Assert.IsTrue(response.IsSuccessStatusCode); - Assert.AreEqual(expectedCode, response.GetData()[0][Status].Value(Code)); - Assert.AreEqual(expectedName, response.GetData()[0][Status].Value(Name)); - Assert.AreEqual(expectedDescription, response.GetData()[0][Status].Value(Description)); + Assert.AreEqual(expectedCode, response.GetData()[0][Status][Code].GetValue()); + Assert.AreEqual(expectedName, response.GetData()[0][Status][Name].GetValue()); + Assert.AreEqual(expectedDescription, response.GetData()[0][Status][Description].GetValue()); } [TestMethod] public void TestSmsStatisticsAsync() { - var expectedData = new JArray(); + var expectedData = new JsonArray(); var mockHttp = new MockHttpMessageHandler(); var jsonResponse = GenerateJsonResponse(1, 1, expectedData); @@ -215,12 +215,12 @@ public void TestSmsStatisticsAsync() MailjetResponse response = client.GetAsync(request).Result; Assert.IsTrue(response.IsSuccessStatusCode); - Assert.IsTrue(JToken.DeepEquals(expectedData, response.GetData())); + Assert.IsTrue(JsonArray.DeepEquals(expectedData, response.GetData())); } - private string GenerateJsonResponse(int total, int count, JArray data) + private string GenerateJsonResponse(int total, int count, JsonArray data) { - var jObject = new JObject() + var jObject = new JsonObject() { { TotalKey, total }, { CountKey, count }, @@ -230,9 +230,9 @@ private string GenerateJsonResponse(int total, int count, JArray data) return GenerateJsonResponse(jObject); } - private string GenerateJsonResponse(JObject jObject) + private string GenerateJsonResponse(JsonObject jObject) { - return jObject.ToString(Newtonsoft.Json.Formatting.None); + return jObject.ToString(); } } } diff --git a/Mailjet.Tests/SendTransactionalEmailAsyncTests.cs b/Mailjet.Tests/SendTransactionalEmailAsyncTests.cs index a0771ca..905d586 100644 --- a/Mailjet.Tests/SendTransactionalEmailAsyncTests.cs +++ b/Mailjet.Tests/SendTransactionalEmailAsyncTests.cs @@ -1,11 +1,11 @@ using System.IO; using System.Linq; using System.Net.Http; +using System.Text.Json.Nodes; using System.Threading.Tasks; using Mailjet.Client; using Mailjet.Client.TransactionalEmails; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json.Linq; using RichardSzalay.MockHttp; namespace Mailjet.Tests @@ -67,7 +67,7 @@ public async Task SendTransactionalEmailAsync_PassesCorrectRequestToMailjetServe { // arrange string expectedRequest = File.ReadAllText(@"MockResponses/SendEmailV31Request.json"); - var expectedJObject = JObject.Parse(expectedRequest); + var expectedJObject = JsonObject.Parse(expectedRequest); _handler .Expect(HttpMethod.Post, "https://api.mailjet.com/v3.1/send") @@ -77,8 +77,8 @@ public async Task SendTransactionalEmailAsync_PassesCorrectRequestToMailjetServe { var content = message.Content.ReadAsStringAsync().Result; - var actualJObject = JObject.Parse(content); - return JToken.DeepEquals(actualJObject, expectedJObject); + var actualJObject = JsonObject.Parse(content); + return JsonNode.DeepEquals(actualJObject, expectedJObject); }) .Respond("application/json", "{}"); From 73ed1bbc51708cd13d9e4c5e287d4ca73b49bee7 Mon Sep 17 00:00:00 2001 From: Srdjan Rudic Date: Tue, 19 Nov 2024 09:00:25 +0100 Subject: [PATCH 3/4] Updated version and product readme --- Mailjet.Client/Mailjet.Client.csproj | 4 ++-- Mailjet.Client/MailjetConstants.cs | 2 +- README.md | 22 +++++++++------------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/Mailjet.Client/Mailjet.Client.csproj b/Mailjet.Client/Mailjet.Client.csproj index 0cdf081..b6e442f 100644 --- a/Mailjet.Client/Mailjet.Client.csproj +++ b/Mailjet.Client/Mailjet.Client.csproj @@ -4,8 +4,8 @@ net8.0 False - 3.0.0 - 3.0.0 + 3.1.0 + 3.1.0 Mailjet, Dimitar Kostov https://github.com/mailjet/mailjet-apiv3-dotnet/blob/master/LICENSE diff --git a/Mailjet.Client/MailjetConstants.cs b/Mailjet.Client/MailjetConstants.cs index 1b68b2d..95d9f4b 100644 --- a/Mailjet.Client/MailjetConstants.cs +++ b/Mailjet.Client/MailjetConstants.cs @@ -3,7 +3,7 @@ public static class MailjetConstants { public const string DefaultBaseAdress = "https://api.mailjet.com"; - public const string UserAgent = "mailjet-api-v3-net/3.0.0"; + public const string UserAgent = "mailjet-api-v3-net/3.1.0"; public const string JsonMediaType = "application/json"; public const string ApiVersionPathV3 = "v3"; public const string ApiVersionPathV3_1 = "v3.1"; diff --git a/README.md b/README.md index b081656..3c777de 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ # Mailjet .NET Wrapper [![Build Status](https://travis-ci.org/mailjet/mailjet-apiv3-dotnet.svg?branch=master)](https://travis-ci.org/mailjet/mailjet-apiv3-dotnet) -![Current Version](https://img.shields.io/badge/version-2.0.0-green.svg) +![Current Version](https://img.shields.io/badge/version-3.1.0-green.svg) ## Overview @@ -29,7 +29,7 @@ Check out all the resources and .NET code examples in the official [Mailjet Docu - [Table of contents](#table-of-contents) - [Release notes](#release-notes) - [Compatibility](#compatibility) - - [Dependencies .NETStandard 1.1](#dependencies-netstandard-11) + - [Dependencies .NET 8.0](#dependencies-net-80) - [Installation](#installation) - [Authentication](#authentication) - [Make your first call](#make-your-first-call) @@ -57,6 +57,8 @@ Check out all the resources and .NET code examples in the official [Mailjet Docu - [Contribute](#contribute) ## Release notes +### v 3.1.0 +- upgraded target framework to .NET 8.0 ### v 3.0.0 - bums version to indicate breaking change (removing extension methods like SendTransactionalEmailAsync) ### v 2.1.0 @@ -77,19 +79,13 @@ Check out all the resources and .NET code examples in the official [Mailjet Docu This .NET library is supported by: - - .NET Core 3.1+ - - .NET Framework 4.6.2 - - Mono 4.6 - - Xamarin.iOS 10.0 - - Xamarin.Android 7.0 - - Universal Windows Platform 10 - - Windows 8.0 - - Windows Phone 8.1 + - .NET 8.0+ + - Windows 10 + - Windows 11 -### Dependencies .NETStandard 1.1 +### Dependencies .NET 8.0 - - NETStandard.Library (>= 1.6.1) - - Newtonsoft.Json (>= 13.0.1) + - .NET (>= 8.0.0) ## Installation From 1e8f66c20add6b0d191c6f071896bbd779322998 Mon Sep 17 00:00:00 2001 From: Srdjan Rudic Date: Tue, 19 Nov 2024 09:05:50 +0100 Subject: [PATCH 4/4] Added Linux and macOS to supported OS list --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3c777de..a8162aa 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,8 @@ This .NET library is supported by: - .NET 8.0+ - Windows 10 - Windows 11 + - Linux + - macOS ### Dependencies .NET 8.0