use crate::config::EbayConfig;
use crate::error::{HermesError, HermesResult};
use crate::ebay::auth::EbayAuth;
use std::sync::Arc;
use hermes_ebay_buy_feed::models::{ItemResponse, ItemGroupResponse, ItemPriorityResponse, ItemSnapshotResponse};
use hermes_ebay_buy_feed::apis::configuration::Configuration as FeedConfiguration;
pub struct FeedClient {
config: EbayConfig,
auth: Arc<EbayAuth>,
}
impl FeedClient {
pub fn new(config: EbayConfig) -> HermesResult<Self> {
let auth = Arc::new(EbayAuth::new(config.clone())?);
Ok(Self { config, auth })
}
pub async fn get_item_feed(
&self,
marketplace_id: &str,
range: &str,
feed_scope: &str,
category_id: &str,
date: Option<&str>,
) -> HermesResult<ItemResponse> {
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_feed: {:?}", token_duration);
let mut config = FeedConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/buy/feed/v1".to_string()
} else {
"https://api.ebay.com/buy/feed/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_buy_feed::apis::item_api::get_item_feed(
&config,
"application/gzip", marketplace_id, range, feed_scope, category_id, date, ).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_item_feed 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_feed total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_item_feed error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_item_feed failed: {:?}", e)))
}
}
}
pub async fn get_item_group_feed(
&self,
marketplace_id: &str,
feed_scope: &str,
category_id: &str,
range: Option<&str>,
date: Option<&str>,
) -> HermesResult<ItemGroupResponse> {
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_group_feed: {:?}", token_duration);
let mut config = FeedConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/buy/feed/v1".to_string()
} else {
"https://api.ebay.com/buy/feed/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_buy_feed::apis::item_group_api::get_item_group_feed(
&config,
"application/gzip", marketplace_id, feed_scope, category_id, range, date, ).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_item_group_feed 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_group_feed total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_item_group_feed error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_item_group_feed failed: {:?}", e)))
}
}
}
pub async fn get_item_priority_feed(
&self,
marketplace_id: &str,
range: &str,
category_id: &str,
date: &str,
) -> HermesResult<ItemPriorityResponse> {
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_priority_feed: {:?}", token_duration);
let mut config = FeedConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/buy/feed/v1".to_string()
} else {
"https://api.ebay.com/buy/feed/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_buy_feed::apis::item_priority_api::get_item_priority_feed(
&config,
"application/gzip", marketplace_id, range, category_id, date, ).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_item_priority_feed 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_priority_feed total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_item_priority_feed error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_item_priority_feed failed: {:?}", e)))
}
}
}
pub async fn get_item_snapshot_feed(
&self,
marketplace_id: &str,
range: &str,
category_id: &str,
snapshot_date: &str,
) -> HermesResult<ItemSnapshotResponse> {
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_snapshot_feed: {:?}", token_duration);
let mut config = FeedConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/buy/feed/v1".to_string()
} else {
"https://api.ebay.com/buy/feed/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_buy_feed::apis::item_snapshot_api::get_item_snapshot_feed(
&config,
"application/gzip", marketplace_id, range, category_id, snapshot_date, ).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_item_snapshot_feed 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_snapshot_feed total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_item_snapshot_feed error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_item_snapshot_feed failed: {:?}", e)))
}
}
}
}