From 3e6bfa8c3488a46427cbfefce0dec841f6cba445 Mon Sep 17 00:00:00 2001 From: TotallyNot <44345987+TotallyNot@users.noreply.github.com> Date: Mon, 19 Sep 2022 01:36:19 +0200 Subject: [PATCH] add revivability --- torn-api/Cargo.toml | 2 +- torn-api/src/de_util.rs | 13 +++++++++++++ torn-api/src/user.rs | 3 +++ torn-key-pool/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/torn-api/Cargo.toml b/torn-api/Cargo.toml index 59c5311..8c0efc8 100644 --- a/torn-api/Cargo.toml +++ b/torn-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "torn-api" -version = "0.5.2" +version = "0.5.3" edition = "2021" authors = ["Pyrit [2111649]"] license = "MIT" diff --git a/torn-api/src/de_util.rs b/torn-api/src/de_util.rs index 01802ec..cf2a2a5 100644 --- a/torn-api/src/de_util.rs +++ b/torn-api/src/de_util.rs @@ -39,3 +39,16 @@ where Ok(Some(DateTime::from_utc(naive, Utc))) } } + +pub fn int_is_bool<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let i = i64::deserialize(deserializer)?; + + match i { + 0 => Ok(false), + 1 => Ok(true), + x => Err(Error::invalid_value(Unexpected::Signed(x), &"0 or 1")), + } +} diff --git a/torn-api/src/user.rs b/torn-api/src/user.rs index a36582e..bbaadc1 100644 --- a/torn-api/src/user.rs +++ b/torn-api/src/user.rs @@ -340,6 +340,9 @@ pub struct Profile { #[serde(deserialize_with = "deserialize_comp")] pub competition: Option, + + #[serde(deserialize_with = "de_util::int_is_bool")] + pub revivable: bool, } #[derive(Debug, Clone, Deserialize)] diff --git a/torn-key-pool/src/lib.rs b/torn-key-pool/src/lib.rs index fff2ec2..9c81e13 100644 --- a/torn-key-pool/src/lib.rs +++ b/torn-key-pool/src/lib.rs @@ -78,3 +78,45 @@ where } } } + +#[cfg(all(test, feature = "postgres"))] +mod test { + use std::sync::{Arc, Once}; + + use sqlx::Row; + use tokio::test; + + use super::*; + + static INIT: Once = Once::new(); + + pub(crate) async fn setup() -> postgres::PgKeyPoolStorage { + INIT.call_once(|| { + dotenv::dotenv().ok(); + }); + + let pool = sqlx::PgPool::connect(&std::env::var("DATABASE_URL").unwrap()) + .await + .unwrap(); + + sqlx::query("update api_keys set uses=0") + .execute(&pool) + .await + .unwrap(); + + postgres::PgKeyPoolStorage::new(pool, 50) + } + + #[test] + async fn key_pool_bulk() { + let storage = setup().await; + + if let Err(e) = storage.initialise().await { + panic!("Initialising key storage failed: {:?}", e); + } + + let pool = send::KeyPool::new(reqwest::Client::default(), storage); + + pool.torn_api(KeyDomain::Public).users([1], |b| b).await; + } +}