#![allow(missing_docs)]
use crate::games::request_types::{CreatorInformation, RootPlaceInformation};
use crate::{Client, RoboatError};
use serde::{Deserialize, Serialize};
const GAMES_V2_API: &str = "https://games.roblox.com/v2";
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GameInformationV2 {
pub id: u64,
pub name: String,
pub description: Option<String>,
pub creator: CreatorInformation,
pub root_place: RootPlaceInformation,
pub created: String,
pub updated: String,
pub place_visits: u64,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GamesResponseV2 {
pub previous_page_cursor: Option<String>,
pub next_page_cursor: Option<String>,
pub data: Vec<GameInformationV2>,
}
mod request_types;
impl Client {
pub async fn user_games(&self, user_id: u64) -> Result<GamesResponseV2, RoboatError> {
match self.user_games_internal(user_id).await {
Ok(x) => Ok(x),
Err(e) => Err(e),
}
}
pub async fn group_games(&self, group_id: u64) -> Result<GamesResponseV2, RoboatError> {
match self.group_games_interal(group_id).await {
Ok(x) => Ok(x),
Err(e) => Err(e),
}
}
}
mod internal {
use crate::{
games::{GamesResponseV2, GAMES_V2_API},
Client, RoboatError,
};
impl Client {
pub(super) async fn user_games_internal(
&self,
user_id: u64,
) -> Result<GamesResponseV2, RoboatError> {
let formatted_url = format!("{}/users/{}/games?limit=50", GAMES_V2_API, user_id);
let request_result = self.reqwest_client.get(formatted_url).send().await;
let response = Self::validate_request_result(request_result).await?;
let users_games_json = Self::parse_to_raw::<GamesResponseV2>(response).await?;
Ok(users_games_json)
}
pub(super) async fn group_games_interal(
&self,
group_id: u64,
) -> Result<GamesResponseV2, RoboatError> {
let formatted_url = format!("{}/groups/{}/gamesv2?limit=100", GAMES_V2_API, group_id);
let request_result = self.reqwest_client.get(formatted_url).send().await;
let response = Self::validate_request_result(request_result).await?;
let group_games_json = Self::parse_to_raw::<GamesResponseV2>(response).await?;
Ok(group_games_json)
}
}
}