Skip to main content

financial_api/a_share/
auction.rs

1use crate::endpoints;
2use crate::endpoints::join_values;
3use crate::{
4    AShareCode, AuctionBenchmarkData, AuctionSnapshotData, AuctionStage, Client, Error,
5    NaturalDate, Response,
6};
7
8impl Client {
9    /// 获取一个或多个 A 股标的的集合竞价快照。
10    ///
11    /// # 示例
12    #[doc = concat!(
13        "```no_run\n",
14        include_str!("../../examples/a_share_auction_snapshot.rs"),
15        "\n```"
16    )]
17    pub async fn a_share_auction_snapshot(
18        &self,
19        thscodes: &[AShareCode],
20        stage: AuctionStage,
21    ) -> Result<Response<AuctionSnapshotData>, Error> {
22        let query = [
23            ("thscodes", join_values("thscodes", thscodes, None)?),
24            ("stage", stage.to_string()),
25        ];
26        self.get(endpoints::AUCTION_SNAPSHOT, &query).await
27    }
28
29    /// 获取集合竞价短线基准,可指定自然日。
30    ///
31    /// # 示例
32    #[doc = concat!(
33        "```no_run\n",
34        include_str!("../../examples/a_share_auction_short_term_benchmark.rs"),
35        "\n```"
36    )]
37    pub async fn a_share_auction_short_term_benchmark(
38        &self,
39        date: Option<NaturalDate>,
40    ) -> Result<Response<AuctionBenchmarkData>, Error> {
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(&[], AuctionStage::Final)
62            .await
63            .unwrap_err();
64
65        assert!(matches!(error, Error::InvalidInput(_)));
66    }
67}