use std::collections::HashSet;
use std::sync::LazyLock;
use crate::error::{Error, Result};
use crate::http::{BaseClient, RequestOptions};
use crate::models::{ArticleDetail, GetArticleByPageRequest, GetArticleByPageResponse};
const PATH_GET_ARTICLE_BY_PAGE: &str = "/dceapi/cms/info/articleByPage";
const PATH_GET_ARTICLE_DETAIL: &str = "/dceapi/cms/info/articleDetail";
static VALID_COLUMN_IDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
let mut set = HashSet::new();
set.insert("244"); set.insert("245"); set.insert("246"); set.insert("248"); set.insert("1076"); set.insert("242"); set
});
pub fn is_valid_column_id(column_id: &str) -> bool {
VALID_COLUMN_IDS.contains(column_id)
}
#[derive(Debug, Clone)]
pub struct NewsService {
client: BaseClient,
}
impl NewsService {
pub fn new(client: BaseClient) -> Self {
NewsService { client }
}
pub async fn get_article_by_page(
&self,
mut req: GetArticleByPageRequest,
opts: Option<RequestOptions>,
) -> Result<GetArticleByPageResponse> {
if !is_valid_column_id(&req.column_id) {
return Err(Error::validation(
"column_id",
"invalid column_id, must be one of: 244, 245, 246, 248, 1076, 242",
));
}
if req.site_id == 0 {
req.site_id = 5;
}
self.client
.do_post(PATH_GET_ARTICLE_BY_PAGE, &req, opts)
.await
}
pub async fn get_article_detail(
&self,
article_id: &str,
opts: Option<RequestOptions>,
) -> Result<ArticleDetail> {
if article_id.is_empty() {
return Err(Error::validation("article_id", "article_id is required"));
}
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct Request<'a> {
article_id: &'a str,
}
let req = Request { article_id };
self.client.do_post(PATH_GET_ARTICLE_DETAIL, &req, opts).await
}
}