vn_core/http/request/
get.rs1use super::request_json;
2use crate::error::{Error, Result};
3use crate::http::{Endpoint, FieldSet, UrlQueryParams};
4use crate::make_request;
5use crate::model::prelude::*;
6use crate::vndb::{Token, Vndb};
7use http::Method;
8use serde::de::DeserializeOwned;
9use std::sync::Weak;
10use tokio::sync::Semaphore;
11use tokio::time::Duration;
12
13pub struct Get {
14 vndb: Weak<Vndb>,
15}
16
17impl Get {
18 pub fn new(vndb: Weak<Vndb>) -> Self {
19 Self { vndb }
20 }
21
22 pub async fn auth_info(&self) -> Result<AuthInfo> {
23 let vndb = Vndb::upgrade(&self.vndb)?;
24 if vndb.token.is_none() {
25 return Err(Error::Unauthorized);
26 }
27
28 make_request!(vndb, get_json(Endpoint::AuthInfo))
29 }
30
31 pub async fn schema(&self) -> Result<Schema> {
32 let vndb = Vndb::upgrade(&self.vndb)?;
33 make_request!(vndb, get_json(Endpoint::Schema))
34 }
35
36 pub async fn stats(&self) -> Result<Stats> {
37 let vndb = Vndb::upgrade(&self.vndb)?;
38 make_request!(vndb, get_json(Endpoint::Stats))
39 }
40
41 pub async fn user<UserQuery, Field>(&self, user: UserQuery, fields: Field) -> Result<Users>
43 where
44 UserQuery: Into<UserUrlQuery>,
45 Field: Into<FieldSet<UserField>>,
46 {
47 let mut query = user.into().into_query();
48 if query.is_empty() {
49 return Ok(Users::default());
50 }
51
52 let fields = fields.into().into_url_query();
53 query.extend(fields);
54
55 let vndb = Vndb::upgrade(&self.vndb)?;
56 make_request!(vndb, get_json(Endpoint::User).query(query))
57 }
58}
59
60impl Clone for Get {
61 fn clone(&self) -> Self {
62 Self { vndb: Weak::clone(&self.vndb) }
63 }
64}
65
66#[bon::builder]
67async fn get_json<Json>(
68 #[builder(start_fn)] endpoint: Endpoint,
69 semaphore: Weak<Semaphore>,
70 query: Option<UrlQueryParams>,
71 token: Option<&Token>,
72 delay: Option<Duration>,
73 timeout: Option<Duration>,
74 user_agent: Option<&str>,
75) -> Result<Json>
76where
77 Json: DeserializeOwned,
78{
79 request_json::<(), _>(endpoint)
80 .method(Method::GET)
81 .semaphore(semaphore)
82 .maybe_query(query)
83 .maybe_token(token)
84 .maybe_delay(delay)
85 .maybe_timeout(timeout)
86 .maybe_user_agent(user_agent)
87 .call()
88 .await
89}