Skip to main content

itchio_api/
credentials_info.rs

1use chrono::{DateTime, Utc};
2use serde::Deserialize;
3
4use crate::{Itchio, ItchioError};
5
6/// An object containing information about how you're accessing the API.
7#[derive(Clone, Debug, Deserialize)]
8pub struct CredentialInfo {
9  pub r#type: Option<String>,
10  pub scopes: Option<Vec<String>>,
11  pub expires_at: Option<DateTime<Utc>>,
12}
13
14impl Itchio {
15  /// Get information on how you're accessing the API: <https://itch.io/docs/api/serverside#reference/httpsitchioapi1keycredentialsinfo>
16  pub async fn get_credentials_info(&self) -> Result<CredentialInfo, ItchioError> {
17    Ok(self.request::<CredentialInfo>("credentials/info".to_string()).await?)
18  }
19}
20
21#[cfg(test)]
22mod tests {
23  use super::*;
24  use std::env;
25  use dotenv::dotenv;
26
27  #[tokio::test]
28  async fn good() {
29    dotenv().ok();
30    let client_secret = env::var("KEY").expect("KEY has to be set");
31    let api = Itchio::new(client_secret).unwrap();
32    let credentials = api.get_credentials_info().await.inspect_err(|err| eprintln!("Error spotted: {}", err));
33    assert!(credentials.is_ok())
34  }
35
36  #[tokio::test]
37  async fn bad_key() {
38    let api = Itchio::new("bad_key".to_string()).unwrap();
39    let credentials = api.get_credentials_info().await;
40    assert!(credentials.is_err_and(|err| matches!(err, ItchioError::BadKey)))
41  }
42}