1use chrono::{DateTime, Utc};
2use serde::Deserialize;
3
4use crate::{Itchio, ItchioError, parsers::date_from_str};
5
6#[derive(Clone, Debug, Deserialize)]
8struct WrappedPurchases {
9 purchases: Vec<Purchase>
10}
11
12#[derive(Clone, Debug, Deserialize)]
14pub struct Purchase {
15 pub id: u64,
16 #[serde(deserialize_with = "date_from_str")]
17 pub created_at: DateTime<Utc>,
18 pub game_id: u32,
19 pub sale_rate: u64,
20 pub donation: bool,
22 pub price: String,
23 pub source: String,
24 pub email: String,
25}
26
27impl Itchio {
28 pub async fn get_purchases(&self, game_id: u32, lookup: &str, property_name: &str) -> Result<Vec<Purchase>, ItchioError> {
30 let url = format!("game/{}/purchases?{}={}", game_id, property_name, lookup);
31 let response = self.request::<WrappedPurchases>(url).await?;
32 Ok(response.purchases)
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39 #[tokio::test]
58 async fn bad_key() {
59 let api = Itchio::new("bad_key".to_string()).unwrap();
60 let purchases = api.get_purchases(
61 2295061,
62 "someone@somewhere.thatdoesnotexist",
63 "email"
64 ).await;
65 assert!(purchases.is_err_and(|err| matches!(err, ItchioError::BadKey)))
66 }
67}