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
use crate::HttpClientError;

const HTTP_URL: &str = "https://openapi.longbridgeapp.com";

/// Configuration options for Http client
#[derive(Debug, Clone)]
pub struct HttpClientConfig {
    /// HTTP API url
    pub(crate) http_url: String,
    /// App key
    pub(crate) app_key: String,
    /// App secret
    pub(crate) app_secret: String,
    /// Access token
    pub(crate) access_token: String,
}

impl HttpClientConfig {
    /// Create a new `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(),
        }
    }

    /// Create a new `HttpClientConfig` from the given environment variables
    ///
    /// # Variables
    ///
    /// - LONGBRIDGE_APP_KEY
    /// - LONGBRIDGE_APP_SECRET
    /// - LONGBRIDGE_ACCESS_TOKEN
    /// - LONGBRIDGE_HTTP_URL
    pub fn from_env() -> Result<Self, HttpClientError> {
        let _ = dotenv::dotenv();

        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",
            }
        })?;

        let mut config = Self::new(app_key, app_secret, access_token);
        if let Ok(http_url) = std::env::var("LONGBRIDGE_HTTP_URL") {
            config.http_url = http_url;
        }
        Ok(config)
    }

    /// Specifies the url of the OpenAPI server.
    ///
    /// Default: <https://openapi.longbridgeapp.com>
    /// NOTE: Usually you don't need to change it.
    #[must_use]
    pub fn http_url(self, url: impl Into<String>) -> Self {
        Self {
            http_url: url.into(),
            ..self
        }
    }
}