Skip to content

Commit

Permalink
Response the raw structure without wrapper #7
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Oct 19, 2023
1 parent 6405185 commit 348036e
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 87 deletions.
48 changes: 0 additions & 48 deletions apiserver/src/response.rs → apiserver/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
/// The object or a Vec<T> objects (the type `T` will depend on the endpoint).
data: Option<T>,
/// Any API endpoint that returns a list of items requires pagination.
#[serde(skip_serializing_if = "Option::is_none")]
pagination: Option<Pagination>,
}

/// 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<T: Serialize> IntoResponse for Response<T> {
fn into_response(self) -> axum::response::Response {
Json(self).into_response()
}
}

/// Returns the successful response with data.
pub fn data<T>(data: T) -> Response<T> {
Response {
data: Some(data),
pagination: None,
}
}

/// Returns the successful paged response.
pub fn paginate<T>(data: T, pagination: Pagination) -> Response<T> {
Response {
data: Some(data),
pagination: Some(pagination),
}
}

#[derive(Serialize, Deserialize, Debug, Error)]
pub enum ApiError {
#[error("Database Error")]
Expand Down
21 changes: 11 additions & 10 deletions apiserver/src/handlers/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -51,8 +52,8 @@ use crate::services::actor::ActorService;
),
tag = "Actors"
)]
pub async fn list(Path(pid): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse, ApiError> {
Ok(data(ActorService::list(ctx, pid).await?))
pub async fn list(Path(pid): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
Ok(Json(ActorService::list(ctx, pid).await?))
}

/// Returns a actor detail.
Expand All @@ -71,8 +72,8 @@ pub async fn list(Path(pid): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Res
pub async fn detail(
State(ctx): State<Arc<Context>>,
Path((pid, name)): Path<(Uuid, String)>,
) -> Result<impl IntoResponse, ApiError> {
Ok(data(ActorService::get(ctx, pid, name).await?))
) -> Result<impl IntoResponse> {
Ok(Json(ActorService::get(ctx, pid, name).await?))
}

/// Output the log streams of actor
Expand Down Expand Up @@ -126,8 +127,8 @@ pub async fn logs(
pub async fn info(
State(ctx): State<Arc<Context>>,
Path((pid, name)): Path<(Uuid, String)>,
) -> Result<impl IntoResponse, ApiError> {
Ok(data(ActorService::info(ctx, pid, name).await?))
) -> Result<impl IntoResponse> {
Ok(Json(ActorService::info(ctx, pid, name).await?))
}

/// Returns a actor's stats.
Expand All @@ -146,8 +147,8 @@ pub async fn info(
pub async fn stats(
State(ctx): State<Arc<Context>>,
Path((pid, name)): Path<(Uuid, String)>,
) -> Result<impl IntoResponse, ApiError> {
Ok(data(ActorService::stats(ctx, pid, name).await?))
) -> Result<impl IntoResponse> {
Ok(Json(ActorService::stats(ctx, pid, name).await?))
}

/// Receive a actor's sources and publish them to Message Queue.
Expand All @@ -172,7 +173,7 @@ pub async fn sync(
State(ctx): State<Arc<Context>>,
Path((pid, name)): Path<(Uuid, String)>,
Json(req): Json<Synchronization>,
) -> Result<impl IntoResponse, ApiError> {
) -> Result<impl IntoResponse> {
ActorService::sync(ctx, pid, name, req)
.await
.map_err(|err| ApiError::NatsError(err.to_string()))?;
Expand Down
2 changes: 2 additions & 0 deletions apiserver/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@

pub mod actor;
pub mod playbook;

type Result<T, E = crate::errors::ApiError> = std::result::Result<T, E>;
30 changes: 13 additions & 17 deletions apiserver/src/handlers/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -45,9 +45,8 @@ use crate::services::playbook::PlaybookService;
),
tag = "Playbooks"
)]
pub async fn list(State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse, ApiError> {
let playbooks = PlaybookService::list(ctx).await?;
Ok(data(playbooks))
pub async fn list(State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
Ok(Json(PlaybookService::list(ctx).await?))
}

/// Create a playbook in the current account.
Expand All @@ -66,9 +65,8 @@ pub async fn list(State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse,
pub async fn create(
State(ctx): State<Arc<Context>>,
Json(req): Json<CreatePlaybookRequest>,
) -> Result<impl IntoResponse, ApiError> {
let response = PlaybookService::create(ctx, &req).await?;
Ok((StatusCode::CREATED, data(response)))
) -> Result<impl IntoResponse> {
Ok((StatusCode::CREATED, Json(PlaybookService::create(ctx, &req).await?)))
}

/// Returns a playbook detail.
Expand All @@ -84,9 +82,8 @@ pub async fn create(
),
tag = "Playbooks"
)]
pub async fn detail(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse, ApiError> {
let playbook = PlaybookService::get(ctx, id).await?;
Ok(data(playbook))
pub async fn detail(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
Ok(Json(PlaybookService::get(ctx, id).await?))
}

/// Update a playbook.
Expand All @@ -109,10 +106,9 @@ pub async fn detail(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Re
pub async fn update(
Path(id): Path<Uuid>,
State(ctx): State<Arc<Context>>,
Json(payload): Json<UpdatePlaybookRequest>,
) -> Result<impl IntoResponse, ApiError> {
let playbook = PlaybookService::update(ctx, id, payload.title, payload.description).await?;
Ok(data(playbook))
Json(req): Json<UpdatePlaybookRequest>,
) -> Result<impl IntoResponse> {
Ok(Json(PlaybookService::update(ctx, id, &req).await?))
}

/// Delete a playbook
Expand All @@ -127,7 +123,7 @@ pub async fn update(
),
tag = "Playbooks"
)]
pub async fn delete(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse, ApiError> {
pub async fn delete(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
PlaybookService::delete(ctx, id).await?;

Ok(StatusCode::NO_CONTENT)
Expand Down Expand Up @@ -176,7 +172,7 @@ pub async fn events(
),
tag = "Playbooks"
)]
pub async fn start(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse, ApiError> {
pub async fn start(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
PlaybookService::start(ctx, id).await?;

Ok(StatusCode::NO_CONTENT)
Expand All @@ -195,7 +191,7 @@ pub async fn start(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Res
),
tag = "Playbooks",
)]
pub async fn stop(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse, ApiError> {
pub async fn stop(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
PlaybookService::stop(ctx, id).await?;

Ok(StatusCode::NO_CONTENT)
Expand Down
2 changes: 1 addition & 1 deletion apiserver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion apiserver/src/services/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions apiserver/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
pub mod actor;
pub mod playbook;

use crate::response::ApiError;
pub type Result<T, E = ApiError> = std::result::Result<T, E>;
pub type Result<T, E = crate::errors::ApiError> = std::result::Result<T, E>;
11 changes: 3 additions & 8 deletions apiserver/src/services/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -92,12 +92,7 @@ impl PlaybookService {
})
}

pub async fn update(
_ctx: Arc<Context>,
_id: Uuid,
_title: Option<String>,
_description: Option<String>,
) -> Result<PlaybookResponse> {
pub async fn update(_ctx: Arc<Context>, _id: Uuid, _req: &UpdatePlaybookRequest) -> Result<PlaybookResponse> {
unimplemented!()
}
}

0 comments on commit 348036e

Please sign in to comment.