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    /// 获取利润表。
13    ///
14    /// # 示例
15    #[doc = concat!(
16        "```no_run\n",
17        include_str!("../../examples/financials_income_statements.rs"),
18        "\n```"
19    )]
20    pub async fn financials_income_statements(
21        &self,
22        thscode: &AShareCode,
23        period: FinancialPeriod,
24        range: FinancialRange,
25    ) -> Result<Response<IncomeStatementsData>, Error> {
26        self.financial_statements(endpoints::INCOME_STATEMENTS, thscode, period, range)
27            .await
28    }
29
30    /// 获取资产负债表。
31    ///
32    /// # 示例
33    #[doc = concat!(
34        "```no_run\n",
35        include_str!("../../examples/financials_balance_sheets.rs"),
36        "\n```"
37    )]
38    pub async fn financials_balance_sheets(
39        &self,
40        thscode: &AShareCode,
41        period: FinancialPeriod,
42        range: FinancialRange,
43    ) -> Result<Response<BalanceSheetsData>, Error> {
44        self.financial_statements(endpoints::BALANCE_SHEETS, thscode, period, range)
45            .await
46    }
47
48    /// 获取现金流量表。
49    ///
50    /// # 示例
51    #[doc = concat!(
52        "```no_run\n",
53        include_str!("../../examples/financials_cash_flow_statements.rs"),
54        "\n```"
55    )]
56    pub async fn financials_cash_flow_statements(
57        &self,
58        thscode: &AShareCode,
59        period: FinancialPeriod,
60        range: FinancialRange,
61    ) -> Result<Response<CashFlowStatementsData>, Error> {
62        self.financial_statements(endpoints::CASH_FLOW_STATEMENTS, thscode, period, range)
63            .await
64    }
65
66    async fn financial_statements<T: DeserializeOwned>(
67        &self,
68        path: &str,
69        thscode: &AShareCode,
70        period: FinancialPeriod,
71        range: FinancialRange,
72    ) -> Result<Response<T>, Error> {
73        let mut query = vec![
74            ("thscode", thscode.to_string()),
75            ("period", period.to_string()),
76        ];
77        match range {
78            FinancialRange::Recent { limit } => query.push(("limit", limit.to_string())),
79            FinancialRange::Between { start, end } => {
80                validate_millis_window(start, end, Some(TEN_YEARS_MS))?;
81                query.push(("start", start.to_string()));
82                query.push(("end", end.to_string()));
83            }
84        }
85        self.get(path, &query).await
86    }
87
88    /// 获取指定报告期的五类财务指标。
89    ///
90    /// # 示例
91    #[doc = concat!(
92        "```no_run\n",
93        include_str!("../../examples/financials_indicators.rs"),
94        "\n```"
95    )]
96    pub async fn financials_indicators(
97        &self,
98        thscode: &AShareCode,
99        report: &FinancialReport,
100    ) -> Result<Response<FinancialIndicatorsData>, Error> {
101        let query = [("thscode", thscode.as_str()), ("report", report.as_str())];
102        self.get(endpoints::FINANCIAL_INDICATORS, &query).await
103    }
104}