Browse Source

Implement Robocraft factory bot info get

tags/v0.5.1
NGnius 3 years ago
parent
commit
0f85739955
6 changed files with 97 additions and 13 deletions
  1. +0
    -3
      src/lib.rs
  2. +19
    -3
      src/robocraft/factory.rs
  3. +60
    -2
      src/robocraft/factory_json.rs
  4. +3
    -3
      src/robocraft/factory_request_builder.rs
  5. +1
    -1
      src/robocraft/mod.rs
  6. +14
    -1
      tests/robocraft_factory.rs

+ 0
- 3
src/lib.rs View File

@@ -1,5 +1,2 @@
pub mod cardlife;
pub mod robocraft;

#[cfg(test)]
mod tests {}

+ 19
- 3
src/robocraft/factory.rs View File

@@ -1,7 +1,7 @@
use reqwest::{Client, Error};
use url::{Url};

use crate::robocraft::{ITokenProvider, DefaultTokenProvider, FactoryInfo, FactorySearchBuilder};
use crate::robocraft::{ITokenProvider, DefaultTokenProvider, FactoryInfo, FactorySearchBuilder, RoboShopItemsInfo, FactoryRobotGetInfo};
use crate::robocraft::factory_json::ListPayload;

const FACTORY_DOMAIN: &str = "https://factory.robocraftgame.com/";
@@ -26,7 +26,7 @@ impl FactoryAPI {
}
}
pub async fn list(&self) -> Result<FactoryInfo, Error> {
pub async fn list(&self) -> Result<FactoryInfo<RoboShopItemsInfo>, Error> {
let url = Url::parse(FACTORY_DOMAIN)
.unwrap()
.join("/api/roboShopItems/list")
@@ -39,7 +39,7 @@ impl FactoryAPI {
}
let result = request_builder.send().await;
if let Ok(response) = result {
return response.json::<FactoryInfo>().await;
return response.json::<FactoryInfo<RoboShopItemsInfo>>().await;
}
Err(result.err().unwrap())
}
@@ -56,4 +56,20 @@ impl FactoryAPI {
let request_builder = self.client.post(url);
FactorySearchBuilder::new(request_builder, token_opt)
}
pub async fn get(&self, item_id: usize) -> Result<FactoryInfo<FactoryRobotGetInfo>, Error> {
let url = Url::parse(FACTORY_DOMAIN)
.unwrap()
.join(&format!("/api/roboShopItems/get/{}", item_id))
.unwrap();
let mut request_builder = self.client.get(url);
if let Ok(token) = self.token.token() {
request_builder = request_builder.header("Authorization", "Web ".to_owned() + &token);
}
let result = request_builder.send().await;
if let Ok(response) = result {
return response.json::<FactoryInfo<FactoryRobotGetInfo>>().await;
}
Err(result.err().unwrap())
}
}

+ 60
- 2
src/robocraft/factory_json.rs View File

@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};

// list endpoint

#[derive(Deserialize, Serialize, Clone)]
pub(crate) struct ListPayload {
#[serde(rename = "page")]
@@ -81,9 +83,9 @@ impl ListPayload {
}

#[derive(Deserialize, Serialize, Clone)]
pub struct FactoryInfo {
pub struct FactoryInfo<T> {
#[serde(rename = "response")]
pub response: RoboShopItemsInfo,
pub response: T,
#[serde(rename = "statusCode")]
pub status_code: usize,
}
@@ -143,3 +145,59 @@ impl std::string::ToString for FactoryRobotListInfo {
format!("{} by {} ({})", &self.item_name, &self.added_by_display_name, &self.item_id)
}
}

// get/<item_id> endpoint

#[derive(Deserialize, Serialize, Clone)]
pub struct FactoryRobotGetInfo {
#[serde(rename = "id")]
pub item_id: usize,
#[serde(rename = "name")]
pub item_name: String,
#[serde(rename = "description")]
pub item_description: String,
#[serde(rename = "thumbnail")]
pub thumbnail: String, // url
#[serde(rename = "addedBy")]
pub added_by: String,
#[serde(rename = "addedByDisplayName")]
pub added_by_display_name: String,
#[serde(rename = "addedDate")]
pub added_date: String, // ISO date
#[serde(rename = "expiryDate")]
pub expiry_date: String, // ISO date
#[serde(rename = "cpu")]
pub cpu: usize,
#[serde(rename = "totalRobotRanking")]
pub total_robot_ranking: usize,
#[serde(rename = "rentCount")]
pub rent_count: usize,
#[serde(rename = "buyCount")]
pub buy_count: usize,
#[serde(rename = "buyable")]
pub buyable: bool,
#[serde(rename = "removedDate")]
pub removed_date: Option<String>,
#[serde(rename = "banDate")]
pub ban_date: Option<String>,
#[serde(rename = "featured")]
pub featured: bool,
#[serde(rename = "bannerMessage")]
pub banner_message: Option<String>,
#[serde(rename = "combatRating")]
pub combat_rating: f32,
#[serde(rename = "cosmeticRating")]
pub cosmetic_rating: f32,
#[serde(rename = "cubeData")]
pub cube_data: String,
#[serde(rename = "colourData")]
pub colour_data: String,
#[serde(rename = "cubeAmounts")]
pub cube_amounts: String, // JSON as str
}

impl std::string::ToString for FactoryRobotGetInfo {
fn to_string(&self) -> String {
format!("{} by {} ({})", &self.item_name, &self.added_by_display_name, &self.item_id)
}
}

+ 3
- 3
src/robocraft/factory_request_builder.rs View File

@@ -1,6 +1,6 @@
use reqwest::{RequestBuilder, Error};

use crate::robocraft::{FactoryInfo};
use crate::robocraft::{FactoryInfo, RoboShopItemsInfo};
use crate::robocraft::factory_json::ListPayload;

pub enum FactoryOrderType {
@@ -156,14 +156,14 @@ impl FactorySearchBuilder {
self
}
pub async fn send(mut self) -> Result<FactoryInfo, Error> {
pub async fn send(mut self) -> Result<FactoryInfo<RoboShopItemsInfo>, Error> {
self.reqwest_builder = self.reqwest_builder.json(&self.payload);
if let Some(token) = self.token.clone() {
self.reqwest_builder = self.reqwest_builder.header("Authorization", "Web ".to_owned() + &token);
}
let result = self.reqwest_builder.send().await;
if let Ok(response) = result {
return response.json::<FactoryInfo>().await;
return response.json::<FactoryInfo<RoboShopItemsInfo>>().await;
}
Err(result.err().unwrap())
}


+ 1
- 1
src/robocraft/mod.rs View File

@@ -2,7 +2,7 @@ mod factory;
mod factory_json;
mod factory_request_builder;
pub use self::factory::{FactoryAPI};
pub use self::factory_json::{FactoryInfo, FactoryRobotListInfo, RoboShopItemsInfo};
pub use self::factory_json::{FactoryInfo, FactoryRobotListInfo, RoboShopItemsInfo, FactoryRobotGetInfo};
pub use self::factory_request_builder::{FactorySearchBuilder, FactoryMovementType, FactoryOrderType, FactoryWeaponType, FactoryTextSearchType};

mod auth;


+ 14
- 1
tests/robocraft_factory.rs View File

@@ -28,7 +28,7 @@ fn builder() -> robocraft::FactorySearchBuilder {
robocraft::FactoryAPI::new().list_builder()
}

fn assert_factory_list(robo_info: robocraft::FactoryInfo) -> Result<(), ()> {
fn assert_factory_list(robo_info: robocraft::FactoryInfo<robocraft::RoboShopItemsInfo>) -> Result<(), ()> {
assert_ne!(robo_info.response.roboshop_items.len(), 0);
assert_eq!(robo_info.status_code, 200);
for robot in &robo_info.response.roboshop_items {
@@ -76,3 +76,16 @@ async fn robocraft_factory_player_query() -> Result<(), ()> {
assert!(result.is_ok());
assert_factory_list(result.unwrap())
}

#[tokio::test]
async fn robocraft_factory_robot_query() -> Result<(), ()> {
let api = robocraft::FactoryAPI::new();
let result = api.get(6478345 /* featured robot id*/).await;
assert!(result.is_ok());
let bot_info = result.unwrap();
assert_ne!(bot_info.response.item_name, "");
assert_eq!(bot_info.response.item_id, 6478345);
assert_ne!(bot_info.response.cube_data, "");
assert_ne!(bot_info.response.colour_data, "");
Ok(())
}

Loading…
Cancel
Save