|
|
@ -1,5 +1,4 @@ |
|
|
|
using Newtonsoft.Json; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using RestEase; |
|
|
|
|
|
|
|
using System; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Net.Http; |
|
|
@ -7,35 +6,43 @@ using System.Threading.Tasks; |
|
|
|
|
|
|
|
|
|
|
|
namespace Booru.Net |
|
|
|
namespace Booru.Net |
|
|
|
{ |
|
|
|
{ |
|
|
|
public class SafebooruClient : IPHPBooruClient<SafebooruImage>, IDisposable |
|
|
|
public class SafebooruClient : IDisposable |
|
|
|
{ |
|
|
|
{ |
|
|
|
IPHPBooruClient<SafebooruImage> _api; |
|
|
|
private readonly HttpClient _api; |
|
|
|
|
|
|
|
private readonly JsonSerializerSettings settings; |
|
|
|
|
|
|
|
|
|
|
|
public SafebooruClient() |
|
|
|
public SafebooruClient() |
|
|
|
{ |
|
|
|
{ |
|
|
|
var httpClient = new HttpClient |
|
|
|
_api = new HttpClient |
|
|
|
{ |
|
|
|
{ |
|
|
|
BaseAddress = new Uri("https://safebooru.org/") |
|
|
|
BaseAddress = new Uri("https://safebooru.org/") |
|
|
|
}; |
|
|
|
}; |
|
|
|
httpClient.DefaultRequestHeaders.Add("User-Agent", $"Booru.Net/v{Props.LibraryVersion} (https://github.com/exsersewo/Booru.Net)"); |
|
|
|
_api.DefaultRequestHeaders.Add("User-Agent", $"Booru.Net/v{Props.LibraryVersion} (https://github.com/exsersewo/Booru.Net)"); |
|
|
|
|
|
|
|
|
|
|
|
JsonSerializerSettings settings = new JsonSerializerSettings |
|
|
|
settings = new JsonSerializerSettings |
|
|
|
{ |
|
|
|
{ |
|
|
|
Formatting = Formatting.Indented, |
|
|
|
Formatting = Formatting.Indented, |
|
|
|
NullValueHandling = NullValueHandling.Ignore |
|
|
|
NullValueHandling = NullValueHandling.Ignore |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
_api = new RestClient(httpClient) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
JsonSerializerSettings = settings |
|
|
|
|
|
|
|
}.For<IPHPBooruClient<SafebooruImage>>(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Task<IReadOnlyList<SafebooruImage>> GetImagesAsync(IEnumerable<string> tags) |
|
|
|
public Task<IReadOnlyList<SafebooruImage>> GetImagesAsync(IEnumerable<string> tags) |
|
|
|
=> _api.GetImagesAsync(tags); |
|
|
|
=> GetImagesAsync(string.Join("%20", tags)); |
|
|
|
|
|
|
|
|
|
|
|
public Task<IReadOnlyList<SafebooruImage>> GetImagesAsync(params string[] tags) |
|
|
|
public Task<IReadOnlyList<SafebooruImage>> GetImagesAsync(params string[] tags) |
|
|
|
=> _api.GetImagesAsync(tags); |
|
|
|
=> GetImagesAsync(string.Join("%20", tags)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<SafebooruImage>> GetImagesAsync(string tags) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var get = await _api.GetAsync($"index.php?page=dapi&s=post&q=index&json=1&tags={tags}").ConfigureAwait(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!get.IsSuccessStatusCode) |
|
|
|
|
|
|
|
throw new HttpRequestException($"Response failed with reason: \"({get.StatusCode}) {get.ReasonPhrase}\""); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var content = await get.Content.ReadAsStringAsync().ConfigureAwait(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<IReadOnlyList<SafebooruImage>>(content, settings); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
{ |
|
|
|