use chrono::{DateTime, Utc};
use serde::Deserialize;
use crate::{Itchio, ItchioError, parsers::date_from_str};
#[derive(Clone, Debug, Deserialize)]
struct WrappedPurchases {
purchases: Vec<Purchase>
}
#[derive(Clone, Debug, Deserialize)]
pub struct Purchase {
pub id: u64,
#[serde(deserialize_with = "date_from_str")]
pub created_at: DateTime<Utc>,
pub game_id: u32,
pub sale_rate: u64,
pub donation: bool,
pub price: String,
pub source: String,
pub email: String,
}
impl Itchio {
pub async fn get_purchases(&self, game_id: u32, lookup: &str, property_name: &str) -> Result<Vec<Purchase>, ItchioError> {
let url = format!("game/{}/purchases?{}={}", game_id, property_name, lookup);
let response = self.request::<WrappedPurchases>(url).await?;
Ok(response.purchases)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn bad_key() {
let api = Itchio::new("bad_key".to_string()).unwrap();
let purchases = api.get_purchases(
2295061,
"someone@somewhere.thatdoesnotexist",
"email"
).await;
assert!(purchases.is_err_and(|err| matches!(err, ItchioError::BadKey)))
}
}