rusty_booru/gelbooru/
client.rs1use derive_more::From;
2
3use crate::{
4 generic::AutoCompleteItem,
5 shared::{
6 self,
7 client::{
8 ClientBuilder, ClientInformation, ClientQueryDispatcher, ClientTypes,
9 ImplementedWithCommonQuery, QueryDispatcher, QueryLike, QueryMode, WithCommonQuery,
10 },
11 },
12};
13
14use super::*;
15
16#[derive(From, Clone)]
18pub struct GelbooruClient(pub ClientBuilder<Self>);
19
20impl ClientInformation for GelbooruClient {
21 const URL: &'static str = "https://gelbooru.com";
22 const SORT: &'static str = "sort:";
23}
24
25impl ClientTypes for GelbooruClient {
26 type Post = GelbooruPost;
27 type Rating = GelbooruRating;
28}
29
30impl WithCommonQuery for GelbooruClient {
31 fn common_query_type() -> crate::shared::client::QueryLike {
32 QueryLike::Gelbooru
33 }
34}
35
36impl QueryDispatcher<GelbooruClient> for ClientQueryDispatcher<GelbooruClient> {
37 async fn get_autocomplete<In: Into<String> + Send>(
38 &self,
39 input: In,
40 ) -> Result<Vec<crate::generic::AutoCompleteItem>, reqwest::Error> {
41 self.builder
42 .client
43 .get(format!("{}/index.php", self.builder.url))
44 .query(&[
45 ("limit", self.query.limit.to_string().as_str()),
46 ("page", "autocomplete2"),
47 ("type", "tag_query"),
48 ("term", &input.into()),
49 ])
50 .send()
51 .await?
52 .json::<Vec<AutoCompleteItem>>()
53 .await
54 }
55
56 async fn get_by_id(&self, id: u32) -> Result<Option<GelbooruPost>, shared::Error> {
57 self.builder
58 .client
59 .get(format!("{}/index.php", &self.builder.url))
60 .query(&Self::get_query(QueryMode::Single(id)))
61 .send()
62 .await?
63 .json::<GelbooruResponse>()
64 .await
65 .map(|r| r.posts.into_iter().next())
66 .map_err(Into::into)
67 }
68
69 async fn get(&self) -> Result<Vec<GelbooruPost>, shared::Error> {
70 self.builder
71 .client
72 .get(format!("{}/index.php", &self.builder.url))
73 .query(&Self::get_query(QueryMode::Multiple(&self.query)))
74 .send()
75 .await?
76 .json::<GelbooruResponse>()
77 .await
78 .map(|r| r.posts)
79 .map_err(Into::into)
80 }
81}