Skip to content

Commit

Permalink
feat(httpd): define types for http requests
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <tareknaser360@gmail.com>
  • Loading branch information
tareknaser committed Jun 26, 2023
1 parent 349d86e commit fc4d718
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coffee_httpd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ coffee_lib = { path = "../coffee_lib" }
paperclip = { version = "0.8.0", features = ["actix4"] }
log = "0.4.17"
env_logger = "0.9.3"
serde = "1"
serde_json = "1"
1 change: 1 addition & 0 deletions coffee_httpd/src/httpd.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod server;
pub use server::*;
pub mod utils;
34 changes: 14 additions & 20 deletions coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tokio::sync::Mutex;

use serde_json::Value;

use super::utils::*;
use coffee_core::coffee::CoffeeManager;
use coffee_lib::plugin_manager::PluginManager;

Expand Down Expand Up @@ -79,11 +80,9 @@ async fn coffee_help(
#[post("/install")]
async fn coffee_install(
data: web::Data<AppState>,
body: Json<HashMap<String, String>>,
body: Json<InstallRequest>,
) -> Result<HttpResponse, Error> {
let plugin = body.get("plugin").ok_or_else(|| {
actix_web::error::ErrorBadRequest("Missing 'plugin' field in the request body")
})?;
let plugin = &body.plugin;

let mut coffee = data.coffee.lock().await;
let result = coffee.install(plugin, false, false).await;
Expand All @@ -100,17 +99,18 @@ async fn coffee_install(
#[post("/remove")]
async fn coffee_remove(
data: web::Data<AppState>,
body: Json<HashMap<String, String>>,
body: Json<RemoveRequest>,
) -> Result<HttpResponse, Error> {
let plugin = body.get("plugin").ok_or_else(|| {
actix_web::error::ErrorBadRequest("Missing 'plugin' field in the request body")
})?;
let plugin = &body.plugin;

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

match result {
Ok(_) => Ok(HttpResponse::Ok().body(format!("Plugin '{}' removed successfully", plugin))),
Ok(val) => {
log::info!("Plugin {:?} removed successfully", val);
Ok(HttpResponse::Ok().body(format!("Plugin '{}' removed successfully", plugin)))
}
Err(err) => Err(actix_web::error::ErrorInternalServerError(format!(
"Failed to remove plugin: {err}"
))),
Expand Down Expand Up @@ -139,14 +139,10 @@ async fn coffee_list(data: web::Data<AppState>) -> Result<Json<Value>, Error> {
#[post("/remote/add")]
async fn coffee_remote_add(
data: web::Data<AppState>,
body: Json<HashMap<String, String>>,
body: Json<RemoteAddRequest>,
) -> Result<HttpResponse, Error> {
let repository_name = body.get("repository_name").ok_or_else(|| {
actix_web::error::ErrorBadRequest("Missing 'repository_name' field in the request body")
})?;
let repository_url = body.get("repository_url").ok_or_else(|| {
actix_web::error::ErrorBadRequest("Missing 'repository_url' field in the request body")
})?;
let repository_name = &body.repository_name;
let repository_url = &body.repository_url;

let mut coffee = data.coffee.lock().await;
let result = coffee.add_remote(repository_name, repository_url).await;
Expand All @@ -166,11 +162,9 @@ async fn coffee_remote_add(
#[post("/remote/rm")]
async fn coffee_remote_rm(
data: web::Data<AppState>,
body: Json<HashMap<String, String>>,
body: Json<RemoteRmRequest>,
) -> Result<HttpResponse, Error> {
let repository_name = body.get("repository_name").ok_or_else(|| {
actix_web::error::ErrorBadRequest("Missing 'repository_name' field in the request body")
})?;
let repository_name = &body.repository_name;

let mut coffee = data.coffee.lock().await;
let result = coffee.rm_remote(repository_name).await;
Expand Down
23 changes: 23 additions & 0 deletions coffee_httpd/src/httpd/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use paperclip::actix::Apiv2Schema;
use serde::Deserialize;

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

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

#[derive(Debug, Deserialize, Apiv2Schema)]
pub struct RemoteAddRequest {
pub repository_name: String,
pub repository_url: String,
}

#[derive(Debug, Deserialize, Apiv2Schema)]
pub struct RemoteRmRequest {
pub repository_name: String,
}

0 comments on commit fc4d718

Please sign in to comment.