longbridge_httpcli/
config.rs1use crate::HttpClientError;
2
3const HTTP_URL: &str = "https://openapi.longbridgeapp.com";
4
5#[derive(Debug, Clone)]
7pub struct HttpClientConfig {
8 pub(crate) http_url: String,
10 pub(crate) app_key: String,
12 pub(crate) app_secret: String,
14 pub(crate) access_token: String,
16}
17
18impl HttpClientConfig {
19 pub fn new(
21 app_key: impl Into<String>,
22 app_secret: impl Into<String>,
23 access_token: impl Into<String>,
24 ) -> Self {
25 Self {
26 http_url: HTTP_URL.to_string(),
27 app_key: app_key.into(),
28 app_secret: app_secret.into(),
29 access_token: access_token.into(),
30 }
31 }
32
33 pub fn from_env() -> Result<Self, HttpClientError> {
42 let _ = dotenv::dotenv();
43
44 let app_key =
45 std::env::var("LONGBRIDGE_APP_KEY").map_err(|_| HttpClientError::MissingEnvVar {
46 name: "LONGBRIDGE_APP_KEY",
47 })?;
48 let app_secret =
49 std::env::var("LONGBRIDGE_APP_SECRET").map_err(|_| HttpClientError::MissingEnvVar {
50 name: "LONGBRIDGE_APP_SECRET",
51 })?;
52 let access_token = std::env::var("LONGBRIDGE_ACCESS_TOKEN").map_err(|_| {
53 HttpClientError::MissingEnvVar {
54 name: "LONGBRIDGE_ACCESS_TOKEN",
55 }
56 })?;
57
58 let mut config = Self::new(app_key, app_secret, access_token);
59 if let Ok(http_url) = std::env::var("LONGBRIDGE_HTTP_URL") {
60 config.http_url = http_url;
61 }
62 Ok(config)
63 }
64
65 #[must_use]
70 pub fn http_url(self, url: impl Into<String>) -> Self {
71 Self {
72 http_url: url.into(),
73 ..self
74 }
75 }
76}