iop_client/
product_country.rs

1use log::info;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5use crate::{
6    constants::{methods, urls},
7    IopClient,
8};
9
10#[derive(Serialize, Deserialize, Debug)]
11struct ProductCountryGetCountryListResponse {
12    #[serde(rename = "alibaba_icbu_product_country_getcountrylist_response")]
13    response: ProductCountryGetCountryList,
14}
15
16#[derive(Serialize, Deserialize, Debug)]
17struct ProductCountryGetCountryList {
18    request_id: Option<String>,
19    _trace_id_: Option<String>,
20    biz_success: bool,
21    trace_id: Option<String>,
22    data: ProductCountryDto,
23}
24
25#[derive(Serialize, Deserialize, Debug)]
26pub struct ProductCountryDto {
27    #[serde(rename = "continent_d_t_o")]
28    pub items: Vec<ProductCountryItem>,
29}
30
31#[derive(Serialize, Deserialize, Debug)]
32pub struct ProductCountryItem {
33    pub continent_name: String,
34    pub continent_code: String,
35
36    #[serde(rename = "country_list")]
37    pub countries: CountryList,
38}
39
40#[derive(Serialize, Deserialize, Debug)]
41pub struct CountryList {
42    #[serde(rename = "country_d_t_o")]
43    pub data: Vec<CountryItem>,
44}
45
46#[derive(Serialize, Deserialize, Debug)]
47pub struct CountryItem {
48    pub country_code: String,
49    pub country_name: String,
50}
51
52impl IopClient {
53    /// 国际站获取商品国家列表
54    ///
55    /// [官方文档](https://open.alibaba.com/doc/api.htm?spm=a2o9m.11193494.0.0.22023a3a2ZhCGD#/api?cid=20966&path=alibaba.icbu.product.country.getcountrylist&methodType=GET/POST)
56    ///
57    /// Retrieves a list of product countries available from the Alibaba ICBU API.
58    ///
59    /// This function constructs the necessary request parameters, generates a signature,
60    /// and sends a request to the Alibaba API to obtain a list of product countries.
61    /// The request includes a language option, though it is currently unused.
62    ///
63    /// # Arguments
64    ///
65    /// * `_language` - An optional language parameter that can be provided for localization,
66    ///   though it is not currently utilized in the request.
67    ///
68    /// # Returns
69    ///
70    /// A `Result` containing a vector of `PhotoAlbumGroup` models if successful, or an error
71    /// if the process fails. The function logs the constructed parameters, the request URL,
72    /// and the response result for debugging purposes.
73    pub async fn list_product_countries(
74        &self,
75        _language: Option<String>,
76    ) -> Result<ProductCountryDto, Box<dyn std::error::Error>> {
77        let mut map = HashMap::new();
78        map.insert("country_request".to_string(), "{}".to_string());
79        map.insert(
80            "method".to_string(),
81            methods::ALIBABA_ICBU_PRODUCT_COUNTRY_GETCOUNTRYLIST.to_string(),
82        );
83        let params = self.build_request_params(map).await;
84
85        let hash = self.generate_sign(None, params.clone());
86        let url = self.generate_url(urls::BASE_SYNC_URL.to_string(), params.clone(), hash);
87        info!("--------list_product_countries-------- url: {:#?}", url);
88
89        let response = self.client.get(&url).send().await?;
90        let result = response
91            .json::<ProductCountryGetCountryListResponse>()
92            .await?;
93
94        Ok(result.response.data)
95    }
96}