Lines
74.11 %
Functions
50 %
Branches
100 %
use axum::{
body::Body,
http::HeaderValue,
response::{IntoResponse, Response},
};
use log::error;
use reqwest::{self, Client, Method, StatusCode};
use serde::Deserialize;
use sylvia_iot_corelib::{err::ErrResp, role::Role};
use super::{middleware::GetTokenInfoData, ErrReq, State};
pub mod application_dldata;
pub mod application_uldata;
pub mod coremgr_opdata;
pub mod network_dldata;
pub mod network_uldata;
#[derive(Deserialize)]
struct GetUser {
data: User,
}
/// The user response that is from auth GET API.
struct User {
#[serde(rename = "userId")]
_user_id: String,
#[serde(rename = "account")]
_account: String,
struct GetUnit {
data: Unit,
/// The unit response that is from broker GET API.
struct Unit {
#[serde(rename = "unitId")]
_unit_id: String,
#[serde(rename = "code")]
_code: String,
async fn get_user_inner(
fn_name: &str,
client: &Client,
auth_base: &str,
user_id: &str,
token: &HeaderValue,
) -> Result<Option<User>, Response> {
let uri = format!("{}/api/v1/user/{}", auth_base, user_id);
match get_stream_resp(fn_name, token, &client, uri.as_str()).await {
Err(resp) => match resp.status() {
StatusCode::NOT_FOUND => Ok(None),
_ => Err(resp),
},
Ok(resp) => match resp.json::<GetUser>().await {
Err(e) => {
let e = format!("wrong response of user: {}", e);
error!("[{}] {}", fn_name, e);
Err(ErrResp::ErrIntMsg(Some(e)).into_response())
Ok(user) => Ok(Some(user.data)),
async fn get_unit_inner(
broker_base: &str,
unit_id: &str,
) -> Result<Option<Unit>, Response> {
let uri = format!("{}/api/v1/unit/{}", broker_base, unit_id);
Ok(resp) => match resp.json::<GetUnit>().await {
let e = format!("wrong response of unit: {}", e);
Ok(unit) => Ok(Some(unit.data)),
async fn get_unit_cond(
token_info: &GetTokenInfoData,
query_unit: Option<&String>,
state: &State,
) -> Result<Option<String>, Response> {
let broker_base = state.broker_base.as_str();
let client = state.client.clone();
match query_unit {
None => {
if !Role::is_role(&token_info.roles, Role::ADMIN)
&& !Role::is_role(&token_info.roles, Role::MANAGER)
{
return Err(ErrResp::ErrParam(Some("missing `unit`".to_string())).into_response());
Ok(None)
Some(unit_id) => match unit_id.len() {
0 => Ok(None),
_ => {
let token =
match HeaderValue::from_str(format!("Bearer {}", token_info.token).as_str()) {
error!("[{}] get token error: {}", fn_name, e);
return Err(ErrResp::ErrRsc(Some(format!("get token error: {}", e)))
.into_response());
Ok(value) => value,
match get_unit_inner(fn_name, &client, broker_base, unit_id, &token).await {
error!("[{}] get unit error", fn_name);
return Err(e);
Ok(unit) => match unit {
return Err(ErrResp::Custom(
ErrReq::UNIT_NOT_EXIST.0,
ErrReq::UNIT_NOT_EXIST.1,
None,
)
.into_response())
Some(_) => Ok(Some(unit_id.clone())),
async fn get_stream_resp(
uri: &str,
) -> Result<reqwest::Response, Response> {
match client
.request(Method::GET, uri)
.header(reqwest::header::AUTHORIZATION, token)
.build()
let e = format!("generate request error: {}", e);
Err(ErrResp::ErrRsc(Some(e)).into_response())
Ok(req) => match client.execute(req).await {
let e = format!("execute request error: {}", e);
Ok(resp) => match resp.status() {
StatusCode::OK => Ok(resp),
let mut resp_builder = Response::builder().status(resp.status());
for (k, v) in resp.headers() {
resp_builder = resp_builder.header(k, v);
match resp_builder.body(Body::from_stream(resp.bytes_stream())) {
let e = format!("wrap response body error: {}", e);
Ok(resp) => Err(resp),