use crate::client::{AuthState, Client};
use crate::error::{ApiErrorResponse, Error, Result};
use crate::internal::V1_API_URL;
use crate::models::ItemStatistics;
use super::V1ApiResponse;
impl<S: AuthState> Client<S> {
pub async fn get_item_statistics(&self, slug: &str) -> Result<ItemStatistics> {
self.wait_for_rate_limit().await;
let response = self
.http
.get(format!("{}/items/{}/statistics", V1_API_URL, slug))
.send()
.await
.map_err(Error::Network)?;
let status = response.status();
if status == reqwest::StatusCode::NOT_FOUND {
return Err(Error::not_found(format!("Item not found: {}", slug)));
}
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
if let Ok(error_response) = serde_json::from_str::<ApiErrorResponse>(&body) {
return Err(Error::api_with_response(
status,
format!("Failed to fetch statistics for item: {}", slug),
error_response,
));
}
return Err(Error::api(
status,
format!("Failed to fetch statistics for {}: {}", slug, body),
));
}
let body = response.text().await.map_err(Error::Network)?;
let api_response: V1ApiResponse<ItemStatistics> =
serde_json::from_str(&body).map_err(|e| Error::parse_with_body(e.to_string(), body))?;
Ok(api_response.payload)
}
}