use crate::config::EbayConfig;
use crate::error::{HermesError, HermesResult};
use crate::ebay::auth::EbayAuth;
use std::sync::Arc;
use hermes_ebay_sell_finances::models::{
Payout, Payouts, SellerFundsSummaryResponse, Transactions,
};
use hermes_ebay_sell_finances::apis::configuration::Configuration as FinancesConfiguration;
pub struct FinancesClient {
config: EbayConfig,
auth: Arc<EbayAuth>,
}
impl FinancesClient {
pub fn new(config: EbayConfig) -> HermesResult<Self> {
let auth = Arc::new(EbayAuth::new(config.clone())?);
Ok(Self { config, auth })
}
pub async fn get_payout(
&self,
marketplace_id: &str,
payout_id: &str,
) -> HermesResult<Payout> {
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_payout: {:?}", token_duration);
let mut config = FinancesConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/finances/v1".to_string()
} else {
"https://api.ebay.com/sell/finances/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_finances::apis::payout_api::get_payout(
&config,
marketplace_id,
payout_id,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_payout 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_payout total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_payout error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_payout failed: {:?}", e)))
}
}
}
pub async fn get_payouts(
&self,
marketplace_id: &str,
filter: Option<&str>,
limit: Option<&str>,
offset: Option<&str>,
sort: Option<&str>,
) -> HermesResult<Payouts> {
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_payouts: {:?}", token_duration);
let mut config = FinancesConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/finances/v1".to_string()
} else {
"https://api.ebay.com/sell/finances/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_finances::apis::payout_api::get_payouts(
&config,
marketplace_id,
filter,
limit,
offset,
sort,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_payouts 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_payouts total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_payouts error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_payouts failed: {:?}", e)))
}
}
}
pub async fn get_seller_funds_summary(
&self,
marketplace_id: &str,
) -> HermesResult<SellerFundsSummaryResponse> {
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_seller_funds_summary: {:?}", token_duration);
let mut config = FinancesConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/finances/v1".to_string()
} else {
"https://api.ebay.com/sell/finances/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_finances::apis::seller_funds_summary_api::get_seller_funds_summary(
&config,
marketplace_id,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_seller_funds_summary 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_seller_funds_summary total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_seller_funds_summary error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_seller_funds_summary failed: {:?}", e)))
}
}
}
pub async fn get_transactions(
&self,
marketplace_id: &str,
filter: Option<&str>,
limit: Option<&str>,
offset: Option<&str>,
sort: Option<&str>,
) -> HermesResult<Transactions> {
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_transactions: {:?}", token_duration);
let mut config = FinancesConfiguration::new();
config.base_path = if self.config.sandbox {
"https://api.sandbox.ebay.com/sell/finances/v1".to_string()
} else {
"https://api.ebay.com/sell/finances/v1".to_string()
};
config.oauth_access_token = Some(token);
let ebay_start = std::time::Instant::now();
let result = hermes_ebay_sell_finances::apis::transaction_api::get_transactions(
&config,
marketplace_id,
filter,
limit,
offset,
sort,
).await;
let ebay_duration = ebay_start.elapsed();
tracing::info!("eBay get_transactions 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_transactions total: {:?} | Our processing: {:?}", total_duration, our_processing);
Ok(response)
},
Err(e) => {
let total_duration = start_time.elapsed();
tracing::error!("eBay get_transactions error after {:?}: {:?}", total_duration, e);
Err(HermesError::ApiRequest(format!("eBay get_transactions failed: {:?}", e)))
}
}
}
}