Skip to content

Commit

Permalink
Merge pull request #50 from Sleitnick/feature/user
Browse files Browse the repository at this point in the history
User API
  • Loading branch information
Sleitnick authored Jun 11, 2024
2 parents 5bb25fd + ef60732 commit cfd17da
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rbxcloud"
version = "0.11.0"
version = "0.12.0"
description = "CLI and SDK for the Roblox Open Cloud APIs"
authors = ["Stephen Leitnick"]
license = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Possible use-cases:
| :white_check_mark: | Subscriptions |
| :x: | Inventory |
| :white_check_mark: | User Notifications |
| :x: | User |
| :white_check_mark: | User |
| :x: | Creator Store |

- :white_check_mark: = Supported
Expand All @@ -44,7 +44,7 @@ The goal of this project is to support all API endpoints that Roblox provides.
### Aftman
Run the `aftman add` command within your project directory. This will add `rbxcloud` to the project's `aftman.toml` file (or create one if it doesn't yet exist).
```sh
$ aftman add Sleitnick/rbxcloud@0.11.0
$ aftman add Sleitnick/rbxcloud@0.12.0
```

### From Release
Expand All @@ -61,7 +61,7 @@ The library built for the CLI tool is available to use directly in Rust projects
To use `rbxcloud` in a Rust project, simply add `rbxcloud` to the `Cargo.toml` dependency list.
```toml
[dependencies]
rbxcloud = "0.11.0"
rbxcloud = "0.12.0"
```

Alternatively, use `cargo add`.
Expand Down
4 changes: 2 additions & 2 deletions docs/cli/cli-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ There are a few different ways to install the `rbxcloud` CLI.
### [Aftman](https://github.com/LPGhatguy/aftman) <small>(Preferred)</small>
Run the `aftman add` command within your project directory. This will add `rbxcloud` to the project's `aftman.toml` file (or create one if it doesn't yet exist).
```sh
$ aftman add Sleitnick/rbxcloud@0.11.0
$ aftman add Sleitnick/rbxcloud@0.12.0
```

Next, run `aftman install` to install `rbxcloud`.
Expand All @@ -17,7 +17,7 @@ Add `rbxcloud` under the `[tools]` section of your `foreman.toml` file.
```toml
# foreman.toml
[tools]
rbxcloud = { github = "Sleitnick/rbxcloud", version = "0.11.0" }
rbxcloud = { github = "Sleitnick/rbxcloud", version = "0.12.0" }
```

Next, run `foreman install` to install `rbxcloud`.
Expand Down
36 changes: 36 additions & 0 deletions docs/cli/cli-user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# User CLI

## Getting User Information
```
Usage: rbxcloud user get [OPTIONS] --user-id <USER_ID> --api-key <API_KEY>
Options:
-u, --user-id <USER_ID> User ID
-p, --pretty Pretty-print the JSON response
-a, --api-key <API_KEY> Roblox Open Cloud API Key [env: RBXCLOUD_API_KEY=]
-h, --help Print help
```

### Example
```
$ rbxcloud user get -p -u 12345 -a MY_KEY
```

## Generating User Thumbnail
```
Usage: rbxcloud user thumbnail [OPTIONS] --user-id <USER_ID> --api-key <API_KEY>
Options:
-u, --user-id <USER_ID> User ID
-s, --size <SIZE> Thumbnail size [possible values: size48x48, size50x50, size60x60, size75x75, size100x100, size110x110, size150x150, size180x180, size352x352, size420x420, size720x720]
-f, --format <FORMAT> Thumbnail format [possible values: png, jpeg]
-S, --shape <SHAPE> Thumbnail shape [possible values: round, square]
-p, --pretty Pretty-print the JSON response
-a, --api-key <API_KEY> Roblox Open Cloud API Key [env: RBXCLOUD_API_KEY=]
-h, --help Print help
```

### Example
```
$ rbxcloud user thumbnail -p -u 12345 -s size100x100 -f png -S round -a MY_KEY
```
2 changes: 1 addition & 1 deletion docs/lib/lib-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
To use `rbxcloud` in a Rust project, simply add `rbxcloud` to the `Cargo.toml` dependency list.
```toml
[dependencies]
rbxcloud = "0.11.0"
rbxcloud = "0.12.0"
```

Alternatively, use `cargo add`.
Expand Down
30 changes: 30 additions & 0 deletions examples/user-info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use rbxcloud::rbx::{
error::Error,
types::RobloxUserId,
v2::{user::GetUserResponse, Client},
};

async fn user_info() -> Result<GetUserResponse, Error> {
// Inputs:
let api_key = "MY_API_KEY";
let user_id = 308165;

let client = Client::new(api_key);
let user_client = client.user();

user_client.get_user(RobloxUserId(user_id)).await
}

#[tokio::main]
async fn main() {
let user_info_res = user_info().await;

match user_info_res {
Ok(user_info) => {
println!("{:?}", user_info);
}
Err(e) => {
eprintln!("{e:?}");
}
}
}
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ nav:
- Subscription: cli/cli-subscription.md
- Notification: cli/cli-notification.md
- Universe: cli/cli-universe.md
- User: cli/cli-user.md
- Rust Lib:
- Install: lib/lib-install.md

Expand Down
6 changes: 6 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ mod notification_cli;
mod ordered_datastore_cli;
mod subscription_cli;
mod universe_cli;
mod user_cli;

use clap::{Parser, Subcommand};
use universe_cli::Universe;
use user_cli::User;

use self::{
assets_cli::Assets, datastore_cli::DataStore, experience_cli::Experience, group_cli::Group,
Expand Down Expand Up @@ -52,6 +54,9 @@ pub enum Command {

/// Access the Roblox Universe API
Universe(Universe),

/// Access the Roblox User API
User(User),
}

impl Cli {
Expand All @@ -66,6 +71,7 @@ impl Cli {
Command::Subscription(command) => command.run().await,
Command::Notification(command) => command.run().await,
Command::Universe(command) => command.run().await,
Command::User(command) => command.run().await,
}
}
}
112 changes: 112 additions & 0 deletions src/cli/user_cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use clap::{Args, Subcommand};
use rbxcloud::rbx::{
types::RobloxUserId,
v2::{
user::{UserThumbnailFormat, UserThumbnailShape, UserThumbnailSize},
Client,
},
};

#[derive(Debug, Subcommand)]
pub enum UserCommands {
/// Get user information
Get {
/// User ID
#[clap(short, long, value_parser)]
user_id: u64,

/// Pretty-print the JSON response
#[clap(short, long, value_parser, default_value_t = false)]
pretty: bool,

/// Roblox Open Cloud API Key
#[clap(short, long, value_parser, env = "RBXCLOUD_API_KEY")]
api_key: String,
},

/// Generate user thumbnail
Thumbnail {
/// User ID
#[clap(short, long, value_parser)]
user_id: u64,

/// Thumbnail size
#[clap(short, long, value_enum)]
size: Option<UserThumbnailSize>,

/// Thumbnail format
#[clap(short, long, value_enum)]
format: Option<UserThumbnailFormat>,

/// Thumbnail shape
#[clap(short = 'S', long, value_enum)]
shape: Option<UserThumbnailShape>,

/// Pretty-print the JSON response
#[clap(short, long, value_parser, default_value_t = false)]
pretty: bool,

/// Roblox Open Cloud API Key
#[clap(short, long, value_parser, env = "RBXCLOUD_API_KEY")]
api_key: String,
},
}

#[derive(Debug, Args)]
pub struct User {
#[clap(subcommand)]
command: UserCommands,
}

impl User {
pub async fn run(self) -> anyhow::Result<Option<String>> {
match self.command {
UserCommands::Get {
user_id,
pretty,
api_key,
} => {
let client = Client::new(&api_key);
let user_client = client.user();
let res = user_client.get_user(RobloxUserId(user_id)).await;
match res {
Ok(universe_info) => {
let r = if pretty {
serde_json::to_string_pretty(&universe_info)?
} else {
serde_json::to_string(&universe_info)?
};
Ok(Some(r))
}
Err(err) => Err(anyhow::anyhow!(err)),
}
}

UserCommands::Thumbnail {
user_id,
size,
format,
shape,
pretty,
api_key,
} => {
let client = Client::new(&api_key);
let user_client = client.user();
let res = user_client
.generate_thumbnail(RobloxUserId(user_id), size, format, shape)
.await;
match res {
Ok(universe_info) => {
let r = if pretty {
serde_json::to_string_pretty(&universe_info)?
} else {
serde_json::to_string(&universe_info)?
};
Ok(Some(r))
}
Err(err) => Err(anyhow::anyhow!(err)),
}
}
}
}
}
42 changes: 42 additions & 0 deletions src/rbx/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use universe::{
GetUniverseParams, RestartUniverseServersParams, UniverseInfo, UpdateUniverseInfo,
UpdateUniverseParams,
};
use user::{
GenerateUserThumbnailOperationResponse, GenerateUserThumbnailParams, GetUserParams,
GetUserResponse, UserThumbnailFormat, UserThumbnailShape, UserThumbnailSize,
};

use self::{
group::{
Expand All @@ -21,6 +25,7 @@ pub(crate) mod http_err;
pub mod notification;
pub mod subscription;
pub mod universe;
pub mod user;

use crate::rbx::error::Error;

Expand Down Expand Up @@ -58,6 +63,10 @@ pub struct UniverseClient {
pub universe_id: UniverseId,
}

pub struct UserClient {
pub api_key: String,
}

impl GroupClient {
pub async fn get_info(&self) -> Result<GetGroupResponse, Error> {
group::get_group(&GetGroupParams {
Expand Down Expand Up @@ -172,6 +181,33 @@ impl UniverseClient {
}
}

impl UserClient {
pub async fn get_user(&self, user_id: RobloxUserId) -> Result<GetUserResponse, Error> {
user::get_user(&GetUserParams {
api_key: self.api_key.clone(),
user_id,
})
.await
}

pub async fn generate_thumbnail(
&self,
user_id: RobloxUserId,
size: Option<UserThumbnailSize>,
format: Option<UserThumbnailFormat>,
shape: Option<UserThumbnailShape>,
) -> Result<GenerateUserThumbnailOperationResponse, Error> {
user::generate_thumbnail(&GenerateUserThumbnailParams {
api_key: self.api_key.clone(),
user_id,
size,
shape,
format,
})
.await
}
}

impl Client {
pub fn new(api_key: &str) -> Client {
Client {
Expand Down Expand Up @@ -205,4 +241,10 @@ impl Client {
universe_id,
}
}

pub fn user(&self) -> UserClient {
UserClient {
api_key: self.api_key.clone(),
}
}
}
Loading

0 comments on commit cfd17da

Please sign in to comment.