use serde::{Deserialize, Serialize};
use crate::{
access_token::AccessTokenProvider,
error::{CommonError, CommonResponse},
wechat::WxApiRequestBuilder,
SdkResult, WxSdk,
};
use super::draft::NewsItemList;
pub struct FreePublishModule<'a, T: AccessTokenProvider>(pub(crate) &'a WxSdk<T>);
#[derive(Serialize, Deserialize)]
pub struct PublishId {
publish_id: String,
}
#[derive(Serialize, Deserialize)]
pub struct PublishResult {
pub publish_id: String,
pub publish_status: i8,
pub article_id: String,
pub article_detail: Option<ArticleDetail>,
pub fail_idx: Vec<i8>,
}
#[derive(Serialize, Deserialize)]
pub struct ArticleDetail {
pub count: i8,
pub item: Vec<ArticleDetailItem>,
}
#[derive(Serialize, Deserialize)]
pub struct ArticleDetailItem {
pub idx: i8,
pub article_url: String,
}
#[derive(Serialize, Deserialize)]
pub struct BatchList {
pub total_count: i32,
pub item_count: i32,
pub item: Vec<PublishListItem>,
}
#[derive(Serialize, Deserialize)]
pub struct PublishListItem {
pub article_id: String,
pub content: NewsItemList,
}
impl<'a, T: AccessTokenProvider> FreePublishModule<'a, T> {
pub async fn submit(&self, media_id: &str) -> SdkResult<PublishId> {
let base_url = "https://api.weixin.qq.com/cgi-bin/freepublish/submit";
let sdk = self.0;
let builder = sdk.wx_post(base_url).await?;
let res: CommonResponse<PublishId> = builder
.json(&serde_json::json!({ "media_id": media_id }))
.send()
.await?
.json()
.await?;
res.into()
}
pub async fn get(&self, publish_id: &str) -> SdkResult<PublishResult> {
let base_url = "https://api.weixin.qq.com/cgi-bin/freepublish/get";
let sdk = self.0;
let builder = sdk.wx_post(base_url).await?;
let res: CommonResponse<PublishResult> = builder
.json(&serde_json::json!({ "publish_id": publish_id }))
.send()
.await?
.json()
.await?;
res.into()
}
pub async fn delete(&self, article_id: &str, index: i8) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/freepublish/delete";
let sdk = self.0;
let builder = sdk.wx_post(base_url).await?;
let res: CommonError = builder
.json(&serde_json::json!({ "article_id": article_id, "index": index }))
.send()
.await?
.json()
.await?;
res.into()
}
pub async fn get_article(&self, article_id: &str) -> SdkResult<NewsItemList> {
let base_url = "https://api.weixin.qq.com/cgi-bin/freepublish/getarticle";
let sdk = self.0;
let builder = sdk.wx_post(base_url).await?;
let res: CommonResponse<NewsItemList> = builder
.json(&serde_json::json!({ "article_id": article_id }))
.send()
.await?
.json()
.await?;
res.into()
}
pub async fn batchget(&self, offset: i64, count: i64, no_content: i8) -> SdkResult<BatchList> {
let base_url = "https://api.weixin.qq.com/cgi-bin/freepublish/batchget";
let sdk = self.0;
let builder = sdk.wx_post(base_url).await?;
let res: CommonResponse<BatchList> = builder
.json(
&serde_json::json!({ "offset": offset, "count": count, "no_content": no_content }),
)
.send()
.await?
.json()
.await?;
res.into()
}
}