Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 8 #141

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions Mailjet.Client/Exceptions/MailjetServerException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Mailjet.Client.Exceptions
namespace Mailjet.Client.Exceptions
{
public class MailjetServerException : MailjetException
{
Expand Down
20 changes: 10 additions & 10 deletions Mailjet.Client/Helpers/HttpContentHelper.cs
Original file line number Diff line number Diff line change
@@ -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<JObject> GetContentAsync(HttpResponseMessage responseMessage)
public static async Task<JsonObject> GetContentAsync(HttpResponseMessage responseMessage)
{
string cnt = null;

Expand All @@ -16,30 +16,30 @@ public static async Task<JObject> 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));
}
}

Expand Down
10 changes: 3 additions & 7 deletions Mailjet.Client/Mailjet.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.1</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>

<Version>3.0.0</Version>
<PackageVersion>3.0.0</PackageVersion>
<Version>3.1.0</Version>
<PackageVersion>3.1.0</PackageVersion>

<Authors>Mailjet, Dimitar Kostov</Authors>
<PackageLicenseUrl>https://github.com/mailjet/mailjet-apiv3-dotnet/blob/master/LICENSE</PackageLicenseUrl>
Expand Down Expand Up @@ -35,8 +35,4 @@
<Deterministic>true</Deterministic>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

</Project>
36 changes: 17 additions & 19 deletions Mailjet.Client/MailjetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,15 +20,6 @@ namespace Mailjet.Client
/// </summary>
public class MailjetClient : IMailjetClient
{
private static readonly JsonSerializer Serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore,
Converters = new List<JsonConverter>
{
new Newtonsoft.Json.Converters.StringEnumConverter()
}
});

private HttpClient _httpClient;

public MailjetClient(string apiKey, string apiSecret, HttpMessageHandler httpMessageHandler = null)
Expand Down Expand Up @@ -73,31 +65,31 @@ public async Task<MailjetResponse> 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);
}

public async Task<MailjetResponse> 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);
}

public async Task<MailjetResponse> 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;
}
Expand All @@ -108,7 +100,7 @@ public async Task<MailjetResponse> 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);
}

Expand Down Expand Up @@ -147,16 +139,22 @@ public async Task<TransactionalEmailResponse> 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>();
TransactionalEmailResponse result = clientResponse.Content.Deserialize<TransactionalEmailResponse>();

if (result.Messages == null && !clientResponse.IsSuccessStatusCode)
{
Expand Down
2 changes: 1 addition & 1 deletion Mailjet.Client/MailjetConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
17 changes: 9 additions & 8 deletions Mailjet.Client/MailjetRequest.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -19,7 +20,7 @@ public class MailjetRequest
public IDictionary<string, string> Filters { get; set; } = new Dictionary<string, string>();

// 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)
{
Expand All @@ -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;
Expand All @@ -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();
Expand Down
33 changes: 23 additions & 10 deletions Mailjet.Client/MailjetResponse.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -46,7 +46,7 @@ public JArray GetData()
return result;
}

result = new JArray(Content);
result = new JsonArray(Content.DeepClone());
return result;
}

Expand Down Expand Up @@ -83,14 +83,27 @@ public string GetErrorMessage()

public bool TryGetValue<T>(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<T>();
value = node.GetValue<T>();
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;
}

Expand Down
6 changes: 1 addition & 5 deletions Mailjet.ConsoleApplication/Mailjet.ConsoleApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Mailjet.Client\Mailjet.Client.csproj" />
</ItemGroup>
Expand Down
11 changes: 5 additions & 6 deletions Mailjet.Tests/HttpContentHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string>("StatusCode"));
HttpStatusCode statusCode = (HttpStatusCode) Enum.Parse(typeof(HttpStatusCode), result["StatusCode"].GetValue<int>().ToString());

// assert
Assert.AreEqual(expectedStatusCode, statusCode);
Expand All @@ -39,7 +38,7 @@ public async Task GetContentAsync_WhenContentNotNull_ParsesMessagesCorrectly()
var result = await HttpContentHelper.GetContentAsync(response);

// assert
Assert.IsTrue(result.Value<JArray>(nameof(TransactionalEmailResponse.Messages)).Any());
Assert.IsTrue(result[nameof(TransactionalEmailResponse.Messages)].AsArray().Any());
}

[TestMethod]
Expand All @@ -51,7 +50,7 @@ public async Task GetContentAsync_WhenContentIsGenericError_ReturnsErrorInfo()

// act
var result = await HttpContentHelper.GetContentAsync(response);
string erroInfo = result.Value<string>(MailjetConstants.ErrorInfo);
string erroInfo = result[MailjetConstants.ErrorInfo].GetValue<string>();

// assert
Assert.IsNotNull(erroInfo);
Expand All @@ -65,7 +64,7 @@ public async Task GetContentAsync_WhenContentIsTooManyRequests_ReturnsCorrectErr

// act
var result = await HttpContentHelper.GetContentAsync(response);
string erroInfo = result.Value<string>(MailjetConstants.ErrorInfo);
string erroInfo = result[MailjetConstants.ErrorInfo].GetValue<string>();

// assert
Assert.AreEqual(MailjetConstants.TooManyRequestsMessage, erroInfo);
Expand All @@ -79,7 +78,7 @@ public async Task GetContentAsync_WhenContentIsInternalServerError_ReturnsCorrec

// act
var result = await HttpContentHelper.GetContentAsync(response);
string erroInfo = result.Value<string>(MailjetConstants.ErrorInfo);
string erroInfo = result[MailjetConstants.ErrorInfo].GetValue<string>();

// assert
Assert.AreEqual(MailjetConstants.InternalServerErrorGeneralMessage, erroInfo);
Expand Down
Loading