2023-01-28 23:54:23 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-03-01 16:33:54 +01:00
|
|
|
|
using System.Linq;
|
2023-01-28 23:54:23 +01:00
|
|
|
|
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;
|
2023-03-01 16:33:54 +01:00
|
|
|
|
using JpnCardsPokemonSdk.Api;
|
2023-01-28 23:54:23 +01:00
|
|
|
|
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())));
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 16:33:54 +01:00
|
|
|
|
private async Task<T?> FetchInternalAsync<T>(string requestUri)
|
|
|
|
|
{
|
|
|
|
|
var options = new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
|
|
|
|
IncludeFields = true,
|
|
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await _client.GetFromJsonAsync<T>(requestUri, options);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 23:54:23 +01:00
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-01 12:52:30 +01:00
|
|
|
|
var response = await _client.GetFromJsonAsync<TResponseGeneric>(requestUri, options);
|
2023-01-28 23:54:23 +01:00
|
|
|
|
|
2023-03-01 12:52:30 +01:00
|
|
|
|
// TODO: Find good way to handle pageable requests
|
|
|
|
|
/*if (response is IPageableApiResponse<TResponseType, TResponseGeneric> pageAbleApiResponse)
|
2023-01-28 23:54:23 +01:00
|
|
|
|
{
|
|
|
|
|
pageAbleApiResponse.CurrentApiClient = this;
|
|
|
|
|
pageAbleApiResponse.RememberRequestUri(requestUri);
|
2023-03-01 12:52:30 +01:00
|
|
|
|
}*/
|
2023-01-28 23:54:23 +01:00
|
|
|
|
|
2023-03-01 12:52:30 +01:00
|
|
|
|
var result = new TResponseType
|
|
|
|
|
{
|
|
|
|
|
Data = response
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
2023-01-28 23:54:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 16:33:54 +01:00
|
|
|
|
private string SetQuery(string? filter)
|
|
|
|
|
{
|
|
|
|
|
var result = "set";
|
|
|
|
|
|
|
|
|
|
return !string.IsNullOrEmpty(filter) ? $"{result}/{filter.TrimStart('/')}" : result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<Set>> FetchSetsAsync()
|
|
|
|
|
{
|
|
|
|
|
return await FetchInternalAsync<IEnumerable<Set>>(SetQuery(null)) ?? Enumerable.Empty<Set>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Set?> FetchSetById(int id)
|
|
|
|
|
{
|
|
|
|
|
return await FetchInternalAsync<Set>(SetQuery(id.ToString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Set?> FetchSetByUuid(int uuid)
|
|
|
|
|
{
|
|
|
|
|
return await FetchInternalAsync<Set>(SetQuery("uuid/" + uuid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2023-01-28 23:54:23 +01:00
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 12:52:30 +01:00
|
|
|
|
public async Task<SingleApiResponse<T>?> FetchByUuidAsync<T>(int uuid) where T : EndpointObject
|
2023-01-28 23:54:23 +01:00
|
|
|
|
{
|
|
|
|
|
var endpoint = EndpointFactory.GetApiEndpoint<T>();
|
|
|
|
|
|
|
|
|
|
return await FetchDataAsync<SingleApiResponse<T>, T>($"{endpoint.ApiUri()}/uuid={uuid}");
|
2023-03-01 16:33:54 +01:00
|
|
|
|
}*/
|
2023-01-28 23:54:23 +01:00
|
|
|
|
}
|