use log::info;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::{
constants::{methods, urls},
IopClient,
};
#[derive(Serialize, Deserialize, Debug)]
struct ProductCountryGetCountryListResponse {
#[serde(rename = "alibaba_icbu_product_country_getcountrylist_response")]
response: ProductCountryGetCountryList,
}
#[derive(Serialize, Deserialize, Debug)]
struct ProductCountryGetCountryList {
request_id: Option<String>,
_trace_id_: Option<String>,
biz_success: bool,
trace_id: Option<String>,
data: ProductCountryDto,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProductCountryDto {
#[serde(rename = "continent_d_t_o")]
pub items: Vec<ProductCountryItem>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProductCountryItem {
pub continent_name: String,
pub continent_code: String,
#[serde(rename = "country_list")]
pub countries: CountryList,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CountryList {
#[serde(rename = "country_d_t_o")]
pub data: Vec<CountryItem>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CountryItem {
pub country_code: String,
pub country_name: String,
}
impl IopClient {
pub async fn list_product_countries(
&self,
_language: Option<String>,
) -> Result<ProductCountryDto, Box<dyn std::error::Error>> {
let mut map = HashMap::new();
map.insert("country_request".to_string(), "{}".to_string());
map.insert(
"method".to_string(),
methods::ALIBABA_ICBU_PRODUCT_COUNTRY_GETCOUNTRYLIST.to_string(),
);
let params = self.build_request_params(map).await;
let hash = self.generate_sign(None, params.clone());
let url = self.generate_url(urls::BASE_SYNC_URL.to_string(), params.clone(), hash);
info!("--------list_product_countries-------- url: {:#?}", url);
let response = self.client.get(&url).send().await?;
let result = response
.json::<ProductCountryGetCountryListResponse>()
.await?;
Ok(result.response.data)
}
}