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