Skip to content

Commit

Permalink
feat: add libiam get_user helper
Browse files Browse the repository at this point in the history
  • Loading branch information
smrtrfszm authored and TwoDCube committed Mar 4, 2023
1 parent 40f237a commit 8facc89
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 19 deletions.
1 change: 1 addition & 0 deletions iam-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ pub mod error;
mod id;
pub mod password;
pub mod token;
pub mod user;

pub use id::*;
22 changes: 22 additions & 0 deletions iam-common/src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::error::{self, Result};
use iam_entity::users;
use sea_orm::{ConnectionTrait, EntityTrait, FromQueryResult};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, FromQueryResult, Serialize, Deserialize, PartialEq, Eq)]
pub struct UserInfo {
pub id: String,
pub name: String,
pub email: String,
}

pub async fn get_user<D>(db: &D, id: &str) -> Result<UserInfo>
where
D: ConnectionTrait,
{
users::Entity::find_by_id(id.to_owned())
.into_model()
.one(db)
.await?
.ok_or(error::USER_NOT_FOUND)
}
17 changes: 3 additions & 14 deletions iam/src/handlers/v1/users/id/get.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{json::Json, shared::SharedTrait};
use axum::{extract::Path, Extension};
use iam_common::error::{self, Result};
use iam_entity::users;
use sea_orm::entity::EntityTrait;
use iam_common::{error::Result, user::UserInfo};
use serde::Serialize;

#[derive(Serialize, Debug)]
Expand All @@ -15,15 +13,6 @@ pub struct GetUserResponse {
pub async fn get_user<S: SharedTrait>(
Extension(shared): Extension<S>,
Path(id): Path<String>,
) -> Result<Json<GetUserResponse>> {
let res = users::Entity::find_by_id(id)
.one(shared.db())
.await?
.ok_or(error::USER_NOT_FOUND)?;

Ok(Json(GetUserResponse {
id: res.id,
name: res.name,
email: res.email,
}))
) -> Result<Json<UserInfo>> {
Ok(Json(iam_common::user::get_user(shared.db(), &id).await?))
}
28 changes: 25 additions & 3 deletions libiam/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use std::sync::Arc;

use iam_common::user::UserInfo;
use reqwest::Client;
use serde::Deserialize;
use serde_json::json;

use crate::{
error::{unwrap_res, ErrorMessage, Result},
utils::Either,
utils::{create_client, Either},
Iam,
};

#[derive(Debug)]
pub struct AppInner {
secret: String,
token: String,
_iam: Iam,
iam: Iam,
client: Client,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -46,12 +48,18 @@ impl App {
Ok(Self {
inner: Arc::new(AppInner {
secret: secret.to_owned(),
client: create_client(&res.token),
token: res.token,
_iam: iam.clone(),
iam: iam.clone(),
}),
})
}

#[inline]
fn client(&self) -> &Client {
&self.inner.client
}

pub fn token(&self) -> &str {
&self.inner.token
}
Expand All @@ -60,4 +68,18 @@ impl App {
let (id, _) = iam_common::app::parse_token(&self.inner.secret).unwrap();
id
}

pub async fn get_user_info(&self, id: &str) -> Result<UserInfo> {
let res = self
.client()
.get(self.inner.iam.get_url(&format!("/v1/users/{}/", id)))
.send()
.await?
.json::<Either<UserInfo, ErrorMessage>>()
.await?;

let res = unwrap_res(res)?;

Ok(res)
}
}
7 changes: 5 additions & 2 deletions libiam/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::utils::Either;
#[derive(Debug, Deserialize)]
pub struct ErrorMessage {
pub code: String,
pub message: String,
pub error: String,
}

#[derive(Debug, thiserror::Error)]
Expand All @@ -22,6 +22,9 @@ pub type Result<T> = std::result::Result<T, Error>;
pub fn unwrap_res<T>(either: Either<T, ErrorMessage>) -> Result<T> {
match either {
Either::Left(t) => Ok(t),
Either::Right(err) => Err(Error::Iam(err)),
Either::Right(err) => {
tracing::error!("iam returned error: {err:?}");
Err(Error::Iam(err))
}
}
}
1 change: 1 addition & 0 deletions libiam/src/testing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod actions;
pub mod apps;
mod db;
pub mod users;

pub use db::Database;
11 changes: 11 additions & 0 deletions libiam/src/testing/users.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub use iam_common::user::UserInfo;
use sea_orm::ConnectionTrait;

pub async fn get_user<D>(db: &D, id: &str) -> UserInfo
where
D: ConnectionTrait,
{
iam_common::user::get_user(db, id)
.await
.expect("failed to get user")
}
17 changes: 17 additions & 0 deletions libiam/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use reqwest::{
header::{HeaderMap, HeaderValue, AUTHORIZATION},
Client,
};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
Expand All @@ -6,3 +10,16 @@ pub enum Either<L, R> {
Left(L),
Right(R),
}

pub fn create_client(token: &str) -> Client {
let mut header_map = HeaderMap::with_capacity(1);
header_map.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(),
);

Client::builder()
.default_headers(header_map)
.build()
.expect("failed to create reqwest client")
}

0 comments on commit 8facc89

Please sign in to comment.