steam_rs/steam_user/
get_player_summaries.rs

1//! Implements the `GetPlayerSummaries` endpoint
2
3use serde::{Deserialize, Serialize};
4use serde_json::{from_value, Value};
5
6use crate::{
7    errors::{ErrorHandle, SteamUserError},
8    macros::do_http,
9    steam_id::{de_steamid_from_str, SteamId},
10    Steam, BASE,
11};
12
13use super::INTERFACE;
14
15const ENDPOINT: &str = "GetPlayerSummaries";
16const VERSION: &str = "0002";
17
18/// Represents a user profile object.
19///
20/// Contained information varies depending on whether or not the user has their profile set to Friends only or Private.
21#[derive(Serialize, Deserialize, Debug, Clone)]
22pub struct Player {
23    /// The user's 64-bit ID
24    #[serde(rename = "steamid")]
25    #[serde(deserialize_with = "de_steamid_from_str")]
26    pub steam_id: SteamId,
27
28    /// User's display name.
29    #[serde(rename = "personaname")]
30    pub persona_name: String,
31
32    /// The URL to the user's Steam Community profile.
33    #[serde(rename = "profileurl")]
34    pub profile_url: String,
35
36    /// The URL to the user's avatar as a 32x32 image
37    pub avatar: String,
38
39    /// The URL to the user's avatar as a 64x64 image
40    #[serde(rename = "avatarmedium")]
41    pub avatar_medium: String,
42
43    /// The URL to the user's avatar as a 184x184 image
44    #[serde(rename = "avatarfull")]
45    pub avatar_full: String,
46
47    /// Hash of the user's avatar
48    #[serde(rename = "avatarhash")]
49    pub avatar_hash: String,
50
51    /// The user's status
52    /// - 0: Offline (Also set when the profile is Private)
53    /// - 1: Online
54    /// - 2: Busy
55    /// - 3: Away
56    /// - 4: Snooze
57    /// - 5: Looking to trade
58    /// - 6: Looking to play
59    #[serde(rename = "personastate")]
60    pub persona_state: u8,
61
62    /// An integer that describes the access setting of the profile.
63    /// - 1: Private
64    /// - 2: Friends only
65    /// - 3: Friends of Friends
66    /// - 4: Users Only
67    /// - 5: Public
68    #[serde(rename = "communityvisibilitystate")]
69    pub community_visibility_state: u8,
70
71    /// If set to 1, the user has configured the profile.
72    #[serde(rename = "profilestate")]
73    pub profile_state: Option<u8>,
74
75    /// A unix timestamp of when the user was last online.
76    #[serde(rename = "lastlogoff")]
77    pub last_logoff: Option<u64>,
78
79    /// If present the profile allows public comments.
80    #[serde(rename = "commentpermission")]
81    pub comment_permission: Option<u8>,
82
83    /// The user's real name.
84    #[serde(rename = "realname")]
85    pub real_name: Option<String>,
86
87    /// The 64-bit ID of the user's primary group.
88    #[serde(rename = "primaryclanid")]
89    pub primary_clan_id: Option<String>,
90
91    /// A unix timestamp of the date the profile was created.
92    #[serde(rename = "timecreated")]
93    pub time_created: Option<u64>,
94
95    /// If the user is in game this will be set to it's app ID as a string.
96    #[serde(rename = "gameid")]
97    pub game_id: Option<String>,
98
99    /// The server URL given as an IP address and port number separated by a colon.
100    /// This will not be present or set to "0.0.0.0:0" if none is available.
101    #[serde(rename = "gameserverip")]
102    pub game_server_ip: Option<String>,
103
104    /// The title of the game that the user is playing.
105    #[serde(rename = "gameextrainfo")]
106    pub game_extra_info: Option<String>,
107
108    /// ISO 3166 code of where the user is located.
109    #[serde(rename = "loccountrycode")]
110    pub loc_country_code: Option<String>,
111
112    /// Variable length code representing the state the user is located in.
113    #[serde(rename = "locstatecode")]
114    pub loc_state_code: Option<String>,
115
116    /// An integer ID internal to Steam representing the user's city.
117    #[serde(rename = "loccityid")]
118    pub loc_city_id: Option<u64>,
119}
120
121#[derive(Serialize, Deserialize, Debug)]
122struct PlayerSummary {
123    /// A list of profile objects. Contained information varies depending on
124    /// whether or not the user has their profile set to Friends only or Private.
125    players: Vec<Player>,
126}
127
128#[derive(Serialize, Deserialize, Debug)]
129struct Wrapper {
130    response: PlayerSummary,
131}
132
133impl Steam {
134    /// Get user profile data.
135    ///
136    /// # Arguments
137    ///
138    /// * `steam_ids` - A vector of `SteamId` objects
139    pub async fn get_player_summaries(
140        &self,
141        steam_ids: Vec<SteamId>,
142    ) -> Result<Vec<Player>, SteamUserError> {
143        let steam_ids: String = steam_ids.iter().map(|&id| id.to_string() + ",").collect();
144
145        let query = format!("?key={}&steamids={}", &self.api_key, steam_ids);
146        let url = format!("{}/{}/{}/v{}/{}", BASE, INTERFACE, ENDPOINT, VERSION, query);
147
148        let json = do_http!(url, Value, ErrorHandle, SteamUserError::GetPlayerSummaries);
149        let wrapper: Wrapper = ErrorHandle!(
150            from_value(json.to_owned()),
151            SteamUserError::GetPlayerSummaries
152        );
153
154        Ok(wrapper.response.players)
155    }
156}