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
use crate::HttpClientError;
const HTTP_URL: &str = "https://openapi.longbridge.global";
#[derive(Debug, Clone)]
pub struct HttpClientConfig {
pub(crate) http_url: String,
pub(crate) app_key: String,
pub(crate) app_secret: String,
pub(crate) access_token: String,
}
impl HttpClientConfig {
pub fn new(
app_key: impl Into<String>,
app_secret: impl Into<String>,
access_token: impl Into<String>,
) -> Self {
Self {
http_url: HTTP_URL.to_string(),
app_key: app_key.into(),
app_secret: app_secret.into(),
access_token: access_token.into(),
}
}
pub fn from_env() -> Result<Self, HttpClientError> {
let app_key =
std::env::var("LONGBRIDGE_APP_KEY").map_err(|_| HttpClientError::MissingEnvVar {
name: "LONGBRIDGE_APP_KEY",
})?;
let app_secret =
std::env::var("LONGBRIDGE_APP_SECRET").map_err(|_| HttpClientError::MissingEnvVar {
name: "LONGBRIDGE_APP_SECRET",
})?;
let access_token = std::env::var("LONGBRIDGE_ACCESS_TOKEN").map_err(|_| {
HttpClientError::MissingEnvVar {
name: "LONGBRIDGE_ACCESS_TOKEN",
}
})?;
Ok(Self::new(app_key, app_secret, access_token))
}
#[must_use]
pub fn http_url(self, url: impl Into<String>) -> Self {
Self {
http_url: url.into(),
..self
}
}
}