@ -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 > > , I Disposable
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 ( )
{