Add oauth2 client

This commit is contained in:
Guilherme Werner
2023-12-01 20:36:29 -03:00
parent 8312341dc3
commit 5e7ffa2b1a
7 changed files with 122 additions and 22 deletions

View File

@ -1,2 +0,0 @@
[build]
target-dir = "Intermediate/Target"

38
.gitignore vendored
View File

@ -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

View File

@ -1,24 +1,28 @@
[package]
name = "tribufu"
version = "0.0.3"
description = "TribuFu SDK"
repository = "https://github.com/TribuFu/SDK-RS"
authors = ["TribuFu <contact@tribufu.com>"]
version = "0.0.4"
description = "Tribufu SDK"
repository = "https://github.com/Tribufu/SDK-Rust"
authors = ["Tribufu <contact@tribufu.com>"]
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"] }

View File

@ -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.

View File

@ -1,5 +0,0 @@
// Copyright (c) TribuFu. All Rights Reserved
#![allow(non_snake_case)]
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

11
examples/client.rs Normal file
View File

@ -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),
}
}

58
src/lib.rs Normal file
View File

@ -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<String>) -> Result<TribufuClient> {
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);
}
}