|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | + |
| 3 | +use crate::rbx::{error::Error, types::RobloxUserId, util::QueryString}; |
| 4 | + |
| 5 | +use super::http_err::handle_http_err; |
| 6 | + |
| 7 | +#[derive(Deserialize, Serialize, Debug)] |
| 8 | +#[serde(rename_all = "camelCase")] |
| 9 | +pub struct ListInventoryItemsParams { |
| 10 | + pub api_key: String, |
| 11 | + pub user_id: RobloxUserId, |
| 12 | + pub max_page_size: Option<u32>, |
| 13 | + pub page_token: Option<String>, |
| 14 | + pub filter: Option<String>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Deserialize, Serialize, Debug)] |
| 18 | +#[serde(rename_all = "camelCase")] |
| 19 | +pub struct InventoryItems { |
| 20 | + inventory_items: Vec<InventoryItem>, |
| 21 | + next_page_token: String, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Deserialize, Serialize, Debug)] |
| 25 | +#[serde(rename_all = "camelCase")] |
| 26 | +pub struct InventoryItem { |
| 27 | + path: String, |
| 28 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 29 | + asset_details: Option<InventoryItemAssetDetails>, |
| 30 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 31 | + badge_details: Option<InventoryItemBadgeDetails>, |
| 32 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 33 | + game_pass_details: Option<InventoryItemGamePassDetails>, |
| 34 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 35 | + private_server_details: Option<InventoryItemPrivateServerDetails>, |
| 36 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 37 | + add_time: Option<String>, |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Deserialize, Serialize, Debug)] |
| 41 | +#[serde(rename_all = "camelCase")] |
| 42 | +pub struct InventoryItemAssetDetails { |
| 43 | + asset_id: String, |
| 44 | + instance_id: String, |
| 45 | + inventory_item_asset_type: InventoryItemAssetType, |
| 46 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 47 | + collectible_details: Option<InventoryItemCollectibleDetails>, |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Deserialize, Serialize, Debug)] |
| 51 | +#[serde(rename_all = "camelCase")] |
| 52 | +pub struct InventoryItemBadgeDetails { |
| 53 | + badge_id: String, |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Deserialize, Serialize, Debug)] |
| 57 | +#[serde(rename_all = "camelCase")] |
| 58 | +pub struct InventoryItemGamePassDetails { |
| 59 | + game_pass_id: String, |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Deserialize, Serialize, Debug)] |
| 63 | +#[serde(rename_all = "camelCase")] |
| 64 | +pub struct InventoryItemPrivateServerDetails { |
| 65 | + private_server_id: String, |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Deserialize, Serialize, Debug)] |
| 69 | +#[serde(rename_all = "camelCase")] |
| 70 | +pub struct InventoryItemCollectibleDetails { |
| 71 | + item_id: String, |
| 72 | + instance_id: String, |
| 73 | + instance_state: InventoryItemInstanceState, |
| 74 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 75 | + serial_number: Option<u64>, |
| 76 | +} |
| 77 | + |
| 78 | +#[derive(Deserialize, Serialize, Debug)] |
| 79 | +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] |
| 80 | +pub enum InventoryItemInstanceState { |
| 81 | + CollectibleItemInstanceStateUnspecified, |
| 82 | + Available, |
| 83 | + Hold, |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Deserialize, Serialize, Debug)] |
| 87 | +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] |
| 88 | +pub enum InventoryItemAssetType { |
| 89 | + ClassicTshirt, |
| 90 | + Audio, |
| 91 | + Hat, |
| 92 | + Model, |
| 93 | + ClassicShirt, |
| 94 | + ClassicPants, |
| 95 | + Decal, |
| 96 | + ClassicHead, |
| 97 | + Face, |
| 98 | + Gear, |
| 99 | + Animation, |
| 100 | + Torso, |
| 101 | + RightArm, |
| 102 | + LeftArm, |
| 103 | + LeftLeg, |
| 104 | + RightLeg, |
| 105 | + Package, |
| 106 | + Plugin, |
| 107 | + MeshPart, |
| 108 | + HairAccessory, |
| 109 | + FaceAccessory, |
| 110 | + NeckAccessory, |
| 111 | + ShoulderAccessory, |
| 112 | + FrontAccessory, |
| 113 | + BackAccessory, |
| 114 | + WaistAccessory, |
| 115 | + ClimbAnimation, |
| 116 | + DeathAnimation, |
| 117 | + FallAnimation, |
| 118 | + IdleAnimation, |
| 119 | + JumpAnimation, |
| 120 | + RunAnimation, |
| 121 | + SwimAnimation, |
| 122 | + WalkAnimation, |
| 123 | + PoseAnimation, |
| 124 | + EmoteAnimation, |
| 125 | + Video, |
| 126 | + TshirtAccessory, |
| 127 | + ShirtAccessory, |
| 128 | + PantsAccessory, |
| 129 | + JacketAccessory, |
| 130 | + SweaterAccessory, |
| 131 | + ShortsAccessory, |
| 132 | + LeftShoeAccessory, |
| 133 | + RightShoeAccessory, |
| 134 | + DressSkirtAccessory, |
| 135 | + EyebrowAccessory, |
| 136 | + EyelashAccessory, |
| 137 | + MoodAnimation, |
| 138 | + DynamicHead, |
| 139 | + CreatedPlace, |
| 140 | + PurchasedPlace, |
| 141 | +} |
| 142 | + |
| 143 | +pub async fn list_inventory_items( |
| 144 | + params: &ListInventoryItemsParams, |
| 145 | +) -> Result<InventoryItems, Error> { |
| 146 | + let client = reqwest::Client::new(); |
| 147 | + |
| 148 | + let url = format!( |
| 149 | + "https://apis.roblox.com/cloud/v2/users/{userId}/inventory-items", |
| 150 | + userId = params.user_id, |
| 151 | + ); |
| 152 | + |
| 153 | + let mut query: QueryString = vec![]; |
| 154 | + if let Some(max_page_size) = params.max_page_size { |
| 155 | + query.push(("maxPageSize", format!("{max_page_size}"))); |
| 156 | + } |
| 157 | + if let Some(page_token) = ¶ms.page_token { |
| 158 | + query.push(("pageToken", page_token.clone())); |
| 159 | + } |
| 160 | + if let Some(filter) = ¶ms.filter { |
| 161 | + query.push(("filter", filter.clone())); |
| 162 | + } |
| 163 | + |
| 164 | + let res = client |
| 165 | + .get(url) |
| 166 | + .header("x-api-key", ¶ms.api_key) |
| 167 | + .query(&query) |
| 168 | + .send() |
| 169 | + .await?; |
| 170 | + |
| 171 | + let status = res.status(); |
| 172 | + |
| 173 | + if !status.is_success() { |
| 174 | + let code = status.as_u16(); |
| 175 | + return handle_http_err(code); |
| 176 | + } |
| 177 | + |
| 178 | + let body = res.json::<InventoryItems>().await?; |
| 179 | + Ok(body) |
| 180 | +} |
0 commit comments