longport_httpcli/
config.rsuse crate::HttpClientError;
#[derive(Debug, Clone)]
pub struct HttpClientConfig {
pub(crate) http_url: Option<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: None,
app_key: app_key.into(),
app_secret: app_secret.into(),
access_token: access_token.into(),
}
}
pub fn from_env() -> Result<Self, HttpClientError> {
let _ = dotenv::dotenv();
let app_key =
std::env::var("LONGPORT_APP_KEY").map_err(|_| HttpClientError::MissingEnvVar {
name: "LONGPORT_APP_KEY",
})?;
let app_secret =
std::env::var("LONGPORT_APP_SECRET").map_err(|_| HttpClientError::MissingEnvVar {
name: "LONGPORT_APP_SECRET",
})?;
let access_token =
std::env::var("LONGPORT_ACCESS_TOKEN").map_err(|_| HttpClientError::MissingEnvVar {
name: "LONGPORT_ACCESS_TOKEN",
})?;
let mut config = Self::new(app_key, app_secret, access_token);
config.http_url = std::env::var("LONGPORT_HTTP_URL").ok();
Ok(config)
}
#[must_use]
pub fn http_url(self, url: impl Into<String>) -> Self {
Self {
http_url: Some(url.into()),
..self
}
}
}