Skip to content

Commit

Permalink
feat(httpd): implement the show endpoint
Browse files Browse the repository at this point in the history
This commit:
1- define a new type Show to send to the show http endpoint
2- implement coffee_show function to handle /show endpoint
3- modify the /list and /remote/list endpoints to use the macro

Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser authored and vincenzopalazzo committed Jul 24, 2023
1 parent ef5df5a commit 334e8db
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
38 changes: 14 additions & 24 deletions coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub async fn run_httpd<T: ToSocketAddrs>(
.service(coffee_remote_add)
.service(coffee_remote_rm)
.service(coffee_remote_list)
.service(coffee_show)
.with_json_spec_at("/api/v1")
.build()
})
Expand Down Expand Up @@ -112,17 +113,7 @@ async fn coffee_remove(
async fn coffee_list(data: web::Data<AppState>) -> Result<Json<Value>, Error> {
let mut coffee = data.coffee.lock().await;
let result = coffee.list().await;
match result {
Ok(coffee_list) => {
let val = serde_json::to_value(coffee_list).map_err(|err| {
actix_web::error::ErrorInternalServerError(format!("coffee list error: {err}"))
})?;
Ok(Json(val))
}
Err(err) => Err(actix_web::error::ErrorInternalServerError(format!(
"coffee list error: {err}"
))),
}
handle_httpd_response!(result)
}

#[api_v2_operation]
Expand Down Expand Up @@ -163,19 +154,18 @@ async fn coffee_remote_list(data: web::Data<AppState>) -> Result<Json<Value>, Er
let mut coffee = data.coffee.lock().await;
let result = coffee.list_remotes().await;

match result {
Ok(coffee_remotes) => {
let val = serde_json::to_value(coffee_remotes).map_err(|err| {
actix_web::error::ErrorInternalServerError(format!(
"Failed to list remote repositories: {err}"
))
})?;
Ok(Json(val))
}
Err(err) => Err(actix_web::error::ErrorInternalServerError(format!(
"Failed to list remote repositories: {err}"
))),
}
handle_httpd_response!(result)
}

#[api_v2_operation]
#[get("/show")]
async fn coffee_show(data: web::Data<AppState>, body: Json<Show>) -> Result<Json<Value>, Error> {
let plugin = &body.plugin;

let mut coffee = data.coffee.lock().await;
let result = coffee.show(plugin).await;

handle_httpd_response!(result)
}

// this is just a hack to support swagger UI with https://paperclip-rs.github.io/paperclip/
Expand Down
5 changes: 5 additions & 0 deletions coffee_lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ pub mod request {
pub struct RemoteRm {
pub repository_name: String,
}

#[derive(Debug, Deserialize, Apiv2Schema, Serialize)]
pub struct Show {
pub plugin: String,
}
}

// Definition of the response types.
Expand Down

0 comments on commit 334e8db

Please sign in to comment.