From 3633ecb6bac63f17fabafaceaaaa3ee998e60b73 Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Sun, 31 Dec 2023 15:26:15 -0300 Subject: [PATCH] Update api --- examples/api.rs | 5 +- examples/token.rs | 4 +- src/actix/lib.rs | 19 ++--- src/api/lib.rs | 180 +++++++++++++++++++++++----------------------- 4 files changed, 100 insertions(+), 108 deletions(-) diff --git a/examples/api.rs b/examples/api.rs index 2a5c7f1..eb1b2c3 100644 --- a/examples/api.rs +++ b/examples/api.rs @@ -4,6 +4,7 @@ use tribufu::*; #[tokio::main] async fn main() { - let mut api = TribufuApi::default(); - api.use_anonymous(); + let api = TribufuApi::default(); + let games = api.get_games(Some(1)).await.unwrap(); + println!("{:?}", games); } diff --git a/examples/token.rs b/examples/token.rs index 9f4a223..133240d 100644 --- a/examples/token.rs +++ b/examples/token.rs @@ -4,5 +4,7 @@ use tribufu::*; #[tokio::main] async fn main() { - let mut api = TribufuApi::from_env(); + let api = TribufuApi::with_client_from_env().unwrap_or_default(); + let games = api.get_games(Some(1)).await.unwrap(); + println!("{:?}", games); } diff --git a/src/actix/lib.rs b/src/actix/lib.rs index 2619b3f..a3c5a74 100644 --- a/src/actix/lib.rs +++ b/src/actix/lib.rs @@ -5,31 +5,20 @@ use tribufu_api::TribufuApi; pub trait TribufuApiActixExtension { fn from_actix(req: &HttpRequest) -> Self; - fn use_actix(&mut self, req: &HttpRequest); } impl TribufuApiActixExtension for TribufuApi { fn from_actix(req: &HttpRequest) -> Self { - let mut api = Self::from_env(); - api.use_actix(req); - api - } + let mut api = Self::with_client_from_env().unwrap_or_default(); - fn use_actix(&mut self, req: &HttpRequest) { if let Some(authorization) = req.headers().get("Authorization") { let authorization = authorization.to_str().unwrap(); - if authorization.starts_with("ApiKey ") { - self.use_api_key(authorization[7..].to_string()); - } - - if authorization.starts_with("Basic ") { - self.use_basic(authorization[6..].to_string()); - } - if authorization.starts_with("Bearer ") { - self.use_bearer(authorization[7..].to_string()); + api = Self::with_user(authorization[7..].to_string()); } } + + return api; } } diff --git a/src/api/lib.rs b/src/api/lib.rs index d03a09a..cf6d890 100644 --- a/src/api/lib.rs +++ b/src/api/lib.rs @@ -12,30 +12,47 @@ use tribufu_types::games::Game; use tribufu_types::oauth2::{OAuth2GrantType, OAuth2TokenRequest, OAuth2TokenResponse}; use tribufu_types::users::*; -pub enum CredentialsType { +pub enum Credentials { Anonymous, - ApiKey, - Basic, - Bearer, + ApiKey { + api_key: String, + }, + Client { + client_id: u64, + client_secret: String, + }, +} + +pub enum Token { + ApiKey { + api_key: String, + }, + Basic { + basic_token: String, + }, + Bearer { + access_token: String, + refresh_token: Option, + }, } pub struct TribufuApi { base_url: String, - credentials: Option, - credentials_kind: CredentialsType, + credentials: Credentials, + token: Option, http: Client, } impl Default for TribufuApi { fn default() -> Self { - Self::new(CredentialsType::Anonymous, None) + Self::new(Credentials::Anonymous) } } impl TribufuApi { - const BASE_URL: &'static str = "https://api.tribufu.com"; + const TRIBUFU_API_URL: &'static str = "https://api.tribufu.com"; - pub fn new(credentials_kind: CredentialsType, credentials: Option) -> Self { + pub fn new(credentials: Credentials) -> Self { let http = Client::builder() .user_agent(Self::user_agent()) .default_headers(Self::default_headers()) @@ -43,9 +60,9 @@ impl TribufuApi { .unwrap(); Self { - base_url: Self::BASE_URL.to_string(), + base_url: Self::get_base_url(), credentials, - credentials_kind, + token: None, http, } } @@ -70,114 +87,97 @@ impl TribufuApi { headers } + fn get_base_url() -> String { + if cfg!(debug_assertions) { + return env::var("TRIBUFU_API_URL") + .unwrap_or_else(|_| Self::TRIBUFU_API_URL.to_string()); + } + + Self::TRIBUFU_API_URL.to_string() + } + pub fn with_api_key(api_key: String) -> Self { - let mut api = Self::default(); - api.use_api_key(api_key); - api + Self::new(Credentials::ApiKey { api_key }) } pub fn with_client(client_id: u64, client_secret: String) -> Self { - let mut api = Self::default(); - api.use_client(client_id, client_secret); - api + Self::new(Credentials::Client { + client_id, + client_secret, + }) } - pub fn with_token(token: String) -> Self { - let mut api = Self::default(); - api.use_bearer(token); - api - } - - pub fn from_env() -> Self { - let mut api = Self::default(); - api.use_env(); - api - } - - pub fn use_env(&mut self) { - #[cfg(debug_assertions)] - if let Ok(base_url) = env::var("TRIBUFU_API_URL") { - self.set_base_url(base_url); - } - - if let Ok(token) = env::var("TRIBUFU_TOKEN") { - self.use_bearer(token); + pub fn with_api_key_from_env() -> Option { + if let Ok(api_key) = env::var("TRIBUFU_API_KEY") { + Some(Self::with_api_key(api_key)) + } else { + None } + } + pub fn with_client_from_env() -> Option { let client_id = env::var("TRIBUFU_CLIENT_ID"); let client_secret = env::var("TRIBUFU_CLIENT_SECRET"); if let (Ok(client_id), Ok(client_secret)) = (client_id, client_secret) { - self.use_client(client_id.parse().unwrap(), client_secret); - } - - if let Ok(api_key) = env::var("TRIBUFU_API_KEY") { - self.use_api_key(api_key); + Some(Self::with_client(client_id.parse().unwrap(), client_secret)) + } else { + None } } - pub fn use_anonymous(&mut self) { - self.credentials_kind = CredentialsType::Anonymous; - self.credentials = None; + pub fn set_anonymous(&mut self) { + self.credentials = Credentials::Anonymous; } - pub fn use_api_key(&mut self, api_key: String) { - self.credentials_kind = CredentialsType::ApiKey; - self.credentials = Some(api_key); + pub fn set_api_key(&mut self, api_key: String) { + self.credentials = Credentials::ApiKey { api_key }; } - pub fn use_basic(&mut self, basic_token: String) { - self.credentials_kind = CredentialsType::Basic; - self.credentials = Some(basic_token); + pub fn set_clients(&mut self, client_id: u64, client_secret: String) { + self.credentials = Credentials::Client { + client_id, + client_secret, + }; } - pub fn use_bearer(&mut self, bearer_token: String) { - self.credentials_kind = CredentialsType::Bearer; - self.credentials = Some(bearer_token); + pub fn set_basic_token(&mut self, basic_token: String) { + self.token = Some(Token::Basic { basic_token }); } - pub fn use_client(&mut self, client_id: u64, client_secret: String) { - let credentials_str = format!("{}:{}", client_id, client_secret); - self.use_basic(BASE64.encode(credentials_str.as_bytes())); - } - - fn set_base_url(&mut self, base_url: String) { - self.base_url = base_url; + pub fn set_bearer_token(&mut self, access_token: String, refresh_token: Option) { + self.token = Some(Token::Bearer { + access_token, + refresh_token, + }); } #[inline] fn headers(&self) -> HeaderMap { let mut headers = Self::default_headers(); - match self.credentials_kind { - CredentialsType::ApiKey => { - headers.insert( - AUTHORIZATION, - HeaderValue::from_str(&format!( - "ApiKey {}", - self.credentials.as_ref().unwrap() - )) - .unwrap(), - ); - } - CredentialsType::Basic => { - headers.insert( - AUTHORIZATION, - HeaderValue::from_str(&format!("Basic {}", self.credentials.as_ref().unwrap())) - .unwrap(), - ); - } - CredentialsType::Bearer => { - headers.insert( - AUTHORIZATION, - HeaderValue::from_str(&format!( - "Bearer {}", - self.credentials.as_ref().unwrap() - )) - .unwrap(), - ); - } - _ => {} + match &self.token { + Some(token) => match token { + Token::ApiKey { api_key } => { + headers.insert( + AUTHORIZATION, + HeaderValue::from_str(&format!("ApiKey {}", api_key)).unwrap(), + ); + } + Token::Basic { basic_token } => { + headers.insert( + AUTHORIZATION, + HeaderValue::from_str(&format!("Basic {}", basic_token)).unwrap(), + ); + } + Token::Bearer { access_token, .. } => { + headers.insert( + AUTHORIZATION, + HeaderValue::from_str(&format!("Bearer {}", access_token)).unwrap(), + ); + } + }, + None => {} } headers