Skip to main content

financial_api/a_share/
auction.rs

1use crate::endpoints;
2use crate::endpoints::join_a_share_codes;
3use crate::{
4    AuctionBenchmarkData, AuctionSnapshotData, AuctionStage, Client, Error, NaturalDate, Response,
5};
6
7impl Client {
8    /// 获取一个或多个 A 股标的的集合竞价快照。
9    ///
10    /// # 示例
11    #[doc = concat!(
12        "```no_run\n",
13        include_str!("../../examples/a_share_auction_snapshot.rs"),
14        "\n```"
15    )]
16    pub async fn a_share_auction_snapshot(
17        &self,
18        thscodes: &[impl AsRef<str> + Sync],
19        stage: AuctionStage,
20    ) -> Result<Response<AuctionSnapshotData>, Error> {
21        let query = [
22            ("thscodes", join_a_share_codes(thscodes, None)?),
23            ("stage", stage.to_string()),
24        ];
25        self.get(endpoints::AUCTION_SNAPSHOT, &query).await
26    }
27
28    /// 获取集合竞价短线基准,可指定自然日。
29    ///
30    /// # 示例
31    #[doc = concat!(
32        "```no_run\n",
33        include_str!("../../examples/a_share_auction_short_term_benchmark.rs"),
34        "\n```"
35    )]
36    pub async fn a_share_auction_short_term_benchmark(
37        &self,
38        date: Option<&str>,
39    ) -> Result<Response<AuctionBenchmarkData>, Error> {
40        let date = date.map(NaturalDate::parse).transpose()?;
41        let query = date
42            .map(|date| vec![("date", date.to_string())])
43            .unwrap_or_default();
44        self.get(endpoints::AUCTION_BENCHMARK, &query).await
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use crate::ApiKey;
52
53    #[tokio::test]
54    async fn snapshot_requires_at_least_one_target_before_transport() {
55        let client = Client::builder(ApiKey::new("test-api-key").unwrap())
56            .base_url("http://127.0.0.1:9")
57            .build()
58            .unwrap();
59
60        let error = client
61            .a_share_auction_snapshot(&[] as &[&str], AuctionStage::Final)
62            .await
63            .unwrap_err();
64
65        assert!(matches!(error, Error::InvalidInput(_)));
66    }
67}