Skip to main content

financial_api/fund/
news.rs

1use crate::endpoints;
2use crate::{Client, Cursor, Error, FundNewsData, FundType, Response, Thscode, ValidationError};
3
4use super::FundTarget;
5
6impl Client {
7    /// 按不透明游标分页获取基金新闻。
8    ///
9    /// # 示例
10    #[doc = concat!(
11        "```no_run\n",
12        include_str!("../../examples/fund_news_article_list.rs"),
13        "\n```"
14    )]
15    pub async fn fund_news_article_list(
16        &self,
17        fund_type: FundType,
18        thscode: &Thscode,
19        limit: Option<u32>,
20        offset: Option<&Cursor>,
21    ) -> Result<Response<FundNewsData>, Error> {
22        let target = FundTarget::new(fund_type, thscode)?;
23        if limit == Some(0) {
24            return Err(ValidationError::new("limit", "must be at least 1").into());
25        }
26        let mut query = target.query();
27        if let Some(limit) = limit {
28            query.push(("limit", limit.to_string()));
29        }
30        if let Some(offset) = offset {
31            query.push(("offset", offset.to_string()));
32        }
33        self.get(endpoints::FUND_NEWS, &query).await
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use crate::ApiKey;
41
42    #[tokio::test]
43    async fn article_limit_must_be_positive() {
44        let client = Client::builder(ApiKey::new("test-api-key").unwrap())
45            .base_url("http://127.0.0.1:9")
46            .build()
47            .unwrap();
48        let fund = Thscode::new("025480.OF").unwrap();
49
50        let error = client
51            .fund_news_article_list(FundType::Otc, &fund, Some(0), None)
52            .await
53            .unwrap_err();
54
55        assert!(matches!(error, Error::InvalidInput(_)));
56    }
57}