|
|
|
@ -1,5 +1,4 @@ |
|
|
|
|
using Newtonsoft.Json; |
|
|
|
|
using RestEase; |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Net.Http; |
|
|
|
@ -7,35 +6,45 @@ using System.Threading.Tasks; |
|
|
|
|
|
|
|
|
|
namespace Booru.Net |
|
|
|
|
{ |
|
|
|
|
public class E621Client : IPostsWrappedBooruClient<WrappedPosts<E621Image>>, IDisposable |
|
|
|
|
public class E621Client : IDisposable |
|
|
|
|
{ |
|
|
|
|
IPostsWrappedBooruClient<WrappedPosts<E621Image>> _api; |
|
|
|
|
private readonly HttpClient _api; |
|
|
|
|
private readonly JsonSerializerSettings settings; |
|
|
|
|
|
|
|
|
|
public E621Client() |
|
|
|
|
{ |
|
|
|
|
var httpClient = new HttpClient |
|
|
|
|
_api = new HttpClient |
|
|
|
|
{ |
|
|
|
|
BaseAddress = new Uri("https://e621.net/") |
|
|
|
|
}; |
|
|
|
|
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, |
|
|
|
|
NullValueHandling = NullValueHandling.Ignore |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Task<IReadOnlyList<E621Image>> GetImagesAsync(IEnumerable<string> tags) |
|
|
|
|
=> GetImagesAsync(string.Join("%20", tags)); |
|
|
|
|
|
|
|
|
|
_api = new RestClient(httpClient) |
|
|
|
|
public Task<IReadOnlyList<E621Image>> GetImagesAsync(params string[] tags) |
|
|
|
|
=> GetImagesAsync(string.Join("%20", tags)); |
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<E621Image>> GetImagesAsync(string tags) |
|
|
|
|
{ |
|
|
|
|
JsonSerializerSettings = settings |
|
|
|
|
}.For<IPostsWrappedBooruClient<WrappedPosts<E621Image>>>(); |
|
|
|
|
} |
|
|
|
|
var get = await _api.GetAsync($"posts.json?tags={tags}").ConfigureAwait(false); |
|
|
|
|
|
|
|
|
|
public Task<WrappedPosts<E621Image>> GetImagesAsync(IEnumerable<string> tags) |
|
|
|
|
=> _api.GetImagesAsync(tags); |
|
|
|
|
if (!get.IsSuccessStatusCode) |
|
|
|
|
throw new HttpRequestException($"Response failed with reason: \"({get.StatusCode}) {get.ReasonPhrase}\""); |
|
|
|
|
|
|
|
|
|
public Task<WrappedPosts<E621Image>> GetImagesAsync(params string[] tags) |
|
|
|
|
=> _api.GetImagesAsync(tags); |
|
|
|
|
var content = await get.Content.ReadAsStringAsync().ConfigureAwait(false); |
|
|
|
|
|
|
|
|
|
var posts = JsonConvert.DeserializeObject<WrappedPosts<E621Image>>(content, settings); |
|
|
|
|
|
|
|
|
|
return posts.Posts; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
|
{ |
|
|
|
|