Skip to main content

financial_api/fund/
holders.rs

1use crate::endpoints;
2use crate::{
3    Client, Error, FundHoldersData, FundTopHoldersData, FundType, HolderMergeScope, Response,
4    Thscode, ValidationError,
5};
6
7use super::fund_target_query;
8
9impl Client {
10    /// 获取基金持有人结构。
11    ///
12    /// # 示例
13    #[doc = concat!(
14        "```no_run\n",
15        include_str!("../../examples/fund_holders_detail.rs"),
16        "\n```"
17    )]
18    pub async fn fund_holders_detail(
19        &self,
20        fund_type: FundType,
21        thscode: impl TryInto<Thscode, Error: Into<ValidationError>>,
22        merge_scope: HolderMergeScope,
23    ) -> Result<Response<FundHoldersData>, Error> {
24        let mut query = fund_target_query(fund_type, thscode)?;
25        query.push(("merge_scope", merge_scope.to_string()));
26        self.get(endpoints::FUND_HOLDERS_DETAIL, &query).await
27    }
28
29    /// 获取最多十名主要基金持有人。
30    ///
31    /// # 示例
32    #[doc = concat!(
33        "```no_run\n",
34        include_str!("../../examples/fund_holders_top.rs"),
35        "\n```"
36    )]
37    pub async fn fund_holders_top(
38        &self,
39        fund_type: FundType,
40        thscode: impl TryInto<Thscode, Error: Into<ValidationError>>,
41        limit: Option<u8>,
42    ) -> Result<Response<FundTopHoldersData>, Error> {
43        let mut query = fund_target_query(fund_type, thscode)?;
44        if limit.is_some_and(|limit| limit == 0 || limit > 10) {
45            return Err(ValidationError::new("limit", "must be in the range 1..=10").into());
46        }
47        if let Some(limit) = limit {
48            query.push(("limit", limit.to_string()));
49        }
50        self.get(endpoints::FUND_HOLDERS_TOP, &query).await
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    use crate::ApiKey;
58
59    #[tokio::test]
60    async fn top_holders_limit_is_one_through_ten() {
61        let client = Client::builder(ApiKey::new("test-api-key").unwrap())
62            .base_url("http://127.0.0.1:9")
63            .build()
64            .unwrap();
65        for limit in [0, 11] {
66            let error = client
67                .fund_holders_top(FundType::Otc, "025480.OF", Some(limit))
68                .await
69                .unwrap_err();
70            assert!(matches!(error, Error::InvalidInput(_)));
71        }
72    }
73}