itchio-api 0.3.0

Easily interact with the itch.io server-side API
Documentation
use chrono::{DateTime, Utc};
use serde::Deserialize;

use crate::{Itchio, ItchioError, parsers::date_from_str};

/// The original response this crate gets from the server, useless to crate users.
#[derive(Clone, Debug, Deserialize)]
struct WrappedPurchases {
  purchases: Vec<Purchase>
}

/// A representation of a purchase made on itch.io.
#[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,
  /// Is true for any purchase that doesn’t have a download key associated with it, currently this only applies to web games
  pub donation: bool,
  pub price: String,
  pub source: String,
  pub email: String,
}

impl Itchio {
  /// Gets the successful purchases from someone on a given game: <https://itch.io/docs/api/serverside#reference/gameviewpurchases-httpsitchioapi1keygamegame-idpurchases>
  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::*;
  // use std::env;
  // use dotenv::dotenv;

  // No one's ever purchased one of my games so I'm unable to test this, lol

  // #[tokio::test]
  // async fn good() {
  //   dotenv().ok();
  //   let client_secret = env::var("KEY").expect("KEY has to be set");
  //   let api = Itchio::new(client_secret).unwrap();
  //   let purchases = api.get_purchases(
  //     2295061,
  //     "someone@somewhere.thatdoesnotexist",
  //     "email"
  //   ).await.inspect_err(|err| eprintln!("Error spotted: {}", err));
  //   assert!(purchases.is_ok())
  // }

  #[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)))
  }
}