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::FundTarget;
8
9impl Client {
10    /// Fetch fund holder structure.
11    pub async fn fund_holders_detail(
12        &self,
13        fund_type: FundType,
14        thscode: &Thscode,
15        merge_scope: HolderMergeScope,
16    ) -> Result<Response<FundHoldersData>, Error> {
17        let mut query = FundTarget::new(fund_type, thscode)?.query();
18        query.push(("merge_scope", merge_scope.to_string()));
19        self.get(endpoints::FUND_HOLDERS_DETAIL, &query).await
20    }
21
22    /// Fetch up to ten largest fund holders.
23    pub async fn fund_holders_top(
24        &self,
25        fund_type: FundType,
26        thscode: &Thscode,
27        limit: Option<u8>,
28    ) -> Result<Response<FundTopHoldersData>, Error> {
29        let target = FundTarget::new(fund_type, thscode)?;
30        if limit.is_some_and(|limit| limit == 0 || limit > 10) {
31            return Err(ValidationError::new("limit", "must be in the range 1..=10").into());
32        }
33        let mut query = target.query();
34        if let Some(limit) = limit {
35            query.push(("limit", limit.to_string()));
36        }
37        self.get(endpoints::FUND_HOLDERS_TOP, &query).await
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use crate::ApiKey;
45
46    #[tokio::test]
47    async fn top_holders_limit_is_one_through_ten() {
48        let client = Client::builder(ApiKey::new("test-api-key").unwrap())
49            .base_url("http://127.0.0.1:9")
50            .build()
51            .unwrap();
52        let fund = Thscode::new("025480.OF").unwrap();
53
54        for limit in [0, 11] {
55            let error = client
56                .fund_holders_top(FundType::Otc, &fund, Some(limit))
57                .await
58                .unwrap_err();
59            assert!(matches!(error, Error::InvalidInput(_)));
60        }
61    }
62}