Refactors API fetching for tests and fixes a typo
This commit is contained in:
parent
0379b9e6f6
commit
48209a130b
|
@ -45,15 +45,21 @@ public class ApiClient
|
||||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||||
};
|
};
|
||||||
|
|
||||||
var response = await _client.GetFromJsonAsync<TResponseType>(requestUri, options);
|
var response = await _client.GetFromJsonAsync<TResponseGeneric>(requestUri, options);
|
||||||
|
|
||||||
if (response is IPageableApiResponse<TResponseType, TResponseGeneric> pageAbleApiResponse)
|
// TODO: Find good way to handle pageable requests
|
||||||
|
/*if (response is IPageableApiResponse<TResponseType, TResponseGeneric> pageAbleApiResponse)
|
||||||
{
|
{
|
||||||
pageAbleApiResponse.CurrentApiClient = this;
|
pageAbleApiResponse.CurrentApiClient = this;
|
||||||
pageAbleApiResponse.RememberRequestUri(requestUri);
|
pageAbleApiResponse.RememberRequestUri(requestUri);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
return response;
|
var result = new TResponseType
|
||||||
|
{
|
||||||
|
Data = response
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
@ -71,7 +77,7 @@ public class ApiClient
|
||||||
return await FetchDataAsync<SingleApiResponse<T>, T>($"{endpoint.ApiUri()}/id={id}");
|
return await FetchDataAsync<SingleApiResponse<T>, T>($"{endpoint.ApiUri()}/id={id}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<SingleApiResponse<T>?> FetchByUuigAsync<T>(int uuid) where T : EndpointObject
|
public async Task<SingleApiResponse<T>?> FetchByUuidAsync<T>(int uuid) where T : EndpointObject
|
||||||
{
|
{
|
||||||
var endpoint = EndpointFactory.GetApiEndpoint<T>();
|
var endpoint = EndpointFactory.GetApiEndpoint<T>();
|
||||||
|
|
||||||
|
|
|
@ -1,58 +1,22 @@
|
||||||
using System;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.RegularExpressions;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Text.Json.Serialization;
|
||||||
using JpnCardsPokemonSdk.Client.Endpoints;
|
using JpnCardsPokemonSdk.Client.Endpoints;
|
||||||
|
|
||||||
namespace JpnCardsPokemonSdk.Client.Responses;
|
namespace JpnCardsPokemonSdk.Client.Responses;
|
||||||
|
|
||||||
public class EnumerableApiResponse<T> : IApiResponse<IEnumerable<T>>,
|
public class EnumerableApiResponse<T> : IApiResponse<IEnumerable<T>>, IEnumerable<T> where T : EndpointObject
|
||||||
IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>> where T : EndpointObject
|
|
||||||
{
|
{
|
||||||
private string? RequestUri { get; set; }
|
[JsonPropertyName("")] public IEnumerable<T>? Data { get; set; }
|
||||||
|
|
||||||
public int TotalPages => (int)Math.Ceiling((decimal)(
|
public IEnumerator<T> GetEnumerator()
|
||||||
(IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).TotalCount / (
|
|
||||||
(IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).PageSize);
|
|
||||||
|
|
||||||
public IEnumerable<T>? Data { get; set; }
|
|
||||||
|
|
||||||
ApiClient? IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.CurrentApiClient { get; set; }
|
|
||||||
|
|
||||||
int IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.Page { get; set; }
|
|
||||||
|
|
||||||
int IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.PageSize { get; set; }
|
|
||||||
|
|
||||||
int IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.Count { get; set; }
|
|
||||||
|
|
||||||
#if !(NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER)
|
|
||||||
bool IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.HasNextPage()
|
|
||||||
{
|
{
|
||||||
return ((IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).Page < ((IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).TotalCount;
|
return Data?.GetEnumerator() ?? Enumerable.Empty<T>().GetEnumerator();
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.TotalCount { get; set; }
|
|
||||||
|
|
||||||
async Task<EnumerableApiResponse<T>> IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.
|
|
||||||
FetchNextPageAsync()
|
|
||||||
{
|
|
||||||
return await ((IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).FetchPageAsync((
|
|
||||||
(IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).Page + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task<EnumerableApiResponse<T>?> IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
FetchPageAsync(int page)
|
|
||||||
{
|
{
|
||||||
var requestUri = RequestUri + "&page=" + page;
|
return GetEnumerator();
|
||||||
|
|
||||||
return await ((IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).CurrentApiClient
|
|
||||||
?.FetchDataAsync<EnumerableApiResponse<T>, IEnumerable<T>>(requestUri)!;
|
|
||||||
}
|
|
||||||
|
|
||||||
void IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>.RememberRequestUri(string requestUri)
|
|
||||||
{
|
|
||||||
// Remember full Uri without page
|
|
||||||
RequestUri = Regex.Replace(requestUri, @"page=\d*&?", "");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
59
src/Client/Responses/PageableApiResponse.cs
Normal file
59
src/Client/Responses/PageableApiResponse.cs
Normal file
|
@ -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<T> : EnumerableApiResponse<T>,
|
||||||
|
IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>
|
||||||
|
where T : EndpointObject
|
||||||
|
{
|
||||||
|
private string? RequestUri { get; set; }
|
||||||
|
|
||||||
|
public int TotalPages => (int)Math.Ceiling((decimal)(
|
||||||
|
(IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).TotalCount / (
|
||||||
|
(IPageableApiResponse<EnumerableApiResponse<T>, IEnumerable<T>>)this).PageSize);
|
||||||
|
|
||||||
|
ApiClient? IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.CurrentApiClient { get; set; }
|
||||||
|
|
||||||
|
int IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.Page { get; set; }
|
||||||
|
|
||||||
|
int IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.PageSize { get; set; }
|
||||||
|
|
||||||
|
int IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.Count { get; set; }
|
||||||
|
|
||||||
|
int IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.TotalCount { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
#if !(NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER)
|
||||||
|
bool IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.HasNextPage()
|
||||||
|
{
|
||||||
|
return ((IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>)this).Page <
|
||||||
|
((IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>)this).TotalCount;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
async Task<PageableApiResponse<T>> IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.
|
||||||
|
FetchNextPageAsync()
|
||||||
|
{
|
||||||
|
return await ((IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>)this).FetchPageAsync((
|
||||||
|
(IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>)this).Page + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<PageableApiResponse<T>?> IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.
|
||||||
|
FetchPageAsync(int page)
|
||||||
|
{
|
||||||
|
var requestUri = RequestUri + "&page=" + page;
|
||||||
|
|
||||||
|
return await ((IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>)this).CurrentApiClient
|
||||||
|
?.FetchDataAsync<PageableApiResponse<T>, IEnumerable<T>>(requestUri)!;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IPageableApiResponse<PageableApiResponse<T>, IEnumerable<T>>.RememberRequestUri(string requestUri)
|
||||||
|
{
|
||||||
|
// Remember full Uri without page
|
||||||
|
RequestUri = Regex.Replace(requestUri, @"page=\d*&?", "");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue