use crate::config::EbayConfig;
use crate::error::{HermesError, HermesResult};
use crate::ebay::auth::EbayAuth;
use std::sync::Arc;
use hermes_ebay_buy_offer::models::{Bidding, PlaceProxyBidRequest, PlaceProxyBidResponse};
use hermes_ebay_buy_offer::apis::configuration::Configuration as OfferConfiguration;
pub struct OfferClient {
config: EbayConfig,
auth: Arc<EbayAuth>,
}
impl OfferClient {
pub fn new(config: EbayConfig) -> HermesResult<Self> {
let auth = Arc::new(EbayAuth::new(config.clone())?);
Ok(Self { config, auth })
}
pub async fn get_bidding(
&self,
item_id: &str,
marketplace_id: &str,
) -> HermesResult<Bidding> {
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_bidding: {:?}", token_duration);
let mut config = OfferConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/buy/offer/v1".to_string()
} else {
"https://api.ebay.com/buy/offer/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_buy_offer::apis::bidding_api::get_bidding(
&config,
item_id,
marketplace_id,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_bidding 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_bidding total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_bidding error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_bidding failed: {:?}", e)))
}
}
}
pub async fn place_proxy_bid(
&self,
item_id: &str,
marketplace_id: &str,
bid_request: &PlaceProxyBidRequest,
) -> HermesResult<PlaceProxyBidResponse> {
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 place_proxy_bid: {:?}", token_duration);
let mut config = OfferConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/buy/offer/v1".to_string()
} else {
"https://api.ebay.com/buy/offer/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_buy_offer::apis::bidding_api::place_proxy_bid(
&config,
item_id,
marketplace_id,
"application/json",
Some(bid_request.clone()),
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay place_proxy_bid 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!("place_proxy_bid total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay place_proxy_bid error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay place_proxy_bid failed: {:?}", e)))
}
}
}
pub async fn can_bid_on_item(
&self,
item_id: &str,
marketplace_id: &str,
) -> HermesResult<bool> {
match self.get_bidding(item_id, marketplace_id).await {
Ok(bidding) => {
Ok(bidding.auction_status.as_ref()
.map(|status| status != "ENDED")
.unwrap_or(false))
},
Err(_) => Ok(false), }
}
}