use super::{post_send, Part};
use crate::{error::CommonResponse, wechat::WxApiRequestBuilder, SdkResult};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct ImgSearchRes {
items: Vec<ImgSearchResItem>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ImgSearchResItem {
pub title: String,
pub img_url: String,
pub price: String,
pub path: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SiteSearch {
pub keyword: String,
pub next_page_info: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SiteSearchRes {
pub items: Vec<SiteSearchResItem>,
pub has_next_page: bool,
pub next_page_info: String,
pub hit_count: i64,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SiteSearchResItem {
pub title: String,
pub description: String,
pub image: String,
pub path: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SubmitPages {
pub pages: Vec<SubmitPagesItem>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SubmitPagesItem {
pub path: String,
pub query: String,
}
pub struct SearchModule<'a, T: WxApiRequestBuilder>(pub(crate) &'a T);
impl<'a, T: WxApiRequestBuilder> SearchModule<'a, T> {
pub async fn image_search(&self, data: Vec<Part>) -> SdkResult<ImgSearchRes> {
let url = "https://api.weixin.qq.com/wxa/imagesearch";
let builder = self.0.wx_post(url).await?;
let form =
data.into_iter()
.try_fold(reqwest::multipart::Form::new(), |form, data_part| {
let part = reqwest::multipart::Part::bytes(data_part.data)
.file_name(data_part.filename)
.mime_str(&data_part.content_type)?;
Ok::<_, reqwest::Error>(form.part("img", part))
})?;
let builder = builder.multipart(form);
let res: CommonResponse<ImgSearchRes> = builder.send().await?.json().await?;
res.into()
}
pub async fn site_search(&self, data: &SiteSearch) -> SdkResult<SiteSearchRes> {
let url = "https://api.weixin.qq.com/wxa/sitesearch";
post_send(self.0, url, data).await
}
pub async fn submit_pages(&self, data: &SubmitPages) -> SdkResult<()> {
let url = "https://api.weixin.qq.com/wxa/search/wxaapi_submitpages";
post_send(self.0, url, data).await
}
}