using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading.Tasks; using JpnCardsPokemonSdk.Api; using JpnCardsPokemonSdk.Client.Responses; namespace JpnCardsPokemonSdk.Client; public class ApiClient { private readonly HttpClient _client; #if NETCOREAPP3_1_OR_GREATER public ApiClient(SocketsHttpHandler handler) : this(new HttpClient(handler)) { } #endif public ApiClient() : this(new HttpClient()) { } public ApiClient(HttpClient client) { _client = client; _client.BaseAddress = new Uri("https://www.jpn-cards.com/v2/"); _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); _client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue( new ProductHeaderValue("JpnCardsPokemonSdkCS", GetType().Assembly.GetName().Version?.ToString()))); } private async Task FetchInternalAsync(string requestUri) { var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, IncludeFields = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; 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) { var result = "set"; return !string.IsNullOrEmpty(filter) ? $"{result}/{filter.TrimStart('/')}" : result; } public async Task> FetchSetsAsync() { return await FetchInternalAsync>(SetQuery(null)) ?? Enumerable.Empty(); } public async Task FetchSetById(int id) { return await FetchInternalAsync(SetQuery(id.ToString())); } public async Task FetchSetByUuid(int uuid) { return await FetchInternalAsync(SetQuery("uuid/" + uuid)); } /* 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}"); }*/ }