From 48209a130b39a3c91b0e8afb6d6cf5df3f3509b3 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 1 Mar 2023 12:52:30 +0100 Subject: [PATCH] Refactors API fetching for tests and fixes a typo --- src/Client/ApiClient.cs | 16 +++-- src/Client/Responses/EnumerableApiResponse.cs | 54 +++-------------- src/Client/Responses/PageableApiResponse.cs | 59 +++++++++++++++++++ 3 files changed, 79 insertions(+), 50 deletions(-) create mode 100644 src/Client/Responses/PageableApiResponse.cs diff --git a/src/Client/ApiClient.cs b/src/Client/ApiClient.cs index d16cb8c..079d0c2 100644 --- a/src/Client/ApiClient.cs +++ b/src/Client/ApiClient.cs @@ -45,15 +45,21 @@ public class ApiClient DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; - var response = await _client.GetFromJsonAsync(requestUri, options); + var response = await _client.GetFromJsonAsync(requestUri, options); - if (response is IPageableApiResponse pageAbleApiResponse) + // TODO: Find good way to handle pageable requests + /*if (response is IPageableApiResponse pageAbleApiResponse) { pageAbleApiResponse.CurrentApiClient = this; pageAbleApiResponse.RememberRequestUri(requestUri); - } + }*/ - return response; + var result = new TResponseType + { + Data = response + }; + + return result; } public async Task?> FetchDataAsync(string? query = null, int page = 1) @@ -71,7 +77,7 @@ public class ApiClient return await FetchDataAsync, T>($"{endpoint.ApiUri()}/id={id}"); } - public async Task?> FetchByUuigAsync(int uuid) where T : EndpointObject + public async Task?> FetchByUuidAsync(int uuid) where T : EndpointObject { var endpoint = EndpointFactory.GetApiEndpoint(); diff --git a/src/Client/Responses/EnumerableApiResponse.cs b/src/Client/Responses/EnumerableApiResponse.cs index 056572a..168f883 100644 --- a/src/Client/Responses/EnumerableApiResponse.cs +++ b/src/Client/Responses/EnumerableApiResponse.cs @@ -1,58 +1,22 @@ -using System; +using System.Collections; using System.Collections.Generic; -using System.Text.RegularExpressions; -using System.Threading.Tasks; +using System.Linq; +using System.Text.Json.Serialization; using JpnCardsPokemonSdk.Client.Endpoints; namespace JpnCardsPokemonSdk.Client.Responses; -public class EnumerableApiResponse : IApiResponse>, - IPageableApiResponse, IEnumerable> where T : EndpointObject +public class EnumerableApiResponse : IApiResponse>, IEnumerable where T : EndpointObject { - private string? RequestUri { get; set; } + [JsonPropertyName("")] public IEnumerable? Data { get; set; } - public int TotalPages => (int)Math.Ceiling((decimal)( - (IPageableApiResponse, IEnumerable>)this).TotalCount / ( - (IPageableApiResponse, IEnumerable>)this).PageSize); - - public IEnumerable? Data { get; set; } - - ApiClient? IPageableApiResponse, IEnumerable>.CurrentApiClient { get; set; } - - int IPageableApiResponse, IEnumerable>.Page { get; set; } - - int IPageableApiResponse, IEnumerable>.PageSize { get; set; } - - int IPageableApiResponse, IEnumerable>.Count { get; set; } - -#if !(NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER) - bool IPageableApiResponse, IEnumerable>.HasNextPage() + public IEnumerator GetEnumerator() { - return ((IPageableApiResponse, IEnumerable>)this).Page < ((IPageableApiResponse, IEnumerable>)this).TotalCount; - } -#endif - - int IPageableApiResponse, IEnumerable>.TotalCount { get; set; } - - async Task> IPageableApiResponse, IEnumerable>. - FetchNextPageAsync() - { - return await ((IPageableApiResponse, IEnumerable>)this).FetchPageAsync(( - (IPageableApiResponse, IEnumerable>)this).Page + 1); + return Data?.GetEnumerator() ?? Enumerable.Empty().GetEnumerator(); } - async Task?> IPageableApiResponse, IEnumerable>. - FetchPageAsync(int page) + IEnumerator IEnumerable.GetEnumerator() { - var requestUri = RequestUri + "&page=" + page; - - return await ((IPageableApiResponse, IEnumerable>)this).CurrentApiClient - ?.FetchDataAsync, IEnumerable>(requestUri)!; - } - - void IPageableApiResponse, IEnumerable>.RememberRequestUri(string requestUri) - { - // Remember full Uri without page - RequestUri = Regex.Replace(requestUri, @"page=\d*&?", ""); + return GetEnumerator(); } } \ No newline at end of file diff --git a/src/Client/Responses/PageableApiResponse.cs b/src/Client/Responses/PageableApiResponse.cs new file mode 100644 index 0000000..a8c31a0 --- /dev/null +++ b/src/Client/Responses/PageableApiResponse.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using JpnCardsPokemonSdk.Client.Endpoints; + +namespace JpnCardsPokemonSdk.Client.Responses; + +public class PageableApiResponse : EnumerableApiResponse, + IPageableApiResponse, IEnumerable> + where T : EndpointObject +{ + private string? RequestUri { get; set; } + + public int TotalPages => (int)Math.Ceiling((decimal)( + (IPageableApiResponse, IEnumerable>)this).TotalCount / ( + (IPageableApiResponse, IEnumerable>)this).PageSize); + + ApiClient? IPageableApiResponse, IEnumerable>.CurrentApiClient { get; set; } + + int IPageableApiResponse, IEnumerable>.Page { get; set; } + + int IPageableApiResponse, IEnumerable>.PageSize { get; set; } + + int IPageableApiResponse, IEnumerable>.Count { get; set; } + + int IPageableApiResponse, IEnumerable>.TotalCount { get; set; } + + +#if !(NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER) + bool IPageableApiResponse, IEnumerable>.HasNextPage() + { + return ((IPageableApiResponse, IEnumerable>)this).Page < + ((IPageableApiResponse, IEnumerable>)this).TotalCount; + } +#endif + + async Task> IPageableApiResponse, IEnumerable>. + FetchNextPageAsync() + { + return await ((IPageableApiResponse, IEnumerable>)this).FetchPageAsync(( + (IPageableApiResponse, IEnumerable>)this).Page + 1); + } + + async Task?> IPageableApiResponse, IEnumerable>. + FetchPageAsync(int page) + { + var requestUri = RequestUri + "&page=" + page; + + return await ((IPageableApiResponse, IEnumerable>)this).CurrentApiClient + ?.FetchDataAsync, IEnumerable>(requestUri)!; + } + + void IPageableApiResponse, IEnumerable>.RememberRequestUri(string requestUri) + { + // Remember full Uri without page + RequestUri = Regex.Replace(requestUri, @"page=\d*&?", ""); + } +} \ No newline at end of file