Skip to main content

financial_api/fund/
portfolio.rs

1use crate::endpoints;
2use crate::{
3    Client, Error, FundAssetAllocationData, FundHoldingsData, FundIndustryAllocationData,
4    FundPortfolioHistoryData, FundReportDatesData, FundType, NaturalDate, ReportType, Response,
5    Thscode,
6};
7
8use super::FundTarget;
9
10impl Client {
11    /// Fetch periodically disclosed fund holdings and summary ratios.
12    pub async fn fund_portfolio_holdings(
13        &self,
14        fund_type: FundType,
15        thscode: &Thscode,
16    ) -> Result<Response<FundHoldingsData>, Error> {
17        self.fund_detail(endpoints::FUND_HOLDINGS, fund_type, thscode)
18            .await
19    }
20
21    /// Fetch historical disclosed stock holdings.
22    pub async fn fund_portfolio_stock_history(
23        &self,
24        fund_type: FundType,
25        thscode: &Thscode,
26        report_type: &ReportType,
27        end_date: NaturalDate,
28    ) -> Result<Response<FundPortfolioHistoryData>, Error> {
29        self.fund_portfolio_history(
30            endpoints::FUND_STOCK_HISTORY,
31            fund_type,
32            thscode,
33            report_type,
34            end_date,
35        )
36        .await
37    }
38
39    /// Fetch historical disclosed bond holdings.
40    pub async fn fund_portfolio_bond_history(
41        &self,
42        fund_type: FundType,
43        thscode: &Thscode,
44        report_type: &ReportType,
45        end_date: NaturalDate,
46    ) -> Result<Response<FundPortfolioHistoryData>, Error> {
47        self.fund_portfolio_history(
48            endpoints::FUND_BOND_HISTORY,
49            fund_type,
50            thscode,
51            report_type,
52            end_date,
53        )
54        .await
55    }
56
57    async fn fund_portfolio_history(
58        &self,
59        path: &str,
60        fund_type: FundType,
61        thscode: &Thscode,
62        report_type: &ReportType,
63        end_date: NaturalDate,
64    ) -> Result<Response<FundPortfolioHistoryData>, Error> {
65        let mut query = FundTarget::new(fund_type, thscode)?.query();
66        query.push(("report_type", report_type.to_string()));
67        query.push(("end_date", end_date.to_string()));
68        self.get(path, &query).await
69    }
70
71    /// Fetch available stock-holding report dates.
72    pub async fn fund_portfolio_stock_report_dates(
73        &self,
74        fund_type: FundType,
75        thscode: &Thscode,
76        report_type: Option<&ReportType>,
77    ) -> Result<Response<FundReportDatesData>, Error> {
78        self.fund_report_dates(
79            endpoints::FUND_STOCK_REPORT_DATES,
80            fund_type,
81            thscode,
82            report_type,
83        )
84        .await
85    }
86
87    /// Fetch available bond-holding report dates.
88    pub async fn fund_portfolio_bond_report_dates(
89        &self,
90        fund_type: FundType,
91        thscode: &Thscode,
92        report_type: Option<&ReportType>,
93    ) -> Result<Response<FundReportDatesData>, Error> {
94        self.fund_report_dates(
95            endpoints::FUND_BOND_REPORT_DATES,
96            fund_type,
97            thscode,
98            report_type,
99        )
100        .await
101    }
102
103    async fn fund_report_dates(
104        &self,
105        path: &str,
106        fund_type: FundType,
107        thscode: &Thscode,
108        report_type: Option<&ReportType>,
109    ) -> Result<Response<FundReportDatesData>, Error> {
110        let mut query = FundTarget::new(fund_type, thscode)?.query();
111        if let Some(report_type) = report_type {
112            query.push(("report_type", report_type.to_string()));
113        }
114        self.get(path, &query).await
115    }
116
117    /// Fetch fund asset allocation.
118    pub async fn fund_portfolio_asset_allocation(
119        &self,
120        fund_type: FundType,
121        thscode: &Thscode,
122    ) -> Result<Response<FundAssetAllocationData>, Error> {
123        self.fund_detail(endpoints::FUND_ASSET_ALLOCATION, fund_type, thscode)
124            .await
125    }
126
127    /// Fetch fund industry allocation.
128    pub async fn fund_portfolio_industry_allocation(
129        &self,
130        fund_type: FundType,
131        thscode: &Thscode,
132    ) -> Result<Response<FundIndustryAllocationData>, Error> {
133        self.fund_detail(endpoints::FUND_INDUSTRY_ALLOCATION, fund_type, thscode)
134            .await
135    }
136}