Skip to content

Commit

Permalink
Add country endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
edgiardina committed Sep 7, 2024
1 parent 802855b commit 8837e51
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 27 deletions.
14 changes: 11 additions & 3 deletions PinballApi.Tests/PinballRankingApiTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public async Task PinballRankingApi_RankingSearch_GetRankingsByType([Values] Ran
{
Assume.That(type, Is.Not.EqualTo(RankingType.Pro));

var result = await rankingApi.RankingSearch(type);
var code = "US";

var result = await rankingApi.RankingSearch(type, countryCode: code);

Assert.That(result, Is.Not.Null);
Assert.That(result.Rankings, Is.Not.Null);
Expand All @@ -62,9 +64,9 @@ public async Task PinballRankingApi_RankingSearch_GetRankingsByType([Values] Ran
}

[Test]
public async Task PinballRankingApi_ProRankingSearch_GetRankings([Values] RankingSystem system)
public async Task PinballRankingApi_ProRankingSearch_GetRankings([Values] TournamentType system)
{
Assume.That(system, Is.Not.EqualTo(RankingSystem.Youth));
Assume.That(system, Is.Not.EqualTo(TournamentType.Youth));

var result = await rankingApi.ProRankingSearch(system);

Expand All @@ -84,6 +86,12 @@ public async Task PinballRankingApi_GetRankingCountries()
Assert.That(result.Country.Count, Is.GreaterThan(0));
}

[Test]
public async Task PinballRankingApi_GetRankingByCountry_ShouldThrowIfCountryCodeIsNullOrEmpty()
{
Assert.ThrowsAsync<ArgumentException>(async () => await rankingApi.RankingSearch(RankingType.Country));
}

[Test]
public async Task PinballRankingApi_PlayerSearch_GetPlayerId()
{
Expand Down
8 changes: 4 additions & 4 deletions PinballApi/Converters/RankingSystemConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

namespace PinballApi.Converters
{
public class RankingSystemConverter : JsonConverter<RankingSystem>
public class RankingSystemConverter : JsonConverter<TournamentType>
{
public override RankingSystem Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override TournamentType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var enumString = reader.GetString().ToLower();
enumString = UppercaseFirst(enumString);
return (RankingSystem)Enum.Parse(typeToConvert, enumString, ignoreCase: true);
return (TournamentType)Enum.Parse(typeToConvert, enumString, ignoreCase: true);
}

public override void Write(Utf8JsonWriter writer, RankingSystem value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, TournamentType value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
Expand Down
6 changes: 3 additions & 3 deletions PinballApi/Interfaces/IPinballRankingApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace PinballApi.Interfaces
{
public interface IPinballRankingApi
{
Task<RankingSearch> RankingSearch(RankingType rankingType, RankingSystem rankingSystem = RankingSystem.Open, int count = 100, int startPosition = 1);
Task<RankingSearch> RankingSearch(RankingType rankingType, RankingSystem rankingSystem = RankingSystem.Open, int count = 100, int startPosition = 1, string countryCode = null);
Task<Models.WPPR.Universal.Tournaments.Tournament> GetTournament(int tournamentId);
Task<TournamentSearch> TournamentSearch(double? latitude = null, double? longitude = null, int? radius = null, DistanceType? distanceType = null, string name = null, string country = null, string stateprov = null, DateTime? startDate = null, DateTime? endDate = null, RankingSystem? rankingSystem = null, int? startPosition = null, int? totalReturn = null, TournamentSearchSortMode? tournamentSearchSortMode = null, TournamentSearchSortOrder? tournamentSearchSortOrder = null, string directorName = null, bool? preRegistration = null, bool? onlyWithResults = null, double? minimumPoints = null, double? maximumPoints = null, bool? pointFilter = null);
Task<TournamentSearch> TournamentSearch(double? latitude = null, double? longitude = null, int? radius = null, DistanceType? distanceType = null, string name = null, string country = null, string stateprov = null, DateTime? startDate = null, DateTime? endDate = null, TournamentType? rankingSystem = null, int? startPosition = null, int? totalReturn = null, TournamentSearchSortMode? tournamentSearchSortMode = null, TournamentSearchSortOrder? tournamentSearchSortOrder = null, string directorName = null, bool? preRegistration = null, bool? onlyWithResults = null, double? minimumPoints = null, double? maximumPoints = null, bool? pointFilter = null);
Task<Player> GetPlayer(int playerId);
Task<RankingCountries> GetRankingCountries();
Task<ProRankingSearch> ProRankingSearch(RankingSystem rankingSystem);
Task<ProRankingSearch> ProRankingSearch(TournamentType rankingSystem);
}
}
4 changes: 2 additions & 2 deletions PinballApi/Models/WPPR/Universal/Rankings/Ranking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class Ranking : BaseRanking
public string BestFinish { get; set; }

[JsonPropertyName("best_finish_position")]
public string BestFinishPosition { get; set; }
public int BestFinishPosition { get; set; }

[JsonPropertyName("best_tournament_id")]
public string BestTournamentId { get; set; }
public long BestTournamentId { get; set; }
}
}
2 changes: 1 addition & 1 deletion PinballApi/Models/WPPR/Universal/Rankings/RankingSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RankingSearch
public RankingType RankingType { get; set; }

[JsonPropertyName("tournament_type")]
public RankingSystem RankingSystem { get; set; }
public TournamentType RankingSystem { get; set; }

[JsonPropertyName("start_position")]
public int StartPosition { get; set; }
Expand Down
14 changes: 14 additions & 0 deletions PinballApi/Models/WPPR/Universal/Rankings/RankingSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PinballApi.Models.WPPR.Universal.Rankings
{
public enum RankingSystem
{
Open,
Restricted
}
}
3 changes: 2 additions & 1 deletion PinballApi/Models/WPPR/Universal/Rankings/RankingType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum RankingType
Pro,
Wppr,
Youth,
Women
Women,
Country
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace PinballApi.Models.WPPR.Universal
{
public enum RankingSystem
public enum TournamentType
{
Main,
Open,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Tournament

[JsonPropertyName("ranking_system")]
[JsonConverter(typeof(RankingSystemConverter))]
public RankingSystem RankingSystem { get; set; }
public TournamentType RankingSystem { get; set; }

[JsonPropertyName("preregistration_date")]
[JsonConverter(typeof(NullableDateConverter))]
Expand Down
2 changes: 1 addition & 1 deletion PinballApi/Models/WPPR/Universal/Tournaments/Tournament.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public partial class Tournament

[JsonPropertyName("ranking_system")]
[JsonConverter(typeof(RankingSystemConverter))]
public RankingSystem RankingSystem { get; set; }
public TournamentType RankingSystem { get; set; }

[JsonPropertyName("details")]
public string Details { get; set; }
Expand Down
26 changes: 16 additions & 10 deletions PinballApi/PinballRankingApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public PinballRankingApi(string apiKey) : base(apiKey)

#region Tournaments

public async Task<TournamentSearch> TournamentSearch(double? latitude = null, double? longitude = null, int? radius = null, DistanceType? distanceType = null, string name = null, string country = null, string stateprov = null, DateTime? startDate = null, DateTime? endDate = null, RankingSystem? rankingSystem = null, int? startPosition = null,
public async Task<TournamentSearch> TournamentSearch(double? latitude = null, double? longitude = null, int? radius = null, DistanceType? distanceType = null, string name = null, string country = null, string stateprov = null, DateTime? startDate = null, DateTime? endDate = null, TournamentType? tournamentType = null, int? startPosition = null,
int? totalReturn = null, TournamentSearchSortMode? tournamentSearchSortMode = null, TournamentSearchSortOrder? tournamentSearchSortOrder = null, string directorName = null,
bool? preRegistration = null, bool? onlyWithResults = null, double? minimumPoints = null, double? maximumPoints = null, bool? pointFilter = null)
{
Expand Down Expand Up @@ -70,8 +70,8 @@ public async Task<TournamentSearch> TournamentSearch(double? latitude = null, do
if (endDate.HasValue)
request = request.SetQueryParam("end_date", endDate.Value.ToString("yyyy-MM-dd"));

if (rankingSystem.HasValue)
request = request.SetQueryParam("rank_type", rankingSystem.Value.ToString().ToUpper());
if (tournamentType.HasValue)
request = request.SetQueryParam("rank_type", tournamentType.Value.ToString().ToUpper());

if (radius.HasValue)
request = request.SetQueryParam("radius", radius);
Expand Down Expand Up @@ -150,11 +150,14 @@ public async Task<RankingCountries> GetRankingCountries()
return await request.GetJsonAsync<RankingCountries>();
}

public async Task<RankingSearch> RankingSearch(RankingType rankingType, RankingSystem rankingSystem = RankingSystem.Open, int count = 100, int startPosition = 1)
public async Task<RankingSearch> RankingSearch(RankingType rankingType, RankingSystem rankingSystem = RankingSystem.Open, int count = 100, int startPosition = 1, string countryCode = null)
{
if (rankingType == RankingType.Pro)
throw new ArgumentException("Use Pro Ranking Search method for Pro Rankings");

if (rankingType == RankingType.Country && string.IsNullOrEmpty(countryCode))
throw new ArgumentException("Country Code must be provided for Country Rankings");

var request = BaseRequest
.AppendPathSegment("rankings")
.AppendPathSegment(rankingType.ToString().ToLower())
Expand All @@ -166,20 +169,23 @@ public async Task<RankingSearch> RankingSearch(RankingType rankingType, RankingS

if (rankingType == RankingType.Women)
{
request = request.AppendPathSegment(rankingSystem == RankingSystem.Women ? "restricted" : "open");
request = request.AppendPathSegment(rankingSystem.ToString().ToLower());
}
else if (rankingType == RankingType.Country)
{
request = request.SetQueryParam("country", countryCode);
}


return await request.GetJsonAsync<RankingSearch>();
}

public async Task<ProRankingSearch> ProRankingSearch(RankingSystem rankingSystem = RankingSystem.Open)
public async Task<ProRankingSearch> ProRankingSearch(TournamentType rankingSystem = TournamentType.Open)
{
if (rankingSystem == RankingSystem.Youth)
if (rankingSystem == TournamentType.Youth)
throw new ArgumentException("Youth Pro Rankings are not supported");

if (rankingSystem == RankingSystem.Main)
rankingSystem = RankingSystem.Open;
if (rankingSystem == TournamentType.Main)
rankingSystem = TournamentType.Open;

var request = BaseRequest
.AppendPathSegment("rankings")
Expand Down

0 comments on commit 8837e51

Please sign in to comment.