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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::sync::Arc;
use reqwest::{
header::{HeaderMap, HeaderName, HeaderValue},
Client, Method,
};
use serde::Deserialize;
use crate::{HttpClientConfig, HttpClientResult, RequestBuilder};
#[derive(Clone)]
pub struct HttpClient {
pub(crate) http_cli: Client,
pub(crate) config: Arc<HttpClientConfig>,
pub(crate) default_headers: HeaderMap,
}
impl HttpClient {
pub fn new(config: HttpClientConfig) -> Self {
Self {
http_cli: Client::new(),
config: Arc::new(config),
default_headers: HeaderMap::new(),
}
}
pub fn header<K, V>(mut self, key: K, value: V) -> Self
where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
{
let key = key.try_into();
let value = value.try_into();
if let (Ok(key), Ok(value)) = (key, value) {
self.default_headers.append(key, value);
}
self
}
#[inline]
pub fn request(&self, method: Method, path: impl Into<String>) -> RequestBuilder<(), (), ()> {
RequestBuilder::new(self.clone(), method, path)
}
pub async fn get_otp(&self) -> HttpClientResult<String> {
#[derive(Debug, Deserialize)]
struct Response {
otp: String,
}
Ok(self
.request(Method::GET, "/v1/socket/token")
.response::<Response>()
.send()
.await?
.otp)
}
pub async fn get_otp_v2(&self) -> HttpClientResult<String> {
#[derive(Debug, Deserialize)]
struct Response {
otp: String,
}
Ok(self
.request(Method::GET, "/v2/socket/token")
.response::<Response>()
.send()
.await?
.otp)
}
}