itchio-api 0.3.0

Easily interact with the itch.io server-side API
Documentation
use serde::Deserialize;

use crate::{Itchio, ItchioError, User};

/// The original response this crate gets from the server, useless to crate users.
#[derive(Clone, Debug, Deserialize)]
struct WrappedUser {
  user: User
}

impl Itchio {
  /// Get your profile: <https://itch.io/docs/api/serverside#reference/profileme-httpsitchioapi1keyme>
  pub async fn get_me(&self) -> Result<User, ItchioError> {
    let response = self.request::<WrappedUser>("me".to_string()).await?;
    Ok(response.user)
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use std::env;
  use dotenv::dotenv;

  #[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 me = api.get_my_games().await.inspect_err(|err| eprintln!("Error spotted: {}", err));
    assert!(me.is_ok())
  }

  #[tokio::test]
  async fn bad_key() {
    let api = Itchio::new("bad_key".to_string()).unwrap();
    let me = api.get_me().await;
    assert!(me.is_err_and(|err| matches!(err, ItchioError::BadKey)))
  }
}