Split crates

This commit is contained in:
Guilherme Werner
2023-12-27 12:40:58 -03:00
parent 5d5bb4db97
commit 9cd88707ff
13 changed files with 133 additions and 59 deletions

View File

@ -11,22 +11,23 @@ publish = true
exclude = [".github/", ".vscode/", ".editorconfig", ".gitattributes"] exclude = [".github/", ".vscode/", ".editorconfig", ".gitattributes"]
[workspace]
resolver = "2"
members = ["src/*"]
[lib] [lib]
name = "tribufu" name = "tribufu"
crate-type = ["rlib"] crate-type = ["rlib"]
path = "src/lib.rs" path = "src/lib.rs"
[features]
actix = ["tribufu-actix"]
[dependencies] [dependencies]
base64 = "0.21.5" tribufu-api = { path = "./src/api" }
mintaka-error = { version = "0.0.1" } tribufu-constants = { path = "./src/constants" }
alnilam-consts = { version = "0.0.4" } tribufu-types = { path = "./src/types" }
anyhow = "1.0.75" tribufu-actix = { path = "./src/actix", optional = true }
chrono = { version = "0.4.22", features = ["serde", "rustc-serialize"] }
derive_more = "0.99.17"
reqwest = { version = "0.11.18", features = ["json", "stream"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
serde_with = "3.4.0"
[dev-dependencies] [dev-dependencies]
dotenv = "0.15.0" dotenv = "0.15.0"

View File

@ -16,3 +16,5 @@ path = "lib.rs"
[dependencies] [dependencies]
actix-web = { version = "4", features = ["rustls"] } actix-web = { version = "4", features = ["rustls"] }
mintaka-error = { version = "0.0.1" }
tribufu-api = { path = "../api" }

View File

@ -1,29 +1,35 @@
// Copyright (c) Tribufu. All Rights Reserved. // Copyright (c) Tribufu. All Rights Reserved.
/*
use actix_web::HttpRequest; use actix_web::HttpRequest;
use tribufu_api::TribufuApi;
pub trait TribufuApiActixExtension { pub trait TribufuApiActixExtension {
fn use_anonymous(req: &HttpRequest) -> Self; fn from_actix(req: &HttpRequest) -> Self;
fn use_actix(&mut self, req: &HttpRequest);
} }
impl TribufuApi { impl TribufuApiActixExtension for TribufuApi {
pub fn from_actix(req: &HttpRequest) -> Self { fn from_actix(req: &HttpRequest) -> Self {
let mut api = Self::default(); let mut api = Self::from_env();
api.use_actix(req);
if let Some(api_key) = req.headers().get("X-Tribufu-Api-Key") { api
api.use_api_key(api_key.to_str().unwrap().to_string());
} }
fn use_actix(&mut self, req: &HttpRequest) {
if let Some(authorization) = req.headers().get("Authorization") { if let Some(authorization) = req.headers().get("Authorization") {
let authorization = authorization.to_str().unwrap(); let authorization = authorization.to_str().unwrap();
if authorization.starts_with("Bearer ") { if authorization.starts_with("ApiKey ") {
api.use_token(authorization[7..].to_string()); self.use_api_key(authorization[7..].to_string());
}
} }
api if authorization.starts_with("Basic ") {
self.use_basic(authorization[6..].to_string());
}
if authorization.starts_with("Bearer ") {
self.use_bearer(authorization[7..].to_string());
}
}
} }
} }
*/

29
src/api/Cargo.toml Normal file
View File

@ -0,0 +1,29 @@
[package]
name = "tribufu-api"
version = "0.0.4"
description = "Tribufu API"
repository = "https://github.com/Tribufu/TribufuRust"
authors = ["Tribufu <contact@tribufu.com>"]
license = "Apache-2.0"
readme = "README.md"
edition = "2021"
publish = true
[lib]
name = "tribufu_api"
crate-type = ["rlib"]
path = "lib.rs"
[dependencies]
tribufu-constants = { path = "../constants" }
tribufu-types = { path = "../types" }
base64 = "0.21.5"
mintaka-error = { version = "0.0.1" }
alnilam-consts = { version = "0.0.4" }
anyhow = "1.0.75"
chrono = { version = "0.4.22", features = ["serde", "rustc-serialize"] }
derive_more = "0.99.17"
reqwest = { version = "0.11.18", features = ["json", "stream"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
serde_with = "3.4.0"

View File

@ -1,17 +1,16 @@
// Copyright (c) Tribufu. All Rights Reserved. // Copyright (c) Tribufu. All Rights Reserved.
use crate::games::Game;
use crate::oauth2::{OAuth2GrantType, OAuth2TokenRequest, OAuth2TokenResponse};
use crate::users::*;
use crate::VERSION;
use alnilam_consts::TARGET_TRIPLE; use alnilam_consts::TARGET_TRIPLE;
use base64::engine::general_purpose::STANDARD as BASE64; use base64::engine::general_purpose::STANDARD as BASE64;
use base64::Engine as _; use base64::Engine as _;
use chrono::{NaiveDateTime, Utc};
use mintaka_error::{Error, Result}; use mintaka_error::{Error, Result};
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION}; use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
use reqwest::Client; use reqwest::Client;
use std::env; use std::env;
use tribufu_constants::VERSION;
use tribufu_types::games::Game;
use tribufu_types::oauth2::{OAuth2GrantType, OAuth2TokenRequest, OAuth2TokenResponse};
use tribufu_types::users::*;
pub enum CredentialsType { pub enum CredentialsType {
Anonymous, Anonymous,
@ -24,8 +23,6 @@ pub struct TribufuApi {
base_url: String, base_url: String,
credentials: Option<String>, credentials: Option<String>,
credentials_kind: CredentialsType, credentials_kind: CredentialsType,
credentials_refreshed_at: Option<NaiveDateTime>,
credentials_expires_at: Option<NaiveDateTime>,
http: Client, http: Client,
} }
@ -49,8 +46,6 @@ impl TribufuApi {
base_url: Self::BASE_URL.to_string(), base_url: Self::BASE_URL.to_string(),
credentials, credentials,
credentials_kind, credentials_kind,
credentials_refreshed_at: None,
credentials_expires_at: None,
http, http,
} }
} }
@ -89,7 +84,7 @@ impl TribufuApi {
pub fn with_token(token: String) -> Self { pub fn with_token(token: String) -> Self {
let mut api = Self::default(); let mut api = Self::default();
api.use_token(token); api.use_bearer(token);
api api
} }
@ -105,8 +100,8 @@ impl TribufuApi {
self.set_base_url(base_url); self.set_base_url(base_url);
} }
if let Ok(api_key) = env::var("TRIBUFU_API_KEY") { if let Ok(token) = env::var("TRIBUFU_TOKEN") {
self.use_api_key(api_key); self.use_bearer(token);
} }
let client_id = env::var("TRIBUFU_CLIENT_ID"); let client_id = env::var("TRIBUFU_CLIENT_ID");
@ -116,8 +111,8 @@ impl TribufuApi {
self.use_client(client_id.parse().unwrap(), client_secret); self.use_client(client_id.parse().unwrap(), client_secret);
} }
if let Ok(token) = env::var("TRIBUFU_TOKEN") { if let Ok(api_key) = env::var("TRIBUFU_API_KEY") {
self.use_token(token); self.use_api_key(api_key);
} }
} }
@ -131,15 +126,19 @@ impl TribufuApi {
self.credentials = Some(api_key); self.credentials = Some(api_key);
} }
pub fn use_client(&mut self, client_id: u64, client_secret: String) { pub fn use_basic(&mut self, basic_token: String) {
let credentials_str = format!("{}:{}", client_id, client_secret);
self.credentials_kind = CredentialsType::Basic; self.credentials_kind = CredentialsType::Basic;
self.credentials = Some(BASE64.encode(credentials_str.as_bytes())); self.credentials = Some(basic_token);
} }
pub fn use_token(&mut self, token: String) { pub fn use_bearer(&mut self, bearer_token: String) {
self.credentials_kind = CredentialsType::Bearer; self.credentials_kind = CredentialsType::Bearer;
self.credentials = Some(token); self.credentials = Some(bearer_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) { fn set_base_url(&mut self, base_url: String) {
@ -288,7 +287,7 @@ impl TribufuApi {
} }
async fn get_oauth_token( async fn get_oauth_token(
&mut self, &self,
grant_type: OAuth2GrantType, grant_type: OAuth2GrantType,
grant_value: Option<String>, grant_value: Option<String>,
client_id: u64, client_id: u64,
@ -365,11 +364,7 @@ impl TribufuApi {
))); )));
} }
let response_body: OAuth2TokenResponse = response.json().await?; Ok(response.json().await?)
self.use_token(response_body.clone().access_token);
Ok(response_body)
} }
pub async fn get_user_info(&self) -> Result<User> { pub async fn get_user_info(&self) -> Result<User> {

17
src/constants/Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "tribufu-constants"
version = "0.0.4"
description = "Tribufu Constants"
repository = "https://github.com/Tribufu/TribufuRust"
authors = ["Tribufu <contact@tribufu.com>"]
license = "Apache-2.0"
readme = "README.md"
edition = "2021"
publish = true
[lib]
name = "tribufu_constants"
crate-type = ["rlib"]
path = "lib.rs"
[dependencies]

3
src/constants/lib.rs Normal file
View File

@ -0,0 +1,3 @@
// Copyright (c) Tribufu. All Rights Reserved.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@ -1,12 +1,5 @@
// Copyright (c) Tribufu. All Rights Reserved. // Copyright (c) Tribufu. All Rights Reserved.
#![allow(dead_code)] pub use tribufu_api::*;
pub use tribufu_constants::VERSION;
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub use tribufu_types as types;
pub mod api;
pub mod games;
pub mod oauth2;
pub mod users;
pub use api::*;

23
src/types/Cargo.toml Normal file
View File

@ -0,0 +1,23 @@
[package]
name = "tribufu-types"
version = "0.0.4"
description = "Tribufu Types"
repository = "https://github.com/Tribufu/TribufuRust"
authors = ["Tribufu <contact@tribufu.com>"]
license = "Apache-2.0"
readme = "README.md"
edition = "2021"
publish = true
[lib]
name = "tribufu_types"
crate-type = ["rlib"]
path = "lib.rs"
[dependencies]
chrono = { version = "0.4.22", features = ["serde", "rustc-serialize"] }
derive_more = "0.99.17"
mintaka-error = { version = "0.0.1" }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
serde_with = "3.4.0"

5
src/types/lib.rs Normal file
View File

@ -0,0 +1,5 @@
// Copyright (c) Tribufu. All Rights Reserved.
pub mod games;
pub mod oauth2;
pub mod users;

View File

@ -155,7 +155,7 @@ pub struct OAuth2IntrospectionResponse {
} }
impl OAuth2IntrospectionResponse { impl OAuth2IntrospectionResponse {
fn inative() -> Self { pub fn inative() -> Self {
Self { Self {
active: false, active: false,
client_id: None, client_id: None,