use crate::client::ZtkClient;
use crate::common::http::{build_params_with_appkey, url_encode};
use crate::error::ZtkResult;
use super::request::{
BatchConvertRequest, ConvertByItemIdRequest, ConvertByTklRequest, CreateTklRequest,
ParseItemIdRequest, QueryOrdersRequest, SearchGoodsRequest,
};
use super::response::{
BatchConvertResponse, ConvertByTklResponse, ConvertResponse, CreateTklResponse,
ParseItemIdResponse, QueryOrdersResponse, SearchGoodsResponse,
};
pub struct TaobaoApi<'a> {
client: &'a ZtkClient,
}
impl<'a> TaobaoApi<'a> {
pub fn new(client: &'a ZtkClient) -> Self {
Self { client }
}
#[allow(dead_code)]
pub(crate) fn client(&self) -> &ZtkClient {
self.client
}
pub async fn convert_by_item_id(
&self,
request: ConvertByItemIdRequest,
) -> ZtkResult<ConvertResponse> {
let params = build_params_with_appkey(self.client.appkey(), &request)?;
self.client
.http_client()
.get("/api/open_gaoyongzhuanlian.ashx", ¶ms)
.await
}
pub async fn convert_by_tkl(
&self,
request: ConvertByTklRequest,
) -> ZtkResult<ConvertByTklResponse> {
let mut encoded_request = request.clone();
encoded_request.tkl = url_encode(&request.tkl);
let params = build_params_with_appkey(self.client.appkey(), &encoded_request)?;
self.client
.http_client()
.get("/api/open_tkljm.ashx", ¶ms)
.await
}
pub async fn batch_convert(
&self,
request: BatchConvertRequest,
) -> ZtkResult<BatchConvertResponse> {
let params = build_params_with_appkey(self.client.appkey(), &request)?;
self.client
.http_client()
.get("/api/open_gaoyongzhuanlian_batch.ashx", ¶ms)
.await
}
pub async fn query_orders(
&self,
request: QueryOrdersRequest,
) -> ZtkResult<QueryOrdersResponse> {
let params = build_params_with_appkey(self.client.appkey(), &request)?;
self.client
.http_client()
.get("/api/open_dingdanchaxun.ashx", ¶ms)
.await
}
pub async fn create_tkl(&self, request: CreateTklRequest) -> ZtkResult<CreateTklResponse> {
let params = build_params_with_appkey(self.client.appkey(), &request)?;
self.client
.http_client()
.get("/api/open_taokoujian.ashx", ¶ms)
.await
}
pub async fn parse_item_id(
&self,
request: ParseItemIdRequest,
) -> ZtkResult<ParseItemIdResponse> {
let encoded_content = url_encode(&request.content);
let params = build_params_with_appkey(
self.client.appkey(),
&ParseItemIdRequestEncoded {
content: encoded_content,
},
)?;
self.client
.http_client()
.get("/api/open_shangpinbianhao.ashx", ¶ms)
.await
}
pub async fn search_goods(
&self,
request: SearchGoodsRequest,
) -> ZtkResult<SearchGoodsResponse> {
let mut encoded_request = request.clone();
if let Some(ref q) = request.q {
encoded_request.q = Some(url_encode(q));
}
let params = build_params_with_appkey(self.client.appkey(), &encoded_request)?;
self.client
.http_client()
.get_with_base_url(
"https://api.zhetaoke.com:10003",
"/api/api_quanwang.ashx",
¶ms,
)
.await
}
}
#[derive(serde::Serialize)]
struct ParseItemIdRequestEncoded {
content: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_taobao_api_creation() {
let client = ZtkClient::new("test_appkey").build().unwrap();
let taobao = client.taobao();
assert_eq!(taobao.client().appkey(), "test_appkey");
}
}