use crate::client::ZtkClient;
use crate::common::http::{build_params_with_appkey, url_encode};
use crate::error::ZtkResult;
use super::request::{
JdConvertRequest, JdGoodsDetailRequest, JdHotGoodsRequest, JdOrderQueryRequest,
JingfenGoodsRequest,
};
use super::response::{
JdConvertResponse, JdGoodsDetailResponse, JdHotGoodsResponse, JdOrderResponse,
JingfenGoodsResponse,
};
pub struct JdApi<'a> {
client: &'a ZtkClient,
}
impl<'a> JdApi<'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(&self, request: JdConvertRequest) -> ZtkResult<JdConvertResponse> {
let mut encoded_request = request.clone();
encoded_request.material_id = url_encode(&request.material_id);
if let Some(ref coupon_url) = request.coupon_url {
encoded_request.coupon_url = Some(url_encode(coupon_url));
}
let params = build_params_with_appkey(self.client.appkey(), &encoded_request)?;
self.client
.http_client()
.get(
"/api/open_jing_union_open_promotion_byunionid_get.ashx",
¶ms,
)
.await
}
pub async fn jingfen_goods(
&self,
request: JingfenGoodsRequest,
) -> ZtkResult<JingfenGoodsResponse> {
let params = build_params_with_appkey(self.client.appkey(), &request)?;
self.client
.http_client()
.get(
"/api/open_jing_union_open_goods_jingfen_query.ashx",
¶ms,
)
.await
}
pub async fn query_orders(&self, request: JdOrderQueryRequest) -> ZtkResult<JdOrderResponse> {
let params = build_params_with_appkey(self.client.appkey(), &request)?;
self.client
.http_client()
.get("/api/open_jing_union_open_order_query.ashx", ¶ms)
.await
}
pub async fn goods_detail(
&self,
request: JdGoodsDetailRequest,
) -> ZtkResult<JdGoodsDetailResponse> {
let params = build_params_with_appkey(self.client.appkey(), &request)?;
self.client
.http_client()
.get("/api/open_jing_union_open_goods_query.ashx", ¶ms)
.await
}
pub async fn hot_goods(&self, request: JdHotGoodsRequest) -> ZtkResult<JdHotGoodsResponse> {
let params = build_params_with_appkey(self.client.appkey(), &request)?;
self.client
.http_client()
.get("/api/open_jing_goods_list.ashx", ¶ms)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_jd_api_creation() {
let client = ZtkClient::new("test_appkey").build().unwrap();
let jd = client.jd();
assert_eq!(jd.client().appkey(), "test_appkey");
}
}