steam_api_wrapper/services/
get_recently_played_games.rs

1use anyhow::Result;
2use serde::__private::Ok;
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    generics::{BASE_URL, GET_RECENTLY_PLAYED_GAMES, IPLAYER_SERVICE, VERSION_V1},
7    helpers::make_api_call::{api_call, FunctionResult},
8    Steam,
9};
10
11#[derive(Clone, Debug, Deserialize, Serialize)]
12pub struct Game {
13    #[serde(rename = "appid")]
14    pub app_id: u64,
15
16    #[serde(rename = "name")]
17    pub name: String,
18
19    #[serde(rename = "playtime_2weeks")]
20    pub recently_played: u64,
21
22    #[serde(rename = "playtime_forever")]
23    pub overall_plat_time: u64,
24
25    pub img_icon_url: String,
26
27    pub img_logo_url: Option<String>,
28}
29
30#[derive(Clone, Debug, Deserialize, Serialize)]
31pub struct Games {
32    pub total_count: u64,
33    pub games: Vec<Game>,
34}
35
36#[derive(Clone, Debug, Deserialize, Serialize)]
37pub struct RecentlyPlayedSummary {
38    pub response: Games,
39}
40
41impl Steam {
42    pub async fn get_recently_played_games(&self, steam_id: u64) -> Result<Games> {
43        let query = format!("?key={}&steamid={}", &self.api_key, steam_id);
44        let url = format!(
45            "{}/{}/{}/{}/{}",
46            BASE_URL, IPLAYER_SERVICE, GET_RECENTLY_PLAYED_GAMES, VERSION_V1, query
47        );
48
49        // Call the api_call function
50        match api_call::<RecentlyPlayedSummary>(url).await {
51            FunctionResult::Success(response) => Ok(response.response),
52            FunctionResult::Error(err) => Err(err),
53        }
54    }
55}