use crate::config::EbayConfig;
use crate::error::{HermesError, HermesResult};
use crate::ebay::auth::EbayAuth;
use std::sync::Arc;
use hermes_ebay_sell_metadata::models::{
CategoryPolicyResponse, ItemConditionPolicyResponse,
ReturnPolicyResponse, ShippingPoliciesResponse, GetCurrenciesResponse,
};
use hermes_ebay_sell_metadata::apis::configuration::Configuration as MetadataConfiguration;
pub struct MetadataClient {
config: EbayConfig,
auth: Arc<EbayAuth>,
}
impl MetadataClient {
pub fn new(config: EbayConfig) -> HermesResult<Self> {
let auth = Arc::new(EbayAuth::new(config.clone())?);
Ok(Self { config, auth })
}
pub async fn get_category_policies(
&self,
marketplace_id: &str,
filter: Option<&str>,
) -> HermesResult<CategoryPolicyResponse> {
let start_time = std::time::Instant::now();
let token_start = std::time::Instant::now();
let token = self.auth.get_access_token().await?;
let token_duration = token_start.elapsed();
tracing::info!("OAuth token request for get_category_policies: {:?}", token_duration);
let mut config = MetadataConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/metadata/v1".to_string()
} else {
"https://api.ebay.com/sell/metadata/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_metadata::apis::marketplace_api::get_category_policies(
&config,
marketplace_id,
filter,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_category_policies API call: {:?}", ebay_duration);
match result {
Ok(response) => {
let total_duration = start_time.elapsed();
let our_processing = total_duration - token_duration - ebay_duration;
tracing::info!("get_category_policies total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_category_policies error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_category_policies failed: {:?}", e)))
}
}
}
pub async fn get_item_condition_policies(
&self,
marketplace_id: &str,
filter: Option<&str>,
) -> HermesResult<ItemConditionPolicyResponse> {
let start_time = std::time::Instant::now();
let token_start = std::time::Instant::now();
let token = self.auth.get_access_token().await?;
let token_duration = token_start.elapsed();
tracing::info!("OAuth token request for get_item_condition_policies: {:?}", token_duration);
let mut config = MetadataConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/metadata/v1".to_string()
} else {
"https://api.ebay.com/sell/metadata/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_metadata::apis::marketplace_api::get_item_condition_policies(
&config,
marketplace_id,
filter,
None, ).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_item_condition_policies API call: {:?}", ebay_duration);
match result {
Ok(response) => {
let total_duration = start_time.elapsed();
let our_processing = total_duration - token_duration - ebay_duration;
tracing::info!("get_item_condition_policies total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_item_condition_policies error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_item_condition_policies failed: {:?}", e)))
}
}
}
pub async fn get_return_policies(
&self,
marketplace_id: &str,
filter: Option<&str>,
) -> HermesResult<ReturnPolicyResponse> {
let start_time = std::time::Instant::now();
let token_start = std::time::Instant::now();
let token = self.auth.get_access_token().await?;
let token_duration = token_start.elapsed();
tracing::info!("OAuth token request for get_return_policies: {:?}", token_duration);
let mut config = MetadataConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/metadata/v1".to_string()
} else {
"https://api.ebay.com/sell/metadata/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_metadata::apis::marketplace_api::get_return_policies(
&config,
marketplace_id,
filter,
None, ).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_return_policies API call: {:?}", ebay_duration);
match result {
Ok(response) => {
let total_duration = start_time.elapsed();
let our_processing = total_duration - token_duration - ebay_duration;
tracing::info!("get_return_policies total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_return_policies error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_return_policies failed: {:?}", e)))
}
}
}
pub async fn get_shipping_policies(
&self,
marketplace_id: &str,
filter: Option<&str>,
) -> HermesResult<ShippingPoliciesResponse> {
let start_time = std::time::Instant::now();
let token_start = std::time::Instant::now();
let token = self.auth.get_access_token().await?;
let token_duration = token_start.elapsed();
tracing::info!("OAuth token request for get_shipping_policies: {:?}", token_duration);
let mut config = MetadataConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/metadata/v1".to_string()
} else {
"https://api.ebay.com/sell/metadata/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_metadata::apis::marketplace_api::get_shipping_policies(
&config,
marketplace_id,
filter,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_shipping_policies API call: {:?}", ebay_duration);
match result {
Ok(response) => {
let total_duration = start_time.elapsed();
let our_processing = total_duration - token_duration - ebay_duration;
tracing::info!("get_shipping_policies total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_shipping_policies error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_shipping_policies failed: {:?}", e)))
}
}
}
pub async fn get_currencies(
&self,
marketplace_id: &str,
accept_language: Option<&str>,
) -> HermesResult<GetCurrenciesResponse> {
let start_time = std::time::Instant::now();
let token_start = std::time::Instant::now();
let token = self.auth.get_access_token().await?;
let token_duration = token_start.elapsed();
tracing::info!("OAuth token request for get_currencies: {:?}", token_duration);
let mut config = MetadataConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/metadata/v1".to_string()
} else {
"https://api.ebay.com/sell/metadata/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_metadata::apis::marketplace_api::get_currencies(
&config,
marketplace_id,
accept_language,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_currencies API call: {:?}", ebay_duration);
match result {
Ok(response) => {
let total_duration = start_time.elapsed();
let our_processing = total_duration - token_duration - ebay_duration;
tracing::info!("get_currencies total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_currencies error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_currencies failed: {:?}", e)))
}
}
}
}