dceapi_rs/services/
news.rs1use std::collections::HashSet;
4use std::sync::LazyLock;
5
6use crate::error::{Error, Result};
7use crate::http::{BaseClient, RequestOptions};
8use crate::models::{ArticleDetail, GetArticleByPageRequest, GetArticleByPageResponse};
9
10const PATH_GET_ARTICLE_BY_PAGE: &str = "/dceapi/cms/info/articleByPage";
12
13const PATH_GET_ARTICLE_DETAIL: &str = "/dceapi/cms/info/articleDetail";
15
16static VALID_COLUMN_IDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
18 let mut set = HashSet::new();
19 set.insert("244"); set.insert("245"); set.insert("246"); set.insert("248"); set.insert("1076"); set.insert("242"); set
26});
27
28pub fn is_valid_column_id(column_id: &str) -> bool {
30 VALID_COLUMN_IDS.contains(column_id)
31}
32
33#[derive(Debug, Clone)]
35pub struct NewsService {
36 client: BaseClient,
37}
38
39impl NewsService {
40 pub fn new(client: BaseClient) -> Self {
42 NewsService { client }
43 }
44
45 pub async fn get_article_by_page(
59 &self,
60 mut req: GetArticleByPageRequest,
61 opts: Option<RequestOptions>,
62 ) -> Result<GetArticleByPageResponse> {
63 if !is_valid_column_id(&req.column_id) {
65 return Err(Error::validation(
66 "column_id",
67 "invalid column_id, must be one of: 244, 245, 246, 248, 1076, 242",
68 ));
69 }
70
71 if req.site_id == 0 {
73 req.site_id = 5;
74 }
75
76 self.client
77 .do_post(PATH_GET_ARTICLE_BY_PAGE, &req, opts)
78 .await
79 }
80
81 pub async fn get_article_detail(
87 &self,
88 article_id: &str,
89 opts: Option<RequestOptions>,
90 ) -> Result<ArticleDetail> {
91 if article_id.is_empty() {
92 return Err(Error::validation("article_id", "article_id is required"));
93 }
94
95 #[derive(serde::Serialize)]
96 #[serde(rename_all = "camelCase")]
97 struct Request<'a> {
98 article_id: &'a str,
99 }
100
101 let req = Request { article_id };
102 self.client.do_post(PATH_GET_ARTICLE_DETAIL, &req, opts).await
103 }
104}