use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct PddConvertRequest {
pub pdd_app_key: String,
pub pdd_app_secret: String,
pub pid: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_parameters: Option<String>,
}
impl PddConvertRequest {
pub fn new(
pdd_app_key: impl Into<String>,
pdd_app_secret: impl Into<String>,
pid: impl Into<String>,
content: impl Into<String>,
) -> Self {
Self {
pdd_app_key: pdd_app_key.into(),
pdd_app_secret: pdd_app_secret.into(),
pid: pid.into(),
content: content.into(),
custom_parameters: None,
}
}
pub fn custom_parameters(mut self, custom_parameters: impl Into<String>) -> Self {
self.custom_parameters = Some(custom_parameters.into());
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PddGoodsDetailSimpleRequest {
pub pdd_app_key: String,
pub pdd_app_secret: String,
pub pid: String,
pub content: String,
}
impl PddGoodsDetailSimpleRequest {
pub fn new(
pdd_app_key: impl Into<String>,
pdd_app_secret: impl Into<String>,
pid: impl Into<String>,
content: impl Into<String>,
) -> Self {
Self {
pdd_app_key: pdd_app_key.into(),
pdd_app_secret: pdd_app_secret.into(),
pid: pid.into(),
content: content.into(),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PddGoodsDetailFullRequest {
pub pdd_app_key: String,
pub pdd_app_secret: String,
pub pid: String,
pub keyword: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_parameters: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cat_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub activity_tags: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub block_cat_packages: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub block_cats: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_brand_goods: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub merchant_type: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sort_type: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub use_customized: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub with_coupon: Option<bool>,
}
impl PddGoodsDetailFullRequest {
pub fn new(
pdd_app_key: impl Into<String>,
pdd_app_secret: impl Into<String>,
pid: impl Into<String>,
keyword: impl Into<String>,
) -> Self {
Self {
pdd_app_key: pdd_app_key.into(),
pdd_app_secret: pdd_app_secret.into(),
pid: pid.into(),
keyword: keyword.into(),
custom_parameters: None,
cat_id: None,
activity_tags: None,
block_cat_packages: None,
block_cats: None,
is_brand_goods: None,
merchant_type: None,
sort_type: None,
use_customized: None,
with_coupon: None,
}
}
pub fn custom_parameters(mut self, custom_parameters: impl Into<String>) -> Self {
self.custom_parameters = Some(custom_parameters.into());
self
}
pub fn cat_id(mut self, cat_id: impl Into<String>) -> Self {
self.cat_id = Some(cat_id.into());
self
}
pub fn activity_tags(mut self, activity_tags: impl Into<String>) -> Self {
self.activity_tags = Some(activity_tags.into());
self
}
pub fn block_cat_packages(mut self, block_cat_packages: impl Into<String>) -> Self {
self.block_cat_packages = Some(block_cat_packages.into());
self
}
pub fn block_cats(mut self, block_cats: impl Into<String>) -> Self {
self.block_cats = Some(block_cats.into());
self
}
pub fn is_brand_goods(mut self, is_brand_goods: bool) -> Self {
self.is_brand_goods = Some(is_brand_goods);
self
}
pub fn merchant_type(mut self, merchant_type: u32) -> Self {
self.merchant_type = Some(merchant_type);
self
}
pub fn sort_type(mut self, sort_type: u32) -> Self {
self.sort_type = Some(sort_type);
self
}
pub fn use_customized(mut self, use_customized: bool) -> Self {
self.use_customized = Some(use_customized);
self
}
pub fn with_coupon(mut self, with_coupon: bool) -> Self {
self.with_coupon = Some(with_coupon);
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PddOrderQueryRequest {
pub pdd_app_key: String,
pub pdd_app_secret: String,
pub start_update_time: i64,
pub end_update_time: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub query_order_type: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_size: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cash_gift_order: Option<bool>,
}
impl PddOrderQueryRequest {
pub fn new(
pdd_app_key: impl Into<String>,
pdd_app_secret: impl Into<String>,
start_update_time: i64,
end_update_time: i64,
) -> Self {
Self {
pdd_app_key: pdd_app_key.into(),
pdd_app_secret: pdd_app_secret.into(),
start_update_time,
end_update_time,
query_order_type: None,
page: None,
page_size: None,
cash_gift_order: None,
}
}
pub fn query_order_type(mut self, query_order_type: u32) -> Self {
self.query_order_type = Some(query_order_type);
self
}
pub fn page(mut self, page: u32) -> Self {
self.page = Some(page);
self
}
pub fn page_size(mut self, page_size: u32) -> Self {
self.page_size = Some(page_size);
self
}
pub fn cash_gift_order(mut self, cash_gift_order: bool) -> Self {
self.cash_gift_order = Some(cash_gift_order);
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PddAuthorizeRequest {
pub pdd_app_key: String,
pub pdd_app_secret: String,
pub pid: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_parameters: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub generate_qq_app: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub generate_we_app: Option<bool>,
}
impl PddAuthorizeRequest {
pub fn new(
pdd_app_key: impl Into<String>,
pdd_app_secret: impl Into<String>,
pid: impl Into<String>,
) -> Self {
Self {
pdd_app_key: pdd_app_key.into(),
pdd_app_secret: pdd_app_secret.into(),
pid: pid.into(),
custom_parameters: None,
generate_qq_app: None,
generate_we_app: None,
}
}
pub fn custom_parameters(mut self, custom_parameters: impl Into<String>) -> Self {
self.custom_parameters = Some(custom_parameters.into());
self
}
pub fn generate_qq_app(mut self, generate_qq_app: bool) -> Self {
self.generate_qq_app = Some(generate_qq_app);
self
}
pub fn generate_we_app(mut self, generate_we_app: bool) -> Self {
self.generate_we_app = Some(generate_we_app);
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PddAuthorizeQueryRequest {
pub pdd_app_key: String,
pub pdd_app_secret: String,
pub pid: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_parameters: Option<String>,
}
impl PddAuthorizeQueryRequest {
pub fn new(
pdd_app_key: impl Into<String>,
pdd_app_secret: impl Into<String>,
pid: impl Into<String>,
) -> Self {
Self {
pdd_app_key: pdd_app_key.into(),
pdd_app_secret: pdd_app_secret.into(),
pid: pid.into(),
custom_parameters: None,
}
}
pub fn custom_parameters(mut self, custom_parameters: impl Into<String>) -> Self {
self.custom_parameters = Some(custom_parameters.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pdd_convert_request_serialize() {
let request = PddConvertRequest::new("app_key", "app_secret", "pid123", "453581732819");
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["pdd_app_key"], "app_key");
assert_eq!(json["pdd_app_secret"], "app_secret");
assert_eq!(json["pid"], "pid123");
assert_eq!(json["content"], "453581732819");
assert!(json.get("custom_parameters").is_none());
}
#[test]
fn test_pdd_convert_request_with_custom_parameters() {
let request = PddConvertRequest::new("app_key", "app_secret", "pid123", "453581732819")
.custom_parameters(r#"{"uid":"user123"}"#);
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["custom_parameters"], r#"{"uid":"user123"}"#);
}
#[test]
fn test_pdd_goods_detail_simple_request_serialize() {
let request =
PddGoodsDetailSimpleRequest::new("app_key", "app_secret", "pid123", "453581732819");
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["pdd_app_key"], "app_key");
assert_eq!(json["content"], "453581732819");
}
#[test]
fn test_pdd_goods_detail_full_request_serialize() {
let request =
PddGoodsDetailFullRequest::new("app_key", "app_secret", "pid123", "453581732819")
.sort_type(2)
.merchant_type(3)
.with_coupon(true);
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["keyword"], "453581732819");
assert_eq!(json["sort_type"], 2);
assert_eq!(json["merchant_type"], 3);
assert_eq!(json["with_coupon"], true);
}
#[test]
fn test_pdd_order_query_request_serialize() {
let request = PddOrderQueryRequest::new("app_key", "app_secret", 1699200000, 1699286400)
.page(1)
.page_size(50);
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["start_update_time"], 1699200000);
assert_eq!(json["end_update_time"], 1699286400);
assert_eq!(json["page"], 1);
assert_eq!(json["page_size"], 50);
}
#[test]
fn test_pdd_authorize_request_serialize() {
let request =
PddAuthorizeRequest::new("app_key", "app_secret", "pid123").generate_we_app(true);
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["pdd_app_key"], "app_key");
assert_eq!(json["pid"], "pid123");
assert_eq!(json["generate_we_app"], true);
}
#[test]
fn test_optional_fields_not_serialized() {
let request = PddConvertRequest::new("app_key", "app_secret", "pid", "content");
let json_str = serde_json::to_string(&request).unwrap();
assert!(!json_str.contains("custom_parameters"));
}
}