From 3036d0dda014952403fe268025a17c4579cad2b5 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 1 Mar 2023 21:47:26 +0100 Subject: [PATCH] Removes obsolete endpoint and response logics --- src/Api/Card.cs | 3 +- src/Api/Set.cs | 4 +- src/Client/ApiClient.cs | 51 ---------------- src/Client/Endpoints/CardEndpoint.cs | 19 ------ src/Client/Endpoints/EndpointFactory.cs | 36 ----------- src/Client/Endpoints/EndpointObject.cs | 5 -- src/Client/Endpoints/IApiEndpoint.cs | 10 ---- src/Client/Endpoints/SetEndpoint.cs | 19 ------ src/Client/Responses/EnumerableApiResponse.cs | 22 ------- src/Client/Responses/IApiResponse.cs | 6 -- src/Client/Responses/IPageableApiResponse.cs | 32 ---------- src/Client/Responses/PageableApiResponse.cs | 59 ------------------- src/Client/Responses/SingleApiResponse.cs | 8 --- 13 files changed, 3 insertions(+), 271 deletions(-) delete mode 100644 src/Client/Endpoints/CardEndpoint.cs delete mode 100644 src/Client/Endpoints/EndpointFactory.cs delete mode 100644 src/Client/Endpoints/EndpointObject.cs delete mode 100644 src/Client/Endpoints/IApiEndpoint.cs delete mode 100644 src/Client/Endpoints/SetEndpoint.cs delete mode 100644 src/Client/Responses/EnumerableApiResponse.cs delete mode 100644 src/Client/Responses/IApiResponse.cs delete mode 100644 src/Client/Responses/IPageableApiResponse.cs delete mode 100644 src/Client/Responses/PageableApiResponse.cs delete mode 100644 src/Client/Responses/SingleApiResponse.cs diff --git a/src/Api/Card.cs b/src/Api/Card.cs index 06118d1..ba73027 100644 --- a/src/Api/Card.cs +++ b/src/Api/Card.cs @@ -1,9 +1,8 @@ using System.Text.Json.Serialization; -using JpnCardsPokemonSdk.Client.Endpoints; namespace JpnCardsPokemonSdk.Api; -public class Card : EndpointObject +public class Card { public string? Name { get; set; } diff --git a/src/Api/Set.cs b/src/Api/Set.cs index 5c5c4f7..29d4da9 100644 --- a/src/Api/Set.cs +++ b/src/Api/Set.cs @@ -1,9 +1,9 @@ using System.Text.Json.Serialization; -using JpnCardsPokemonSdk.Client.Endpoints; +using JpnCardsPokemonSdk.Utils.JsonConverter; namespace JpnCardsPokemonSdk.Api; -public class Set : EndpointObject +public class Set { static Set() { diff --git a/src/Client/ApiClient.cs b/src/Client/ApiClient.cs index 40030b6..2bb6596 100644 --- a/src/Client/ApiClient.cs +++ b/src/Client/ApiClient.cs @@ -8,7 +8,6 @@ using System.Text.Json; using System.Text.Json.Serialization; using System.Threading.Tasks; using JpnCardsPokemonSdk.Api; -using JpnCardsPokemonSdk.Client.Responses; using JpnCardsPokemonSdk.Utils.QueryFilter; namespace JpnCardsPokemonSdk.Client; @@ -49,33 +48,6 @@ public class ApiClient return await _client.GetFromJsonAsync(requestUri, options); } - public async Task FetchDataAsync(string requestUri) - where TResponseType : IApiResponse, new() - { - var options = new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true, - IncludeFields = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull - }; - - var response = await _client.GetFromJsonAsync(requestUri, options); - - // TODO: Find good way to handle pageable requests - /*if (response is IPageableApiResponse pageAbleApiResponse) - { - pageAbleApiResponse.CurrentApiClient = this; - pageAbleApiResponse.RememberRequestUri(requestUri); - }*/ - - var result = new TResponseType - { - Data = response - }; - - return result; - } - private string SetQuery(string? filter) { return !string.IsNullOrEmpty(filter) ? $"set/{filter.TrimStart('/')}" : "set"; @@ -118,27 +90,4 @@ public class ApiClient { return await FetchCardsAsync(filterBuilder.BuildQueryString()); } - - /* - public async Task?> FetchDataAsync(string? query = null, int page = 1) - where T : EndpointObject - { - var endpoint = EndpointFactory.GetApiEndpoint(); - - return await FetchDataAsync, IEnumerable>($"{endpoint.ApiUri()}?page={page}"); - } - - public async Task?> FetchByIdAsync(int id) where T : EndpointObject - { - var endpoint = EndpointFactory.GetApiEndpoint(); - - return await FetchDataAsync, T>($"{endpoint.ApiUri()}/id={id}"); - } - - public async Task?> FetchByUuidAsync(int uuid) where T : EndpointObject - { - var endpoint = EndpointFactory.GetApiEndpoint(); - - return await FetchDataAsync, T>($"{endpoint.ApiUri()}/uuid={uuid}"); - }*/ } \ No newline at end of file diff --git a/src/Client/Endpoints/CardEndpoint.cs b/src/Client/Endpoints/CardEndpoint.cs deleted file mode 100644 index 7d58460..0000000 --- a/src/Client/Endpoints/CardEndpoint.cs +++ /dev/null @@ -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}"; - } -} \ No newline at end of file diff --git a/src/Client/Endpoints/EndpointFactory.cs b/src/Client/Endpoints/EndpointFactory.cs deleted file mode 100644 index 0c71ca7..0000000 --- a/src/Client/Endpoints/EndpointFactory.cs +++ /dev/null @@ -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 EndpointMapping { get; } = new(); - - public static void RegisterTypeEndpoint(IApiEndpoint endpoint) where T : EndpointObject - { - EndpointMapping.Add(typeof(T), endpoint); - } - - public static IApiEndpoint GetApiEndpoint() 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}"); - } -} \ No newline at end of file diff --git a/src/Client/Endpoints/EndpointObject.cs b/src/Client/Endpoints/EndpointObject.cs deleted file mode 100644 index d84ecf2..0000000 --- a/src/Client/Endpoints/EndpointObject.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace JpnCardsPokemonSdk.Client.Endpoints; - -public abstract class EndpointObject -{ -} \ No newline at end of file diff --git a/src/Client/Endpoints/IApiEndpoint.cs b/src/Client/Endpoints/IApiEndpoint.cs deleted file mode 100644 index 8f4dae0..0000000 --- a/src/Client/Endpoints/IApiEndpoint.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace JpnCardsPokemonSdk.Client.Endpoints; - -public interface IApiEndpoint -{ - string ApiUri(); - - string IdQuery(int id); - - string UuidQuery(int uuid); -} \ No newline at end of file diff --git a/src/Client/Endpoints/SetEndpoint.cs b/src/Client/Endpoints/SetEndpoint.cs deleted file mode 100644 index 2597718..0000000 --- a/src/Client/Endpoints/SetEndpoint.cs +++ /dev/null @@ -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}"; - } -} \ No newline at end of file diff --git a/src/Client/Responses/EnumerableApiResponse.cs b/src/Client/Responses/EnumerableApiResponse.cs deleted file mode 100644 index 168f883..0000000 --- a/src/Client/Responses/EnumerableApiResponse.cs +++ /dev/null @@ -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 : IApiResponse>, IEnumerable where T : EndpointObject -{ - [JsonPropertyName("")] public IEnumerable? Data { get; set; } - - public IEnumerator GetEnumerator() - { - return Data?.GetEnumerator() ?? Enumerable.Empty().GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } -} \ No newline at end of file diff --git a/src/Client/Responses/IApiResponse.cs b/src/Client/Responses/IApiResponse.cs deleted file mode 100644 index 00bbbc8..0000000 --- a/src/Client/Responses/IApiResponse.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace JpnCardsPokemonSdk.Client.Responses; - -public interface IApiResponse -{ - T? Data { get; set; } -} \ No newline at end of file diff --git a/src/Client/Responses/IPageableApiResponse.cs b/src/Client/Responses/IPageableApiResponse.cs deleted file mode 100644 index a9da98d..0000000 --- a/src/Client/Responses/IPageableApiResponse.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Threading.Tasks; - -namespace JpnCardsPokemonSdk.Client.Responses; - -public interface IPageableApiResponse - where TResponseType : IApiResponse -{ - 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 FetchNextPageAsync(); - - Task FetchPageAsync(int page); -} \ No newline at end of file diff --git a/src/Client/Responses/PageableApiResponse.cs b/src/Client/Responses/PageableApiResponse.cs deleted file mode 100644 index a8c31a0..0000000 --- a/src/Client/Responses/PageableApiResponse.cs +++ /dev/null @@ -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 : 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 diff --git a/src/Client/Responses/SingleApiResponse.cs b/src/Client/Responses/SingleApiResponse.cs deleted file mode 100644 index 6cbe381..0000000 --- a/src/Client/Responses/SingleApiResponse.cs +++ /dev/null @@ -1,8 +0,0 @@ -using JpnCardsPokemonSdk.Client.Endpoints; - -namespace JpnCardsPokemonSdk.Client.Responses; - -public class SingleApiResponse : IApiResponse where T : EndpointObject -{ - T? IApiResponse.Data { get; set; } -} \ No newline at end of file