From 54b3a527058242d6c0d807f85ada17c2a484cce2 Mon Sep 17 00:00:00 2001 From: xtrm Date: Wed, 31 Jan 2024 00:50:35 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20fix=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xtrm --- .github/workflows/build-debug.yml | 2 +- .github/workflows/build-release.yml | 2 +- {tests => examples}/simple-test.rs | 2 +- src/client.rs | 36 ++++++++++++++++------------- src/lib.rs | 2 -- src/models/token.rs | 13 ++++++++--- 6 files changed, 33 insertions(+), 24 deletions(-) rename {tests => examples}/simple-test.rs (89%) diff --git a/.github/workflows/build-debug.yml b/.github/workflows/build-debug.yml index ca88042..a861c96 100644 --- a/.github/workflows/build-debug.yml +++ b/.github/workflows/build-debug.yml @@ -21,4 +21,4 @@ jobs: - name: Build run: cargo build --verbose - name: Run tests - run: cargo test --verbose \ No newline at end of file + run: cargo test --verbose --tests \ No newline at end of file diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index 5966260..2a49a40 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -21,4 +21,4 @@ jobs: - name: Build run: cargo build --verbose --release - name: Run tests - run: cargo test --verbose --release \ No newline at end of file + run: cargo test --verbose --tests --release \ No newline at end of file diff --git a/tests/simple-test.rs b/examples/simple-test.rs similarity index 89% rename from tests/simple-test.rs rename to examples/simple-test.rs index 75d83e6..bd26af1 100644 --- a/tests/simple-test.rs +++ b/examples/simple-test.rs @@ -1,6 +1,6 @@ use ft_rs::FtClient; -#[tokio::test(flavor = "multi_thread")] +#[tokio::main] async fn main() -> ft_rs::Result<()> { let client = FtClient::from_app( std::env::var("FT_RS_TEST_UID").expect("FT_RS_TEST_UID not set"), diff --git a/src/client.rs b/src/client.rs index e196403..a6395c4 100644 --- a/src/client.rs +++ b/src/client.rs @@ -26,14 +26,15 @@ pub struct FtClient { impl FtClient { /// Creates a new client for a v2 application, providing the application's UID and secret. /// - /// # Example - /// /// ```rust /// use ft_rs::FtClient; /// - /// fn main() { - /// let client = FtClient::from_app("my_uid", "my_super_secret_secret"); - /// } + /// let client = FtClient::from_app("my_uid", "my_super_secret_secret"); + /// ``` + /// + /// # Errors + /// + /// This method will return an error if the reqwest client could not be built, or if the UID or secret are invalid. pub fn from_app, S: Into>( app_uid: U, app_secret: S, @@ -45,18 +46,18 @@ impl FtClient { .user_agent(format!("{}/{}", PKG_NAME, PKG_VERSION)) .connect_timeout(Duration::from_secs(30)) .build(); + if let Err(err) = client { - return Err(FtError::ReqwestBuilderError(err)); + Err(FtError::ReqwestBuilderError(err)) + } else { + Ok(Self { + app_uid, + app_secret, + + client: client.unwrap(), + last_valid_token: None + }) } - let client = client.unwrap(); - - Ok(Self { - app_uid, - app_secret, - - client, - last_valid_token: None - }) } /// Fetches a new access token from the API. @@ -67,7 +68,7 @@ impl FtClient { /// /// # Example /// - /// ```rust + /// ```no_run /// use ft_rs::FtClient; /// /// #[tokio::main] @@ -107,6 +108,9 @@ impl FtClient { } } + /// Ensures that the last valid token is still valid, and fetches a new one if it is not. + /// + /// This method is called automatically by the API Client when making a request, so there is no need to call it manually. pub async fn ensure_valid_token(&mut self) -> Result<()> { if let Some(token) = &self.last_valid_token { if token.is_expired() { diff --git a/src/lib.rs b/src/lib.rs index 381f95d..e52e55f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,4 @@ #![deny(clippy::all)] -#![deny(clippy::pedantic)] -#![deny(clippy::nursery)] #![deny(clippy::cargo)] //#![deny(missing_docs)] diff --git a/src/models/token.rs b/src/models/token.rs index 3f3be59..0782a3c 100644 --- a/src/models/token.rs +++ b/src/models/token.rs @@ -4,13 +4,20 @@ use serde::{Deserialize, Serialize}; pub struct AccessToken { access_token: String, token_type: String, - expires_in: u32, + expires_in: u64, scope: String, - created_at: u32 + created_at: u64 } impl AccessToken { + /// Checks if the token is expired. + /// + /// This method will return true if the token is expired, and false otherwise. + /// + /// # Panics + /// + /// This method will panic if the system's time is not set correctly. pub fn is_expired(&self) -> bool { - self.created_at + self.expires_in <= (chrono::Utc::now().timestamp() + 5) as u32 + self.created_at + self.expires_in <= (chrono::Utc::now().timestamp() + 5).try_into().unwrap() } } \ No newline at end of file