use super::{Client, ClientBuilder};
use crate::error::Result;
use crate::model::danbooru::*;
use reqwest::header::{self, HeaderMap, HeaderValue};
fn get_headers() -> HeaderMap {
let mut headers = HeaderMap::with_capacity(1);
headers.insert(
header::USER_AGENT,
HeaderValue::from_static("booru-rs/0.3.0"),
);
headers
}
#[derive(Debug)]
pub struct DanbooruClient(ClientBuilder<Self>);
impl From<ClientBuilder<Self>> for DanbooruClient {
fn from(value: ClientBuilder<Self>) -> Self {
Self(value)
}
}
impl Client for DanbooruClient {
type Post = DanbooruPost;
type Rating = DanbooruRating;
const URL: &'static str = "https://danbooru.donmai.us";
const SORT: &'static str = "order:";
const MAX_TAGS: Option<usize> = Some(2);
async fn get_by_id(&self, id: u32) -> Result<Self::Post> {
let builder = &self.0;
let url = &builder.url;
let response = builder
.client
.get(format!("{url}/posts/{id}.json"))
.headers(get_headers())
.send()
.await?
.json::<DanbooruPost>()
.await?;
Ok(response)
}
async fn get(&self) -> Result<Vec<Self::Post>> {
let builder = &self.0;
let tag_string = builder.tags.join(" ");
let url = &builder.url;
let response = builder
.client
.get(format!("{url}/posts.json"))
.headers(get_headers())
.query(&[
("limit", builder.limit.to_string()),
("page", builder.page.to_string()),
("tags", tag_string),
])
.send()
.await?
.json::<Vec<DanbooruPost>>()
.await?;
Ok(response)
}
}