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::fund_target_query;
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: impl TryInto<Thscode, Error: Into<ValidationError>>,
19        limit: Option<u32>,
20        offset: Option<&Cursor>,
21    ) -> Result<Response<FundNewsData>, Error> {
22        let mut query = fund_target_query(fund_type, thscode)?;
23        if limit == Some(0) {
24            return Err(ValidationError::new("limit", "must be at least 1").into());
25        }
26        if let Some(limit) = limit {
27            query.push(("limit", limit.to_string()));
28        }
29        if let Some(offset) = offset {
30            query.push(("offset", offset.to_string()));
31        }
32        self.get(endpoints::FUND_NEWS, &query).await
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use crate::ApiKey;
40
41    #[tokio::test]
42    async fn article_limit_must_be_positive() {
43        let client = Client::builder(ApiKey::new("test-api-key").unwrap())
44            .base_url("http://127.0.0.1:9")
45            .build()
46            .unwrap();
47        let error = client
48            .fund_news_article_list(FundType::Otc, "025480.OF", Some(0), None)
49            .await
50            .unwrap_err();
51
52        assert!(matches!(error, Error::InvalidInput(_)));
53    }
54}