From b6ff5f86a6365f2f8e5fbf4f15b1dca771b88e7e Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 1 Mar 2023 21:17:08 +0100 Subject: [PATCH] Adds methods to fetch cards --- src/Client/ApiClient.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Client/ApiClient.cs b/src/Client/ApiClient.cs index 2af64ee..40030b6 100644 --- a/src/Client/ApiClient.cs +++ b/src/Client/ApiClient.cs @@ -9,6 +9,7 @@ using System.Text.Json.Serialization; using System.Threading.Tasks; using JpnCardsPokemonSdk.Api; using JpnCardsPokemonSdk.Client.Responses; +using JpnCardsPokemonSdk.Utils.QueryFilter; namespace JpnCardsPokemonSdk.Client; @@ -95,6 +96,29 @@ public class ApiClient return await FetchInternalAsync(SetQuery($"uuid/{uuid}")); } + public async Task> 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($"card/{query}"); + return jsonData?.RootElement.GetProperty("data").Deserialize>(options) ?? + Enumerable.Empty(); + } + + public async Task?> FetchCardsAsync(IQueryFilterBuilder filterBuilder) + { + return await FetchCardsAsync(filterBuilder.BuildQueryString()); + } + /* public async Task?> FetchDataAsync(string? query = null, int page = 1) where T : EndpointObject