-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_handler.rs
More file actions
38 lines (33 loc) · 1.15 KB
/
Copy pathrequest_handler.rs
File metadata and controls
38 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use crate::request::tgju_data::{ApiResponse, TgjuData};
use reqwest::Client;
use uuid::Uuid;
pub struct RequestHandler {
client: Client,
}
impl RequestHandler {
pub fn new() -> Self {
let client = Client::builder()
.use_rustls_tls()
.timeout(std::time::Duration::from_secs(10))
.build()
.expect("reqwest client [RequestGold] build failed");
Self { client }
}
pub async fn start(&self) -> Result<ApiResponse, reqwest::Error> {
let uuid = Uuid::new_v4();
let url = format!("https://call3.tgju.org/ajax.json?rev={}", uuid);
let resp = self
.client
.get(url)
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
.send()
.await?
.error_for_status()?; // convert 4xx/5xx into Err
let api: ApiResponse = resp.json().await?;
Ok(api)
}
pub async fn tgju_typed(&self) -> Result<TgjuData, reqwest::Error> {
let api = self.start().await?;
Ok(TgjuData::from(&api.current))
}
}