Skip to main content

itchio_api/endpoints/
me.rs

1use serde::Deserialize;
2use url::Url;
3
4use crate::{Itchio, ItchioError};
5
6/// The original response this crate gets from the server, useless to crate users.
7#[derive(Clone, Debug, Deserialize)]
8struct WrappedUser {
9  user: User
10}
11
12#[derive(Clone, Debug, Deserialize)] #[allow(dead_code)]
13pub struct User {
14  username: String,
15  display_name: String,
16  url: Url,
17  cover_url: Url,
18  press_user: bool,
19  developer: bool,
20  gamer: bool,
21}
22
23impl Itchio {
24  /// Get your profile: <https://itch.io/docs/api/serverside#reference/profileme-httpsitchioapi1keyme>
25  pub async fn get_me(&self) -> Result<User, ItchioError> {
26    let response = self.request::<WrappedUser>("me".to_string()).await?;
27    Ok(response.user)
28  }
29}
30
31#[cfg(test)]
32mod tests {
33  use super::*;
34  use std::env;
35  use dotenv::dotenv;
36
37  #[tokio::test]
38  async fn good() {
39    dotenv().ok();
40    let client_secret = env::var("KEY").expect("KEY has to be set");
41    let api = Itchio::new(client_secret);
42    let me = api.get_my_games().await.inspect_err(|err| eprintln!("Error spotted: {}", err));
43    assert!(me.is_ok())
44  }
45
46  #[tokio::test]
47  async fn bad() {
48    let api = Itchio::new("bad_key".to_string());
49    let me = api.get_me().await;
50    assert!(me.is_err_and(|err| matches!(err, ItchioError::BadKey)))
51  }
52}