use crate::config::EbayConfig;
use crate::error::{HermesError, HermesResult};
use crate::ebay::auth::EbayAuth;
use std::sync::Arc;
use hermes_ebay_sell_negotiation::models::{
PagedEligibleItemCollection, CreateOffersRequest,
};
use hermes_ebay_sell_negotiation::apis::configuration::Configuration as NegotiationConfiguration;
pub struct NegotiationClient {
config: EbayConfig,
auth: Arc<EbayAuth>,
}
impl NegotiationClient {
pub fn new(config: EbayConfig) -> HermesResult<Self> {
let auth = Arc::new(EbayAuth::new(config.clone())?);
Ok(Self { config, auth })
}
pub async fn find_eligible_items(
&self,
marketplace_id: &str,
limit: Option<&str>,
offset: Option<&str>,
) -> HermesResult<PagedEligibleItemCollection> {
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 find_eligible_items: {:?}", token_duration);
let mut config = NegotiationConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/negotiation/v1".to_string()
} else {
"https://api.ebay.com/sell/negotiation/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_negotiation::apis::offer_api::find_eligible_items(
&config,
marketplace_id,
limit,
offset,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay find_eligible_items 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!("find_eligible_items total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay find_eligible_items error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay find_eligible_items failed: {:?}", e)))
}
}
}
pub async fn send_offer_to_interested_buyers(
&self,
marketplace_id: &str,
create_offers_request: &CreateOffersRequest,
) -> HermesResult<()> {
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 send_offer_to_interested_buyers: {:?}", token_duration);
let mut config = NegotiationConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/negotiation/v1".to_string()
} else {
"https://api.ebay.com/sell/negotiation/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_negotiation::apis::offer_api::send_offer_to_interested_buyers(
&config,
marketplace_id,
"application/json",
Some(create_offers_request.clone()),
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay send_offer_to_interested_buyers API call: {:?}", ebay_duration);
match result {
Ok(_) => {
let total_duration = start_time.elapsed();
let our_processing = total_duration - token_duration - ebay_duration;
tracing::info!("send_offer_to_interested_buyers total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(())
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay send_offer_to_interested_buyers error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay send_offer_to_interested_buyers failed: {:?}", e)))
}
}
}
}