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};

/// An object containing information about how you're accessing the API.
#[derive(Clone, Debug, Deserialize)]
pub struct CredentialInfo {
  pub r#type: Option<String>,
  pub scopes: Option<Vec<String>>,
  pub expires_at: Option<DateTime<Utc>>,
}

impl Itchio {
  /// Get information on how you're accessing the API: <https://itch.io/docs/api/serverside#reference/httpsitchioapi1keycredentialsinfo>
  pub async fn get_credentials_info(&self) -> Result<CredentialInfo, ItchioError> {
    Ok(self.request::<CredentialInfo>("credentials/info".to_string()).await?)
  }
}

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

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