longbridge_httpcli/
config.rs

1use crate::HttpClientError;
2
3const HTTP_URL: &str = "https://openapi.longbridgeapp.com";
4
5/// Configuration options for Http client
6#[derive(Debug, Clone)]
7pub struct HttpClientConfig {
8    /// HTTP API url
9    pub(crate) http_url: String,
10    /// App key
11    pub(crate) app_key: String,
12    /// App secret
13    pub(crate) app_secret: String,
14    /// Access token
15    pub(crate) access_token: String,
16}
17
18impl HttpClientConfig {
19    /// Create a new `HttpClientConfig`
20    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    /// Create a new `HttpClientConfig` from the given environment variables
34    ///
35    /// # Variables
36    ///
37    /// - LONGBRIDGE_APP_KEY
38    /// - LONGBRIDGE_APP_SECRET
39    /// - LONGBRIDGE_ACCESS_TOKEN
40    /// - LONGBRIDGE_HTTP_URL
41    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    /// Specifies the url of the OpenAPI server.
66    ///
67    /// Default: <https://openapi.longbridgeapp.com>
68    /// NOTE: Usually you don't need to change it.
69    #[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}