From 348036e2ef53fc938a4249c83e6ce6a28f40a42c Mon Sep 17 00:00:00 2001 From: Eguo Wang Date: Thu, 19 Oct 2023 21:57:49 +0800 Subject: [PATCH] Response the raw structure without wrapper #7 --- apiserver/src/{response.rs => errors.rs} | 48 ------------------------ apiserver/src/handlers/actor.rs | 21 ++++++----- apiserver/src/handlers/mod.rs | 2 + apiserver/src/handlers/playbook.rs | 30 +++++++-------- apiserver/src/lib.rs | 2 +- apiserver/src/services/actor.rs | 2 +- apiserver/src/services/mod.rs | 3 +- apiserver/src/services/playbook.rs | 11 ++---- 8 files changed, 32 insertions(+), 87 deletions(-) rename apiserver/src/{response.rs => errors.rs} (55%) diff --git a/apiserver/src/response.rs b/apiserver/src/errors.rs similarity index 55% rename from apiserver/src/response.rs rename to apiserver/src/errors.rs index b27a5d9..db80fd6 100644 --- a/apiserver/src/response.rs +++ b/apiserver/src/errors.rs @@ -20,54 +20,6 @@ use serde_json::json; use thiserror::Error; use tracing::error; -/// Represents the response from an API call -#[derive(Serialize, Deserialize, Debug)] -pub struct Response { - /// The object or a Vec objects (the type `T` will depend on the endpoint). - data: Option, - /// Any API endpoint that returns a list of items requires pagination. - #[serde(skip_serializing_if = "Option::is_none")] - pagination: Option, -} - -/// Any API endpoint that returns a list of items requires pagination. -/// By default we will return 30 records from any listing endpoint. If an API -/// endpoint returns a list of items, then it will include a pagination object -/// that contains pagination information. -#[derive(Serialize, Deserialize, Debug)] -pub struct Pagination { - /// The page currently returned (default: 1) - pub current_page: u64, - /// The number of entries returned per page (default: 30) - pub per_page: u64, - /// The Total number of entries available in the entries collection. - pub total_entries: u64, - /// The total number of pages available given the current `per_page` value - pub total_pages: u64, -} - -impl IntoResponse for Response { - fn into_response(self) -> axum::response::Response { - Json(self).into_response() - } -} - -/// Returns the successful response with data. -pub fn data(data: T) -> Response { - Response { - data: Some(data), - pagination: None, - } -} - -/// Returns the successful paged response. -pub fn paginate(data: T, pagination: Pagination) -> Response { - Response { - data: Some(data), - pagination: Some(pagination), - } -} - #[derive(Serialize, Deserialize, Debug, Error)] pub enum ApiError { #[error("Database Error")] diff --git a/apiserver/src/handlers/actor.rs b/apiserver/src/handlers/actor.rs index a7bef17..be0e229 100644 --- a/apiserver/src/handlers/actor.rs +++ b/apiserver/src/handlers/actor.rs @@ -32,8 +32,9 @@ use kube::api::LogParams; use kube::Api; use uuid::Uuid; +use super::Result; use crate::context::Context; -use crate::response::{data, ApiError}; +use crate::errors::ApiError; use crate::services::actor::ActorService; // The Actors Service Handlers. @@ -51,8 +52,8 @@ use crate::services::actor::ActorService; ), tag = "Actors" )] -pub async fn list(Path(pid): Path, State(ctx): State>) -> Result { - Ok(data(ActorService::list(ctx, pid).await?)) +pub async fn list(Path(pid): Path, State(ctx): State>) -> Result { + Ok(Json(ActorService::list(ctx, pid).await?)) } /// Returns a actor detail. @@ -71,8 +72,8 @@ pub async fn list(Path(pid): Path, State(ctx): State>) -> Res pub async fn detail( State(ctx): State>, Path((pid, name)): Path<(Uuid, String)>, -) -> Result { - Ok(data(ActorService::get(ctx, pid, name).await?)) +) -> Result { + Ok(Json(ActorService::get(ctx, pid, name).await?)) } /// Output the log streams of actor @@ -126,8 +127,8 @@ pub async fn logs( pub async fn info( State(ctx): State>, Path((pid, name)): Path<(Uuid, String)>, -) -> Result { - Ok(data(ActorService::info(ctx, pid, name).await?)) +) -> Result { + Ok(Json(ActorService::info(ctx, pid, name).await?)) } /// Returns a actor's stats. @@ -146,8 +147,8 @@ pub async fn info( pub async fn stats( State(ctx): State>, Path((pid, name)): Path<(Uuid, String)>, -) -> Result { - Ok(data(ActorService::stats(ctx, pid, name).await?)) +) -> Result { + Ok(Json(ActorService::stats(ctx, pid, name).await?)) } /// Receive a actor's sources and publish them to Message Queue. @@ -172,7 +173,7 @@ pub async fn sync( State(ctx): State>, Path((pid, name)): Path<(Uuid, String)>, Json(req): Json, -) -> Result { +) -> Result { ActorService::sync(ctx, pid, name, req) .await .map_err(|err| ApiError::NatsError(err.to_string()))?; diff --git a/apiserver/src/handlers/mod.rs b/apiserver/src/handlers/mod.rs index 45af800..0cf2539 100644 --- a/apiserver/src/handlers/mod.rs +++ b/apiserver/src/handlers/mod.rs @@ -14,3 +14,5 @@ pub mod actor; pub mod playbook; + +type Result = std::result::Result; diff --git a/apiserver/src/handlers/playbook.rs b/apiserver/src/handlers/playbook.rs index e8a88c4..f04c465 100644 --- a/apiserver/src/handlers/playbook.rs +++ b/apiserver/src/handlers/playbook.rs @@ -28,9 +28,9 @@ use kube::Api; use tokio_stream::StreamExt as _; use uuid::Uuid; +use super::Result; use crate::context::Context; use crate::requests::playbook::{CreatePlaybookRequest, UpdatePlaybookRequest}; -use crate::response::{data, ApiError}; use crate::services::playbook::PlaybookService; // The Playbooks Service Handlers. @@ -45,9 +45,8 @@ use crate::services::playbook::PlaybookService; ), tag = "Playbooks" )] -pub async fn list(State(ctx): State>) -> Result { - let playbooks = PlaybookService::list(ctx).await?; - Ok(data(playbooks)) +pub async fn list(State(ctx): State>) -> Result { + Ok(Json(PlaybookService::list(ctx).await?)) } /// Create a playbook in the current account. @@ -66,9 +65,8 @@ pub async fn list(State(ctx): State>) -> Result>, Json(req): Json, -) -> Result { - let response = PlaybookService::create(ctx, &req).await?; - Ok((StatusCode::CREATED, data(response))) +) -> Result { + Ok((StatusCode::CREATED, Json(PlaybookService::create(ctx, &req).await?))) } /// Returns a playbook detail. @@ -84,9 +82,8 @@ pub async fn create( ), tag = "Playbooks" )] -pub async fn detail(Path(id): Path, State(ctx): State>) -> Result { - let playbook = PlaybookService::get(ctx, id).await?; - Ok(data(playbook)) +pub async fn detail(Path(id): Path, State(ctx): State>) -> Result { + Ok(Json(PlaybookService::get(ctx, id).await?)) } /// Update a playbook. @@ -109,10 +106,9 @@ pub async fn detail(Path(id): Path, State(ctx): State>) -> Re pub async fn update( Path(id): Path, State(ctx): State>, - Json(payload): Json, -) -> Result { - let playbook = PlaybookService::update(ctx, id, payload.title, payload.description).await?; - Ok(data(playbook)) + Json(req): Json, +) -> Result { + Ok(Json(PlaybookService::update(ctx, id, &req).await?)) } /// Delete a playbook @@ -127,7 +123,7 @@ pub async fn update( ), tag = "Playbooks" )] -pub async fn delete(Path(id): Path, State(ctx): State>) -> Result { +pub async fn delete(Path(id): Path, State(ctx): State>) -> Result { PlaybookService::delete(ctx, id).await?; Ok(StatusCode::NO_CONTENT) @@ -176,7 +172,7 @@ pub async fn events( ), tag = "Playbooks" )] -pub async fn start(Path(id): Path, State(ctx): State>) -> Result { +pub async fn start(Path(id): Path, State(ctx): State>) -> Result { PlaybookService::start(ctx, id).await?; Ok(StatusCode::NO_CONTENT) @@ -195,7 +191,7 @@ pub async fn start(Path(id): Path, State(ctx): State>) -> Res ), tag = "Playbooks", )] -pub async fn stop(Path(id): Path, State(ctx): State>) -> Result { +pub async fn stop(Path(id): Path, State(ctx): State>) -> Result { PlaybookService::stop(ctx, id).await?; Ok(StatusCode::NO_CONTENT) diff --git a/apiserver/src/lib.rs b/apiserver/src/lib.rs index a389fe0..b1f1fb5 100644 --- a/apiserver/src/lib.rs +++ b/apiserver/src/lib.rs @@ -15,9 +15,9 @@ pub mod app; pub mod config; pub mod context; +pub mod errors; pub mod handlers; pub mod requests; -pub mod response; pub mod responses; pub mod routes; pub mod services; diff --git a/apiserver/src/services/actor.rs b/apiserver/src/services/actor.rs index 1ce3b53..7208b09 100644 --- a/apiserver/src/services/actor.rs +++ b/apiserver/src/services/actor.rs @@ -21,7 +21,7 @@ use tracing::error; use uuid::Uuid; use crate::context::Context; -use crate::response::ApiError; +use crate::errors::ApiError; use crate::responses::actor::ActorResponse; use crate::services::Result; use amp_resources::actor; diff --git a/apiserver/src/services/mod.rs b/apiserver/src/services/mod.rs index 562906f..6867de9 100644 --- a/apiserver/src/services/mod.rs +++ b/apiserver/src/services/mod.rs @@ -15,5 +15,4 @@ pub mod actor; pub mod playbook; -use crate::response::ApiError; -pub type Result = std::result::Result; +pub type Result = std::result::Result; diff --git a/apiserver/src/services/playbook.rs b/apiserver/src/services/playbook.rs index 6a93f04..b4f45d7 100644 --- a/apiserver/src/services/playbook.rs +++ b/apiserver/src/services/playbook.rs @@ -22,8 +22,8 @@ use tracing::debug; use uuid::Uuid; use crate::context::Context; -use crate::requests::playbook::CreatePlaybookRequest; -use crate::response::ApiError; +use crate::errors::ApiError; +use crate::requests::playbook::{CreatePlaybookRequest, UpdatePlaybookRequest}; use crate::responses::playbook::PlaybookResponse; use crate::services::Result; @@ -92,12 +92,7 @@ impl PlaybookService { }) } - pub async fn update( - _ctx: Arc, - _id: Uuid, - _title: Option, - _description: Option, - ) -> Result { + pub async fn update(_ctx: Arc, _id: Uuid, _req: &UpdatePlaybookRequest) -> Result { unimplemented!() } }