Removes obsolete endpoint and response logics

This commit is contained in:
Serraniel 2023-03-01 21:47:26 +01:00
parent 62a4ac5c4b
commit 3036d0dda0
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3
13 changed files with 3 additions and 271 deletions

View file

@ -1,9 +1,8 @@
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using JpnCardsPokemonSdk.Client.Endpoints;
namespace JpnCardsPokemonSdk.Api; namespace JpnCardsPokemonSdk.Api;
public class Card : EndpointObject public class Card
{ {
public string? Name { get; set; } public string? Name { get; set; }

View file

@ -1,9 +1,9 @@
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using JpnCardsPokemonSdk.Client.Endpoints; using JpnCardsPokemonSdk.Utils.JsonConverter;
namespace JpnCardsPokemonSdk.Api; namespace JpnCardsPokemonSdk.Api;
public class Set : EndpointObject public class Set
{ {
static Set() static Set()
{ {

View file

@ -8,7 +8,6 @@ using System.Text.Json;
using System.Text.Json.Serialization; 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.Utils.QueryFilter; using JpnCardsPokemonSdk.Utils.QueryFilter;
namespace JpnCardsPokemonSdk.Client; namespace JpnCardsPokemonSdk.Client;
@ -49,33 +48,6 @@ public class ApiClient
return await _client.GetFromJsonAsync<T>(requestUri, options); return await _client.GetFromJsonAsync<T>(requestUri, options);
} }
public async Task<TResponseType?> FetchDataAsync<TResponseType, TResponseGeneric>(string requestUri)
where TResponseType : IApiResponse<TResponseGeneric>, new()
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
IncludeFields = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var response = await _client.GetFromJsonAsync<TResponseGeneric>(requestUri, options);
// TODO: Find good way to handle pageable requests
/*if (response is IPageableApiResponse<TResponseType, TResponseGeneric> pageAbleApiResponse)
{
pageAbleApiResponse.CurrentApiClient = this;
pageAbleApiResponse.RememberRequestUri(requestUri);
}*/
var result = new TResponseType
{
Data = response
};
return result;
}
private string SetQuery(string? filter) private string SetQuery(string? filter)
{ {
return !string.IsNullOrEmpty(filter) ? $"set/{filter.TrimStart('/')}" : "set"; return !string.IsNullOrEmpty(filter) ? $"set/{filter.TrimStart('/')}" : "set";
@ -118,27 +90,4 @@ public class ApiClient
{ {
return await FetchCardsAsync(filterBuilder.BuildQueryString()); return await FetchCardsAsync(filterBuilder.BuildQueryString());
} }
/*
public async Task<EnumerableApiResponse<T>?> FetchDataAsync<T>(string? query = null, int page = 1)
where T : EndpointObject
{
var endpoint = EndpointFactory.GetApiEndpoint<T>();
return await FetchDataAsync<EnumerableApiResponse<T>, IEnumerable<T>>($"{endpoint.ApiUri()}?page={page}");
}
public async Task<SingleApiResponse<T>?> FetchByIdAsync<T>(int id) where T : EndpointObject
{
var endpoint = EndpointFactory.GetApiEndpoint<T>();
return await FetchDataAsync<SingleApiResponse<T>, T>($"{endpoint.ApiUri()}/id={id}");
}
public async Task<SingleApiResponse<T>?> FetchByUuidAsync<T>(int uuid) where T : EndpointObject
{
var endpoint = EndpointFactory.GetApiEndpoint<T>();
return await FetchDataAsync<SingleApiResponse<T>, T>($"{endpoint.ApiUri()}/uuid={uuid}");
}*/
} }

View file

@ -1,19 +0,0 @@
namespace JpnCardsPokemonSdk.Client.Endpoints;
internal class CardEndpoint : IApiEndpoint
{
string IApiEndpoint.ApiUri()
{
return "card";
}
string IApiEndpoint.IdQuery(int id)
{
return $"{((IApiEndpoint)this).ApiUri()}/id={id}";
}
string IApiEndpoint.UuidQuery(int uuid)
{
return $"{((IApiEndpoint)this).ApiUri()}/uuid={uuid}";
}
}

View file

@ -1,36 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace JpnCardsPokemonSdk.Client.Endpoints;
internal static class EndpointFactory
{
static EndpointFactory()
{
var knownTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t =>
typeof(EndpointObject).IsAssignableFrom(t) &&
t != typeof(EndpointObject));
foreach (var knownType in knownTypes) RuntimeHelpers.RunClassConstructor(knownType.TypeHandle);
}
private static Dictionary<Type, IApiEndpoint> EndpointMapping { get; } = new();
public static void RegisterTypeEndpoint<T>(IApiEndpoint endpoint) where T : EndpointObject
{
EndpointMapping.Add(typeof(T), endpoint);
}
public static IApiEndpoint GetApiEndpoint<T>() where T : EndpointObject
{
foreach (var endpointMappingKey in EndpointMapping.Keys.Where(endpointMappingKey =>
typeof(T) == endpointMappingKey))
return EndpointMapping[endpointMappingKey];
// Todo: Custom exception class
throw new Exception($"No endpoint had been found for ${typeof(T).FullName}");
}
}

View file

@ -1,5 +0,0 @@
namespace JpnCardsPokemonSdk.Client.Endpoints;
public abstract class EndpointObject
{
}

View file

@ -1,10 +0,0 @@
namespace JpnCardsPokemonSdk.Client.Endpoints;
public interface IApiEndpoint
{
string ApiUri();
string IdQuery(int id);
string UuidQuery(int uuid);
}

View file

@ -1,19 +0,0 @@
namespace JpnCardsPokemonSdk.Client.Endpoints;
internal class SetEndpoint : IApiEndpoint
{
string IApiEndpoint.ApiUri()
{
return "set";
}
string IApiEndpoint.IdQuery(int id)
{
return $"{((IApiEndpoint)this).ApiUri()}/{id}";
}
string IApiEndpoint.UuidQuery(int uuid)
{
return $"{((IApiEndpoint)this).ApiUri()}/uuid/{uuid}";
}
}

View file

@ -1,22 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using JpnCardsPokemonSdk.Client.Endpoints;
namespace JpnCardsPokemonSdk.Client.Responses;
public class EnumerableApiResponse<T> : IApiResponse<IEnumerable<T>>, IEnumerable<T> where T : EndpointObject
{
[JsonPropertyName("")] public IEnumerable<T>? Data { get; set; }
public IEnumerator<T> GetEnumerator()
{
return Data?.GetEnumerator() ?? Enumerable.Empty<T>().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

View file

@ -1,6 +0,0 @@
namespace JpnCardsPokemonSdk.Client.Responses;
public interface IApiResponse<T>
{
T? Data { get; set; }
}

View file

@ -1,32 +0,0 @@
using System.Threading.Tasks;
namespace JpnCardsPokemonSdk.Client.Responses;
public interface IPageableApiResponse<TResponseType, TResponseGeneric>
where TResponseType : IApiResponse<TResponseGeneric>
{
ApiClient? CurrentApiClient { get; set; }
int Page { get; set; }
int PageSize { get; set; }
int Count { get; set; }
int TotalCount { get; set; }
#if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
bool HasNextPage()
{
return Page < TotalCount;
}
#else
bool HasNextPage();
#endif
void RememberRequestUri(string requestUri);
Task<TResponseType> FetchNextPageAsync();
Task<TResponseType> FetchPageAsync(int page);
}

View file

@ -1,59 +0,0 @@
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*&?", "");
}
}

View file

@ -1,8 +0,0 @@
using JpnCardsPokemonSdk.Client.Endpoints;
namespace JpnCardsPokemonSdk.Client.Responses;
public class SingleApiResponse<T> : IApiResponse<T> where T : EndpointObject
{
T? IApiResponse<T>.Data { get; set; }
}