Skip to content

Commit

Permalink
Merge pull request #48 from dlmelendez/master
Browse files Browse the repository at this point in the history
Json improvements
  • Loading branch information
ahwm authored Dec 1, 2024
2 parents 585c3d6 + e38f22b commit b957bd4
Show file tree
Hide file tree
Showing 27 changed files with 474 additions and 217 deletions.
179 changes: 179 additions & 0 deletions src/GodaddyWrapper.Tests/ClientJsonSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#if NETCORE
using GodaddyWrapper.Serialization;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace GodaddyWrapper.Tests
{
public class ClientJsonSerializationTests
{
private readonly ITestOutputHelper _output;

public ClientJsonSerializationTests(ITestOutputHelper output)
{
_output = output;
}


[Fact]
public void ClientJsonRequestTest()
{
const string namespaceName = "GodaddyWrapper.Requests";
HashSet<string> missingTypes = new HashSet<string>();
Assembly assembly = typeof(GoDaddyClient).Assembly;

//Check all the types in the client
typeof(GoDaddyClient).GetMethods()
.ToList()
.ForEach(method =>
{
var parameters = method.GetParameters();
if (parameters.Length > 0)
{
//_output.WriteLine($"Method: {nameof(GoDaddyClient)}.{method.Name}");

foreach (var p in parameters)
{
if (p.ParameterType.FullName.StartsWith(namespaceName))
{
//_output.WriteLine($"Parm: {p.ParameterType.Name}:{p.Name}");
bool found = JsonContext.Default.Options.TryGetTypeInfo(p.ParameterType, out _);
if (!found)
{
_ = missingTypes.Add($"[JsonSerializable(typeof({p.ParameterType.Name}))]");
}
}
else if (p.ParameterType.IsGenericType && p.ParameterType.GenericTypeArguments.Any(a => a.FullName.StartsWith(namespaceName)))
{
StringBuilder sb = new StringBuilder();
sb.Append(p.ParameterType.GetGenericTypeDefinition().Name.Split('`')[0]);
sb.Append('<');
foreach (var a in p.ParameterType.GenericTypeArguments)
{
sb.Append(a.Name);
sb.Append(',');
}
sb.Remove(sb.Length - 1, 1);
sb.Append('>');

//_output.WriteLine($"GParm: {sb.ToString()}:{p.Name}");
bool found = JsonContext.Default.Options.TryGetTypeInfo(p.ParameterType, out _);
if (!found)
{
_ = missingTypes.Add($"[JsonSerializable(typeof({sb.ToString()}))]");
}
}
}
}
});

//Check the types in the namespace
var types = assembly.GetTypes().Where(x => x.Namespace == namespaceName).ToList();
foreach (var t in types)
{
bool found = JsonContext.Default.Options.TryGetTypeInfo(t, out _);
if (!found)
{
_ = missingTypes.Add($"[JsonSerializable(typeof({t.Name}))]");
}
}

if (missingTypes.Count > 0)
{
_output.WriteLine($"Add the following attributes to the {nameof(JsonContext)} class:");
foreach (var item in missingTypes)
{
_output.WriteLine(item);
}
}
Assert.Empty(missingTypes);
}

[Fact]
public void ClientJsonResponseTest()
{
const string namespaceName = "GodaddyWrapper.Responses";
HashSet<string> missingTypes = new HashSet<string>();
Assembly assembly = typeof(GoDaddyClient).Assembly;

//Check all the types in the client
typeof(GoDaddyClient).GetMethods()
.ToList()
.ForEach(method =>
{
var p = method.ReturnParameter;
if (p is not null)
{
//_output.WriteLine($"Method: {nameof(GoDaddyClient)}.{method.Name}");
Type parameterType = p.ParameterType;
if(parameterType.Name.StartsWith("Task"))
{
parameterType = parameterType.GenericTypeArguments.FirstOrDefault();
}
if (parameterType is not null)
{
if (parameterType.FullName.StartsWith(namespaceName))
{
//_output.WriteLine($"Parm: {parameterType.Name}:{p.Name}");
bool found = JsonContext.Default.Options.TryGetTypeInfo(parameterType, out _);
if (!found)
{
_ = missingTypes.Add($"[JsonSerializable(typeof({parameterType.Name}))]");
}
}
else if (parameterType.IsGenericType && parameterType.GenericTypeArguments.Any(a => a.FullName.StartsWith(namespaceName)))
{
StringBuilder sb = new StringBuilder();
sb.Append(parameterType.GetGenericTypeDefinition().Name.Split('`')[0]);
sb.Append('<');
foreach (var a in parameterType.GenericTypeArguments)
{
sb.Append(a.Name);
sb.Append(',');
}
sb.Remove(sb.Length - 1, 1);
sb.Append('>');

//_output.WriteLine($"GParm: {sb.ToString()}:{p.Name}");
bool found = JsonContext.Default.Options.TryGetTypeInfo(parameterType, out _);
if (!found)
{
_ = missingTypes.Add($"[JsonSerializable(typeof({sb.ToString()}))]");
}
}
}
}
});

//Check the types in the namespace
var types = assembly.GetTypes().Where(x => x.Namespace == namespaceName).ToList();
foreach (var t in types)
{
bool found = JsonContext.Default.Options.TryGetTypeInfo(t, out _);
if (!found)
{
_ = missingTypes.Add($"[JsonSerializable(typeof({t.Name}))]");
}
}

if (missingTypes.Count > 0)
{
_output.WriteLine($"Add the following attributes to the {nameof(JsonContext)} class:");
foreach (var item in missingTypes)
{
_output.WriteLine(item);
}
}
Assert.Empty(missingTypes);
}

}
}
#endif
28 changes: 6 additions & 22 deletions src/GodaddyWrapper.Tests/DomainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,20 @@ public DomainTests()
[Fact]
public async Task DomainCheckTest()
{
try
var response = await client.CheckDomainAvailable(new DomainAvailable
{
var response = await client.CheckDomainAvailable(new DomainAvailable
{
Domain = "google.com"
});
Domain = "google.com"
});

response.Available.ShouldBe(false);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Assert.Fail();
}
response.Available.ShouldBe(false);
}

[Fact]
public async Task DomainListTest()
{
try
{
var response = await client.RetrieveDomainList(new DomainRetrieve { Limit = 100 });
var response = await client.RetrieveDomainList(new DomainRetrieve { Limit = 100 });

response.Count.ShouldBe(0);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Assert.Fail();
}
response.Count.ShouldBe(0);
}
}
}
1 change: 1 addition & 0 deletions src/GodaddyWrapper.Tests/GodaddyWrapper.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions src/GodaddyWrapper/Client.AbuseTicket.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GodaddyWrapper.Helper;
using GodaddyWrapper.Requests;
using GodaddyWrapper.Responses;
using Newtonsoft.Json;
using System.Net.Http.Json;
using System.Threading.Tasks;

namespace GodaddyWrapper
Expand All @@ -15,9 +15,9 @@ public partial class GoDaddyClient
/// <returns></returns>
public async Task<AbuseTicketIdResponse> CreateAbuseTicket(AbuseTicketCreate request)
{
var response = await httpClient.PostAsync($"abuse/tickets", JsonConvert.SerializeObject(request, JsonSettings));
var response = await httpClient.PostAsJsonAsync($"abuse/tickets", request, JsonSettings);
await CheckResponseMessageIsValid(response);
return await response.Content.ReadAsAsync<AbuseTicketIdResponse>();
return await response.Content.ReadAsAsync<AbuseTicketIdResponse>(JsonSettings);
}
/// <summary>
/// List all abuse tickets ids that match user provided filters
Expand All @@ -28,7 +28,7 @@ public async Task<AbuseTicketListResponse> RetrieveAbuseTickets(AbuseTicketRetri
{
var response = await httpClient.GetAsync($"abuse/tickets{QueryStringBuilder.RequestObjectToQueryString(request)}");
await CheckResponseMessageIsValid(response);
return await response.Content.ReadAsAsync<AbuseTicketListResponse>();
return await response.Content.ReadAsAsync<AbuseTicketListResponse>(JsonSettings);
}

/// <summary>
Expand All @@ -40,7 +40,7 @@ public async Task<AbuseTicketResponse> RetrieveAbuseTicketDetail(AbuseTicketDeta
{
var response = await httpClient.GetAsync($"abuse/tickets/{request.TicketId}");
await CheckResponseMessageIsValid(response);
return await response.Content.ReadAsAsync<AbuseTicketResponse>();
return await response.Content.ReadAsAsync<AbuseTicketResponse>(JsonSettings);
}
}
}
8 changes: 4 additions & 4 deletions src/GodaddyWrapper/Client.Aftermarket.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using GodaddyWrapper.Helper;
using GodaddyWrapper.Requests;
using GodaddyWrapper.Responses;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http.Json;
using System.Threading.Tasks;

namespace GodaddyWrapper
Expand All @@ -17,9 +17,9 @@ public partial class GoDaddyClient
/// <returns></returns>
public async Task<AftermarketListingActionResponse> AddExpiryAuction(List<AftermarketListingExpiryCreate> request)
{
var response = await httpClient.PostAsync($"aftermarket/listings/expiry", JsonConvert.SerializeObject(request, JsonSettings));
var response = await httpClient.PostAsJsonAsync($"aftermarket/listings/expiry", request, JsonSettings);
await CheckResponseMessageIsValid(response);
return await response.Content.ReadAsAsync<AftermarketListingActionResponse>();
return await response.Content.ReadAsAsync<AftermarketListingActionResponse>(JsonSettings);
}

/// <summary>
Expand All @@ -31,7 +31,7 @@ public async Task<ListingActionResponse> RemoveAuctionListings(AgreementRetrieve
{
var response = await httpClient.DeleteAsync($"aftermarket/listings{QueryStringBuilder.RequestObjectToQueryString(request)}");
await CheckResponseMessageIsValid(response);
return await response.Content.ReadAsAsync<ListingActionResponse>();
return await response.Content.ReadAsAsync<ListingActionResponse>(JsonSettings);
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/GodaddyWrapper/Client.Agreements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<List<LegalAgreementResponse>> RetrieveAgreements(AgreementRetr
httpClient.DefaultRequestHeaders.Add("X-Market-Id", XMarketId);
var response = await httpClient.GetAsync($"aggreements{QueryStringBuilder.RequestObjectToQueryString(request)}");
await CheckResponseMessageIsValid(response);
return await response.Content.ReadAsAsync<List<LegalAgreementResponse>>();
return await response.Content.ReadAsAsync<List<LegalAgreementResponse>>(JsonSettings);
}

}
Expand Down
Loading

0 comments on commit b957bd4

Please sign in to comment.