Skip to main content

financial_api/a_share/
financials.rs

1use serde::de::DeserializeOwned;
2
3use crate::endpoints;
4use crate::{
5    AShareCode, BalanceSheetsData, CashFlowStatementsData, Client, Error, FinancialIndicatorsData,
6    FinancialPeriod, FinancialRange, FinancialReport, IncomeStatementsData, Response,
7};
8
9use super::{TEN_YEARS_MS, validate_millis_window};
10
11impl Client {
12    /// Fetch income statements.
13    pub async fn financials_income_statements(
14        &self,
15        thscode: &AShareCode,
16        period: FinancialPeriod,
17        range: FinancialRange,
18    ) -> Result<Response<IncomeStatementsData>, Error> {
19        self.financial_statements(endpoints::INCOME_STATEMENTS, thscode, period, range)
20            .await
21    }
22
23    /// Fetch balance sheets.
24    pub async fn financials_balance_sheets(
25        &self,
26        thscode: &AShareCode,
27        period: FinancialPeriod,
28        range: FinancialRange,
29    ) -> Result<Response<BalanceSheetsData>, Error> {
30        self.financial_statements(endpoints::BALANCE_SHEETS, thscode, period, range)
31            .await
32    }
33
34    /// Fetch cash-flow statements.
35    pub async fn financials_cash_flow_statements(
36        &self,
37        thscode: &AShareCode,
38        period: FinancialPeriod,
39        range: FinancialRange,
40    ) -> Result<Response<CashFlowStatementsData>, Error> {
41        self.financial_statements(endpoints::CASH_FLOW_STATEMENTS, thscode, period, range)
42            .await
43    }
44
45    async fn financial_statements<T: DeserializeOwned>(
46        &self,
47        path: &str,
48        thscode: &AShareCode,
49        period: FinancialPeriod,
50        range: FinancialRange,
51    ) -> Result<Response<T>, Error> {
52        let mut query = vec![
53            ("thscode", thscode.to_string()),
54            ("period", period.to_string()),
55        ];
56        match range {
57            FinancialRange::Recent { limit } => query.push(("limit", limit.to_string())),
58            FinancialRange::Between { start, end } => {
59                validate_millis_window(start, end, Some(TEN_YEARS_MS))?;
60                query.push(("start", start.to_string()));
61                query.push(("end", end.to_string()));
62            }
63        }
64        self.get(path, &query).await
65    }
66
67    /// Fetch the five groups of financial indicators for one report.
68    pub async fn financials_indicators(
69        &self,
70        thscode: &AShareCode,
71        report: &FinancialReport,
72    ) -> Result<Response<FinancialIndicatorsData>, Error> {
73        let query = [("thscode", thscode.as_str()), ("report", report.as_str())];
74        self.get(endpoints::FINANCIAL_INDICATORS, &query).await
75    }
76}