-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into smrtrfszm/new-key-management
- Loading branch information
Showing
10 changed files
with
212 additions
and
148 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use super::Api; | ||
use anyhow::Result; | ||
use reqwest::Method; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub mod login { | ||
use super::*; | ||
|
||
#[derive(Debug, Serialize)] | ||
pub struct Request<'a> { | ||
pub token: &'a str, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Response { | ||
pub token: String, | ||
} | ||
} | ||
|
||
pub async fn login(api: &Api, req: &login::Request<'_>) -> Result<login::Response> { | ||
api.request(Method::POST, "/v1/apps/login", Some(req)).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
pub mod app; | ||
pub mod user; | ||
|
||
use reqwest::{header::AUTHORIZATION, Client, Method, Url}; | ||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
use thiserror::Error; | ||
use tokio::runtime::Handle; | ||
|
||
#[derive(Debug)] | ||
pub struct Api { | ||
client: Client, | ||
base: Url, | ||
handle: Handle, | ||
token: Option<String>, | ||
} | ||
|
||
impl Api { | ||
pub fn new(base: &str, token: Option<String>) -> anyhow::Result<Self> { | ||
Ok(Self { | ||
client: Client::new(), | ||
base: Url::parse(base)?, | ||
handle: Handle::current(), | ||
token, | ||
}) | ||
} | ||
|
||
pub fn with_token(&self, token: String) -> Self { | ||
Self { | ||
client: self.client.clone(), | ||
base: self.base.clone(), | ||
handle: self.handle.clone(), | ||
token: Some(token), | ||
} | ||
} | ||
|
||
#[inline] | ||
async fn request<Req, Res>( | ||
&self, | ||
method: Method, | ||
path: &str, | ||
req: Option<&Req>, | ||
) -> anyhow::Result<Res> | ||
where | ||
Req: Serialize, | ||
Res: DeserializeOwned + Send + 'static, | ||
{ | ||
let mut r = self.client.request(method, self.base.join(path).unwrap()); | ||
|
||
if let Some(token) = &self.token { | ||
r = r.header(AUTHORIZATION, &format!("Bearer {}", token)); | ||
} | ||
|
||
if let Some(req) = req { | ||
r = r.json(req) | ||
} | ||
|
||
self.handle | ||
.spawn(async move { | ||
let res: ErrorOr<Res> = r.send().await?.json().await?; | ||
|
||
match res { | ||
ErrorOr::Error(error) => { | ||
tracing::error!("iam error: {error:?}"); | ||
Err(error.into()) | ||
} | ||
ErrorOr::Data(res) => anyhow::Ok(res), | ||
} | ||
}) | ||
.await | ||
.unwrap() | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize, Error)] | ||
#[error("{error}")] | ||
pub struct Error { | ||
pub code: String, | ||
pub error: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(untagged)] | ||
enum ErrorOr<T> { | ||
Error(Error), | ||
Data(T), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use super::Api; | ||
use anyhow::Result; | ||
use reqwest::Method; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub mod register { | ||
use super::*; | ||
|
||
#[derive(Debug, Serialize)] | ||
pub struct Request<'a> { | ||
pub name: &'a str, | ||
pub email: &'a str, | ||
pub password: &'a str, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Response { | ||
pub id: String, | ||
} | ||
} | ||
|
||
pub async fn register(api: &Api, req: ®ister::Request<'_>) -> Result<register::Response> { | ||
api.request(Method::POST, "/v1/users/register", Some(req)) | ||
.await | ||
} | ||
|
||
pub mod login { | ||
use super::*; | ||
|
||
#[derive(Debug, Serialize)] | ||
pub struct Request<'a> { | ||
pub email: &'a str, | ||
pub password: &'a str, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Response { | ||
pub token: String, | ||
} | ||
} | ||
|
||
pub async fn login(api: &Api, req: &login::Request<'_>) -> Result<login::Response> { | ||
api.request(Method::POST, "/v1/users/login", Some(req)) | ||
.await | ||
} | ||
|
||
pub mod get_user { | ||
use iam_common::user::UserInfo; | ||
|
||
pub type Response = UserInfo; | ||
} | ||
|
||
pub async fn get_user(api: &Api, id: &str) -> Result<get_user::Response> { | ||
api.request(Method::GET, &format!("/v1/users/{id}"), None::<&()>) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.