Skip to main content

financial_api/
market_dump.rs

1use std::fmt;
2
3use serde::{Deserialize, Deserializer};
4use time::OffsetDateTime;
5
6use crate::endpoints;
7use crate::{Client, Error, Response, ValidationError};
8
9/// Short-lived presigned URL whose debug representation is always redacted.
10#[derive(Clone, PartialEq, Eq)]
11pub struct SecretUrl(String);
12
13impl SecretUrl {
14    /// Validate an absolute HTTP(S) download URL.
15    pub fn new(value: impl Into<String>) -> Result<Self, ValidationError> {
16        let value = value.into();
17        let url = reqwest::Url::parse(&value)
18            .map_err(|_| ValidationError::new("presigned_url", "must be a valid absolute URL"))?;
19        if !matches!(url.scheme(), "http" | "https")
20            || url.host().is_none()
21            || !url.username().is_empty()
22            || url.password().is_some()
23        {
24            return Err(ValidationError::new(
25                "presigned_url",
26                "must be an absolute HTTP(S) URL without userinfo",
27            ));
28        }
29        Ok(Self(value))
30    }
31
32    /// Borrow the URL for immediate download.
33    #[must_use]
34    pub fn expose(&self) -> &str {
35        &self.0
36    }
37}
38
39impl<'de> Deserialize<'de> for SecretUrl {
40    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
41        let value = String::deserialize(deserializer)?;
42        Self::new(value).map_err(serde::de::Error::custom)
43    }
44}
45
46impl fmt::Debug for SecretUrl {
47    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
48        formatter.write_str("SecretUrl([REDACTED])")
49    }
50}
51
52/// Short-lived market dump download authorization.
53#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
54pub struct MarketDumpUrl {
55    pub presigned_url: SecretUrl,
56    #[serde(with = "time::serde::rfc3339")]
57    pub presigned_url_expires_at: OffsetDateTime,
58}
59
60impl Client {
61    /// Sign a URL for the full ten-year daily K-line dump.
62    pub async fn market_dump_daily_k(&self) -> Result<Response<MarketDumpUrl>, Error> {
63        self.market_dump_url(endpoints::DUMP_DAILY_K).await
64    }
65
66    /// Sign a URL for the latest ten trading days of daily K-lines.
67    pub async fn market_dump_daily_k_10d(&self) -> Result<Response<MarketDumpUrl>, Error> {
68        self.market_dump_url(endpoints::DUMP_DAILY_K_10D).await
69    }
70
71    /// Sign a URL for the full corporate-action event dump.
72    pub async fn market_dump_adjustment_factors(&self) -> Result<Response<MarketDumpUrl>, Error> {
73        self.market_dump_url(endpoints::DUMP_ADJUSTMENT_FACTORS)
74            .await
75    }
76
77    async fn market_dump_url(&self, path: &str) -> Result<Response<MarketDumpUrl>, Error> {
78        self.get(path, &[] as &[(&str, &str)]).await
79    }
80}