mirror of
https://github.com/tribufu/sdk-rust
synced 2025-06-15 18:54:19 +00:00
Update api
This commit is contained in:
@ -4,6 +4,7 @@ use tribufu::*;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut api = TribufuApi::default();
|
let api = TribufuApi::default();
|
||||||
api.use_anonymous();
|
let games = api.get_games(Some(1)).await.unwrap();
|
||||||
|
println!("{:?}", games);
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,7 @@ use tribufu::*;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn 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);
|
||||||
}
|
}
|
||||||
|
@ -5,31 +5,20 @@ use tribufu_api::TribufuApi;
|
|||||||
|
|
||||||
pub trait TribufuApiActixExtension {
|
pub trait TribufuApiActixExtension {
|
||||||
fn from_actix(req: &HttpRequest) -> Self;
|
fn from_actix(req: &HttpRequest) -> Self;
|
||||||
fn use_actix(&mut self, req: &HttpRequest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TribufuApiActixExtension for TribufuApi {
|
impl TribufuApiActixExtension for TribufuApi {
|
||||||
fn from_actix(req: &HttpRequest) -> Self {
|
fn from_actix(req: &HttpRequest) -> Self {
|
||||||
let mut api = Self::from_env();
|
let mut api = Self::with_client_from_env().unwrap_or_default();
|
||||||
api.use_actix(req);
|
|
||||||
api
|
|
||||||
}
|
|
||||||
|
|
||||||
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("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 ") {
|
if authorization.starts_with("Bearer ") {
|
||||||
self.use_bearer(authorization[7..].to_string());
|
api = Self::with_user(authorization[7..].to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return api;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
180
src/api/lib.rs
180
src/api/lib.rs
@ -12,30 +12,47 @@ use tribufu_types::games::Game;
|
|||||||
use tribufu_types::oauth2::{OAuth2GrantType, OAuth2TokenRequest, OAuth2TokenResponse};
|
use tribufu_types::oauth2::{OAuth2GrantType, OAuth2TokenRequest, OAuth2TokenResponse};
|
||||||
use tribufu_types::users::*;
|
use tribufu_types::users::*;
|
||||||
|
|
||||||
pub enum CredentialsType {
|
pub enum Credentials {
|
||||||
Anonymous,
|
Anonymous,
|
||||||
ApiKey,
|
ApiKey {
|
||||||
Basic,
|
api_key: String,
|
||||||
Bearer,
|
},
|
||||||
|
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<String>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TribufuApi {
|
pub struct TribufuApi {
|
||||||
base_url: String,
|
base_url: String,
|
||||||
credentials: Option<String>,
|
credentials: Credentials,
|
||||||
credentials_kind: CredentialsType,
|
token: Option<Token>,
|
||||||
http: Client,
|
http: Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TribufuApi {
|
impl Default for TribufuApi {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(CredentialsType::Anonymous, None)
|
Self::new(Credentials::Anonymous)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TribufuApi {
|
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<String>) -> Self {
|
pub fn new(credentials: Credentials) -> Self {
|
||||||
let http = Client::builder()
|
let http = Client::builder()
|
||||||
.user_agent(Self::user_agent())
|
.user_agent(Self::user_agent())
|
||||||
.default_headers(Self::default_headers())
|
.default_headers(Self::default_headers())
|
||||||
@ -43,9 +60,9 @@ impl TribufuApi {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
base_url: Self::BASE_URL.to_string(),
|
base_url: Self::get_base_url(),
|
||||||
credentials,
|
credentials,
|
||||||
credentials_kind,
|
token: None,
|
||||||
http,
|
http,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,114 +87,97 @@ impl TribufuApi {
|
|||||||
headers
|
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 {
|
pub fn with_api_key(api_key: String) -> Self {
|
||||||
let mut api = Self::default();
|
Self::new(Credentials::ApiKey { api_key })
|
||||||
api.use_api_key(api_key);
|
|
||||||
api
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_client(client_id: u64, client_secret: String) -> Self {
|
pub fn with_client(client_id: u64, client_secret: String) -> Self {
|
||||||
let mut api = Self::default();
|
Self::new(Credentials::Client {
|
||||||
api.use_client(client_id, client_secret);
|
client_id,
|
||||||
api
|
client_secret,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_token(token: String) -> Self {
|
pub fn with_api_key_from_env() -> Option<Self> {
|
||||||
let mut api = Self::default();
|
if let Ok(api_key) = env::var("TRIBUFU_API_KEY") {
|
||||||
api.use_bearer(token);
|
Some(Self::with_api_key(api_key))
|
||||||
api
|
} else {
|
||||||
}
|
None
|
||||||
|
|
||||||
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_client_from_env() -> Option<Self> {
|
||||||
let client_id = env::var("TRIBUFU_CLIENT_ID");
|
let client_id = env::var("TRIBUFU_CLIENT_ID");
|
||||||
let client_secret = env::var("TRIBUFU_CLIENT_SECRET");
|
let client_secret = env::var("TRIBUFU_CLIENT_SECRET");
|
||||||
|
|
||||||
if let (Ok(client_id), Ok(client_secret)) = (client_id, client_secret) {
|
if let (Ok(client_id), Ok(client_secret)) = (client_id, client_secret) {
|
||||||
self.use_client(client_id.parse().unwrap(), client_secret);
|
Some(Self::with_client(client_id.parse().unwrap(), client_secret))
|
||||||
}
|
} else {
|
||||||
|
None
|
||||||
if let Ok(api_key) = env::var("TRIBUFU_API_KEY") {
|
|
||||||
self.use_api_key(api_key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_anonymous(&mut self) {
|
pub fn set_anonymous(&mut self) {
|
||||||
self.credentials_kind = CredentialsType::Anonymous;
|
self.credentials = Credentials::Anonymous;
|
||||||
self.credentials = None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_api_key(&mut self, api_key: String) {
|
pub fn set_api_key(&mut self, api_key: String) {
|
||||||
self.credentials_kind = CredentialsType::ApiKey;
|
self.credentials = Credentials::ApiKey { api_key };
|
||||||
self.credentials = Some(api_key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_basic(&mut self, basic_token: String) {
|
pub fn set_clients(&mut self, client_id: u64, client_secret: String) {
|
||||||
self.credentials_kind = CredentialsType::Basic;
|
self.credentials = Credentials::Client {
|
||||||
self.credentials = Some(basic_token);
|
client_id,
|
||||||
|
client_secret,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_bearer(&mut self, bearer_token: String) {
|
pub fn set_basic_token(&mut self, basic_token: String) {
|
||||||
self.credentials_kind = CredentialsType::Bearer;
|
self.token = Some(Token::Basic { basic_token });
|
||||||
self.credentials = Some(bearer_token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn use_client(&mut self, client_id: u64, client_secret: String) {
|
pub fn set_bearer_token(&mut self, access_token: String, refresh_token: Option<String>) {
|
||||||
let credentials_str = format!("{}:{}", client_id, client_secret);
|
self.token = Some(Token::Bearer {
|
||||||
self.use_basic(BASE64.encode(credentials_str.as_bytes()));
|
access_token,
|
||||||
}
|
refresh_token,
|
||||||
|
});
|
||||||
fn set_base_url(&mut self, base_url: String) {
|
|
||||||
self.base_url = base_url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn headers(&self) -> HeaderMap {
|
fn headers(&self) -> HeaderMap {
|
||||||
let mut headers = Self::default_headers();
|
let mut headers = Self::default_headers();
|
||||||
|
|
||||||
match self.credentials_kind {
|
match &self.token {
|
||||||
CredentialsType::ApiKey => {
|
Some(token) => match token {
|
||||||
headers.insert(
|
Token::ApiKey { api_key } => {
|
||||||
AUTHORIZATION,
|
headers.insert(
|
||||||
HeaderValue::from_str(&format!(
|
AUTHORIZATION,
|
||||||
"ApiKey {}",
|
HeaderValue::from_str(&format!("ApiKey {}", api_key)).unwrap(),
|
||||||
self.credentials.as_ref().unwrap()
|
);
|
||||||
))
|
}
|
||||||
.unwrap(),
|
Token::Basic { basic_token } => {
|
||||||
);
|
headers.insert(
|
||||||
}
|
AUTHORIZATION,
|
||||||
CredentialsType::Basic => {
|
HeaderValue::from_str(&format!("Basic {}", basic_token)).unwrap(),
|
||||||
headers.insert(
|
);
|
||||||
AUTHORIZATION,
|
}
|
||||||
HeaderValue::from_str(&format!("Basic {}", self.credentials.as_ref().unwrap()))
|
Token::Bearer { access_token, .. } => {
|
||||||
.unwrap(),
|
headers.insert(
|
||||||
);
|
AUTHORIZATION,
|
||||||
}
|
HeaderValue::from_str(&format!("Bearer {}", access_token)).unwrap(),
|
||||||
CredentialsType::Bearer => {
|
);
|
||||||
headers.insert(
|
}
|
||||||
AUTHORIZATION,
|
},
|
||||||
HeaderValue::from_str(&format!(
|
None => {}
|
||||||
"Bearer {}",
|
|
||||||
self.credentials.as_ref().unwrap()
|
|
||||||
))
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
headers
|
headers
|
||||||
|
Reference in New Issue
Block a user