robloxapi_s/
lib.rs

1mod errors;
2mod games;
3mod https;
4mod users;
5
6pub use errors::ApiResult;
7pub use games::models::{DevProduct, Game, Server};
8pub use games::GameBuilder;
9pub use https::Https;
10pub use users::models::User;
11pub use users::UserBuilder;
12
13use reqwest::Method;
14
15// STATIC URLS
16//pub(crate) const BASE: &str = "https://api.roblox.com";
17#[allow(dead_code)]
18pub(crate) const AUTH: &str = "https://auth.roblox.com/v1/account/pin/unlock";
19#[allow(dead_code)]
20pub(crate) const ACCOUNT: &str = "https://accountinformation.roblox.com/v1";
21pub(crate) const FRIENDS: &str = "https://friends.roblox.com/v1/";
22#[allow(dead_code)]
23pub(crate) const MESSAGES: &str = "https://privatemessages.roblox.com/v1";
24pub(crate) const USER: &str = "https://users.roblox.com/v1";
25pub(crate) const GAMES: &str = "https://games.roblox.com/v1";
26#[allow(dead_code)]
27pub(crate) const GROUPS: &str = "https://groups.roblox.com/v1";
28#[allow(dead_code)]
29pub(crate) const PRESENCE: &str = "https://presence.roblox.com/v1/presence/users";
30#[allow(dead_code)]
31pub(crate) const ECONOMY: &str = "https://economy.roblox.com/v1/assets";
32#[allow(dead_code)]
33pub(crate) const INVENTORY: &str = "https://inventory.roblox.com";
34#[allow(dead_code)]
35pub(crate) const DEVPAGE: &str = "https://develop.roblox.com/v1/universes";
36
37#[derive(Debug, Clone)]
38pub struct Client {
39    pub session: crate::Https,
40}
41
42impl Default for Client {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl Client {
49    /// Create a new client instance
50    pub fn new() -> Self {
51        Self {
52            session: crate::Https::new(),
53        }
54    }
55
56    /// Create a new user given user_id
57    /// ## Example
58    /// ```
59    ///
60    /// #[tokio::main]
61    /// async fn main() {
62    ///     let mut client = robloxapi::Client::new();
63    ///     let user = client.user(242872495).await;
64    /// }
65    /// ```
66    pub async fn user(&mut self, builder: impl UserBuilder) -> User {
67        builder.new(&mut self.session).await
68    }
69
70    /// Get the current user. Must be logged in with a cookie to get current_user
71    /// # Example
72    /// ```
73    /// #[tokio::main]
74    /// async fn main() {
75    ///     let COOKIE: &str = "";
76    ///     let mut client = robloxapi::Client::new();
77    ///     client.set_cookie(COOKIE).await;
78    ///     let current_user = client.current_user().await;
79    /// }
80    //
81    pub async fn current_user(self: &mut Client) -> User {
82        let data = self
83            .session
84            .request::<serde_json::Value>(Method::GET, "https://www.roblox.com/mobileapi/userinfo")
85            .await
86            .expect("Failed to get user info");
87
88        let builder = data.get("UserID").unwrap().as_u64().unwrap();
89        UserBuilder::new(builder, &mut self.session).await
90    }
91
92    /// Returns a Game struct given the place ID. Get information about a game.
93    /// ## Example
94    /// ```
95    ///
96    /// #[tokio::main]
97    /// async fn main() {
98    ///     let place_id = 7415484311; // Place ID for game
99    ///     let mut client = robloxapi::Client::new(); // Initialize a new client instance
100    ///
101    ///     // Create a new game given place id
102    ///     let game = client.game(place_id).await;
103    /// }
104    /// ````
105    pub async fn game(&self, builder: impl GameBuilder) -> ApiResult<Game> {
106        builder.new(&mut self.session.clone()).await
107    }
108}