Adds methods to fetch cards

This commit is contained in:
Serraniel 2023-03-01 21:17:08 +01:00
parent e6c7cdf9b4
commit b6ff5f86a6
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3

View file

@ -9,6 +9,7 @@ using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using JpnCardsPokemonSdk.Api; using JpnCardsPokemonSdk.Api;
using JpnCardsPokemonSdk.Client.Responses; using JpnCardsPokemonSdk.Client.Responses;
using JpnCardsPokemonSdk.Utils.QueryFilter;
namespace JpnCardsPokemonSdk.Client; namespace JpnCardsPokemonSdk.Client;
@ -95,6 +96,29 @@ public class ApiClient
return await FetchInternalAsync<Set>(SetQuery($"uuid/{uuid}")); return await FetchInternalAsync<Set>(SetQuery($"uuid/{uuid}"));
} }
public async Task<IEnumerable<Card>> FetchCardsAsync(string query)
{
if (string.IsNullOrWhiteSpace(query))
throw new Exception("Query string required");
// JSON response is wrapped into a data property, so we parse as JsonDocument first before deserialization.
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
IncludeFields = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var jsonData = await FetchInternalAsync<JsonDocument>($"card/{query}");
return jsonData?.RootElement.GetProperty("data").Deserialize<IEnumerable<Card>>(options) ??
Enumerable.Empty<Card>();
}
public async Task<IEnumerable<Card>?> FetchCardsAsync(IQueryFilterBuilder filterBuilder)
{
return await FetchCardsAsync(filterBuilder.BuildQueryString());
}
/* /*
public async Task<EnumerableApiResponse<T>?> FetchDataAsync<T>(string? query = null, int page = 1) public async Task<EnumerableApiResponse<T>?> FetchDataAsync<T>(string? query = null, int page = 1)
where T : EndpointObject where T : EndpointObject