use crate::{
types::ImageList,
Error,
};
use std::time::Duration;
use url::Url;
const DEFAULT_USER_AGENT: &str = "nekos-rs";
#[derive(Debug, Clone)]
pub struct Client {
pub client: reqwest::Client,
}
impl Client {
pub fn new() -> Self {
Client {
client: reqwest::Client::builder()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(10))
.user_agent(DEFAULT_USER_AGENT)
.build()
.expect("failed to build client"),
}
}
pub async fn get_random(&self, nsfw: Option<bool>, count: u8) -> Result<ImageList, Error> {
let mut buf = itoa::Buffer::new();
let count_query = std::iter::once(("count", buf.format(count.min(100))));
let nsfw_query = nsfw.map(|nsfw| ("nsfw", if nsfw { "true" } else { "false" }));
let query = count_query.chain(nsfw_query);
let url = Url::parse_with_params("https://nekos.moe/api/v1/random/image", query)?;
Ok(self
.client
.get(url.as_str())
.send()
.await?
.error_for_status()?
.json()
.await?)
}
}
impl Default for Client {
fn default() -> Self {
Self::new()
}
}