From 5e7ffa2b1a4d13aff8fd633000b1f07c9ef715b9 Mon Sep 17 00:00:00 2001 From: Guilherme Werner Date: Fri, 1 Dec 2023 20:36:29 -0300 Subject: [PATCH] Add oauth2 client --- .cargo/config.toml | 2 -- .gitignore | 38 ++++++++++++++++++++++++++++-- Cargo.toml | 28 ++++++++++++---------- LICENSE.txt | 2 +- Source/lib.rs | 5 ---- examples/client.rs | 11 +++++++++ src/lib.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 22 deletions(-) delete mode 100644 .cargo/config.toml delete mode 100644 Source/lib.rs create mode 100644 examples/client.rs create mode 100644 src/lib.rs diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 420e44e..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -target-dir = "Intermediate/Target" diff --git a/.gitignore b/.gitignore index 20af12c..d5805d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,40 @@ -[Bb]inaries/ -[Ii]ntermediate/ +__pycache__/ +.gradle/ +.idea/ +.metals/ +.next/ +.parcel-cache/ +.vs/ +.vscode/ +bin/ +binaries/ +build/ +node_modules/ +obj/ +saved/ +target/ .DS_Store +.env +*.crt +*.csproj +*.filters +*.fsproj +*.key +*.log +*.make +*.mwb.bak +*.pem +*.sln +*.user +*.vcxproj +*.vpp.* +*.wasm +*.xcodeproj +*.xcworkspace Cargo.lock desktop.ini +keystore.jks +local.properties +Makefile +next-env.d.ts diff --git a/Cargo.toml b/Cargo.toml index 16fa2fe..3dbd74c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,24 +1,28 @@ [package] name = "tribufu" -version = "0.0.3" -description = "TribuFu SDK" -repository = "https://github.com/TribuFu/SDK-RS" -authors = ["TribuFu "] +version = "0.0.4" +description = "Tribufu SDK" +repository = "https://github.com/Tribufu/SDK-Rust" +authors = ["Tribufu "] license = "Apache-2.0" readme = "README.md" edition = "2021" publish = true -exclude = [ - ".github/", - ".vscode/", - ".editorconfig", - ".gitattributes", -] +exclude = [".github/", ".vscode/", ".editorconfig", ".gitattributes"] [lib] -name = "TribuFu" +name = "tribufu" crate-type = ["rlib"] -path = "Source/lib.rs" +path = "src/lib.rs" [dependencies] +alnilam-consts = { version = "0.0.4" } +anyhow = "1.0.75" +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"] } + +[dev-dependencies] +tokio = { version = "1", features = ["full"] } diff --git a/LICENSE.txt b/LICENSE.txt index a9afc15..7df1b1d 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright (c) TribuFu. All Rights Reserved + Copyright (c) Tribufu. All Rights Reserved Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/Source/lib.rs b/Source/lib.rs deleted file mode 100644 index 76eeb18..0000000 --- a/Source/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) TribuFu. All Rights Reserved - -#![allow(non_snake_case)] - -pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/examples/client.rs b/examples/client.rs new file mode 100644 index 0000000..152db3b --- /dev/null +++ b/examples/client.rs @@ -0,0 +1,11 @@ +// Copyright (c) Tribufu. All Rights Reserved + +use tribufu::*; + +#[tokio::main] +async fn main() { + match TribufuClient::new(0, "client_secret") { + Ok(client) => println!("client_id: {}", client.id()), + Err(e) => println!("error: {:?}", e), + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..810af7a --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,58 @@ +// Copyright (c) Tribufu. All Rights Reserved. + +#![allow(dead_code)] + +use alnilam_consts::TARGET_TRIPLE; +use anyhow::Result; +use reqwest::header::{HeaderMap, HeaderValue}; +use reqwest::Client; + +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + +#[derive(Clone)] +pub struct TribufuClient { + client_id: u64, + client_secret: String, + http: Client, +} + +impl TribufuClient { + const BASE_URL: &'static str = "https://api.tribufu.com"; + + pub fn new(id: u64, secret: impl Into) -> Result { + let user_agent = format!( + "Tribufu/{} (+https://api.tribufu.com; {})", + VERSION, TARGET_TRIPLE + ); + + let mut headers = HeaderMap::new(); + headers.insert("X-Tribufu-Language", HeaderValue::from_static("rust")); + headers.insert("X-Tribufu-Version", HeaderValue::from_static(VERSION)); + + let http = Client::builder() + .default_headers(headers) + .user_agent(user_agent) + .build()?; + + Ok(TribufuClient { + client_id: id, + client_secret: secret.into(), + http, + }) + } + + pub fn id(&self) -> u64 { + self.client_id + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_client() { + let client = TribufuClient::new(0, "client_secret").unwrap(); + assert_eq!(client.id(), 0); + } +}