dceapi_rs/services/
news.rs

1//! News service for article and announcement APIs.
2
3use std::collections::HashSet;
4use std::sync::LazyLock;
5
6use crate::error::{Error, Result};
7use crate::http::{BaseClient, RequestOptions};
8use crate::models::{GetArticleByPageRequest, GetArticleByPageResponse};
9
10/// API endpoint for paginated article list.
11const PATH_GET_ARTICLE_BY_PAGE: &str = "/dceapi/cms/info/articleByPage";
12
13/// Valid column IDs for articles.
14/// - 244: 业务公告与通知
15/// - 245: 活动公告与通知
16/// - 246: 交易所新闻-文媒
17/// - 248: 媒体看大商所-文媒
18/// - 1076: 今日提示
19/// - 242: 新闻发布
20static VALID_COLUMN_IDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
21    let mut set = HashSet::new();
22    set.insert("244"); // 业务公告与通知
23    set.insert("245"); // 活动公告与通知
24    set.insert("246"); // 交易所新闻-文媒
25    set.insert("248"); // 媒体看大商所-文媒
26    set.insert("1076"); // 今日提示
27    set.insert("242"); // 新闻发布
28    set
29});
30
31/// Check if a column ID is valid.
32pub fn is_valid_column_id(column_id: &str) -> bool {
33    VALID_COLUMN_IDS.contains(column_id)
34}
35
36/// News service for accessing articles and announcements.
37#[derive(Debug, Clone)]
38pub struct NewsService {
39    client: BaseClient,
40}
41
42impl NewsService {
43    /// Create a new news service.
44    pub fn new(client: BaseClient) -> Self {
45        NewsService { client }
46    }
47
48    /// Get paginated article list.
49    ///
50    /// # Arguments
51    /// * `req` - Request parameters including column_id, page_no, page_size
52    /// * `opts` - Optional request options
53    ///
54    /// # Valid Column IDs (官方文档定义)
55    /// * `244` - 业务公告与通知
56    /// * `245` - 活动公告与通知
57    /// * `246` - 交易所新闻-文媒
58    /// * `248` - 媒体看大商所-文媒
59    /// * `1076` - 今日提示
60    /// * `242` - 新闻发布
61    pub async fn get_article_by_page(
62        &self,
63        mut req: GetArticleByPageRequest,
64        opts: Option<RequestOptions>,
65    ) -> Result<GetArticleByPageResponse> {
66        // Validate column_id
67        if !is_valid_column_id(&req.column_id) {
68            return Err(Error::validation(
69                "column_id",
70                "invalid column_id, must be one of: 244, 245, 246, 248, 1076, 242",
71            ));
72        }
73
74        // Apply default site_id if not set
75        if req.site_id == 0 {
76            req.site_id = 5;
77        }
78
79        self.client
80            .do_post(PATH_GET_ARTICLE_BY_PAGE, &req, opts)
81            .await
82    }
83}