From 0b1f102d46c9da9974c63587605a9ef04fbd29d3 Mon Sep 17 00:00:00 2001 From: JegernOUTT Date: Fri, 13 Dec 2024 19:20:19 +1030 Subject: [PATCH 1/2] Add telemetry support for DELETE HTTP operations - Introduce a new `telemetry_delete` macro to wrap DELETE requests with telemetry functionality. - Modify the `/integration-delete` route to use the `telemetry_delete` macro for enhanced tracking. - Update `handle_v1_integration_delete` function to parse request body as JSON and validate integration paths more robustly. - Switch query parameter integration path type to `PathBuf` for better path handling. - Improve logging and error messages for clarity in path deletion and error responses. --- src/http/routers/v1.rs | 4 ++-- src/http/routers/v1/v1_integrations.rs | 31 +++++++++++++++----------- src/http/utils.rs | 16 +++++++++++++ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/http/routers/v1.rs b/src/http/routers/v1.rs index 7e7027573..4a0e98e37 100644 --- a/src/http/routers/v1.rs +++ b/src/http/routers/v1.rs @@ -9,7 +9,7 @@ use hyper::Body; use hyper::Response; use tower_http::cors::CorsLayer; -use crate::{telemetry_get, telemetry_post}; +use crate::{telemetry_delete, telemetry_get, telemetry_post}; use crate::custom_error::ScratchError; use crate::global_context::SharedGlobalContext; use crate::http::routers::v1::code_completion::{handle_v1_code_completion_web, handle_v1_code_completion_prompt}; @@ -129,7 +129,7 @@ pub fn make_v1_router() -> Router { .route("/integrations-filtered/:integr_name", get(handle_v1_integrations_filtered)) .route("/integration-get", telemetry_post!(handle_v1_integration_get)) .route("/integration-save", telemetry_post!(handle_v1_integration_save)) - .route("/integration-delete", delete(handle_v1_integration_delete)) + .route("/integration-delete", telemetry_delete!(handle_v1_integration_delete)) .route("/integration-icon/:icon_name", get(handle_v1_integration_icon)) .route("/docker-container-list", telemetry_post!(handle_v1_docker_container_list)) diff --git a/src/http/routers/v1/v1_integrations.rs b/src/http/routers/v1/v1_integrations.rs index 455f3c973..dbf0ec365 100644 --- a/src/http/routers/v1/v1_integrations.rs +++ b/src/http/routers/v1/v1_integrations.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::sync::Arc; use axum::Extension; use axum::http::{Response, StatusCode}; @@ -6,12 +7,11 @@ use serde::Deserialize; use tokio::sync::RwLock as ARwLock; use regex::Regex; use axum::extract::Path; -use axum::extract::Query; use crate::custom_error::ScratchError; use crate::global_context::GlobalContext; - +use crate::integrations::setting_up_integrations::split_path_into_project_and_integration; pub async fn handle_v1_integrations( Extension(gcx): Extension>>, @@ -155,28 +155,33 @@ pub async fn handle_v1_integration_icon( // Define a structure to match query parameters #[derive(Deserialize)] pub struct HTTPIntegrationDeleteQueryParams { - integration_path: String, // Optional field for flexibility + integration_path: PathBuf } pub async fn handle_v1_integration_delete( - Query(params): Query, -) -> axum::response::Result, ScratchError> { - - let integration_path = params.integration_path; - log::info!("Deleting integration path: {}", integration_path); - - // If file path exists, delete it - if !std::path::Path::new(&integration_path).exists() { + Extension(_gcx): Extension>>, + body_bytes: hyper::body::Bytes, +) -> axum::response::Result, ScratchError> { + let data = serde_json::from_slice::(&body_bytes) + .map_err(|e| ScratchError::new(StatusCode::UNPROCESSABLE_ENTITY, format!("JSON problem: {}", e)))?; + let integration_path = data.integration_path; + log::info!("Deleting integration path: {:?}", integration_path); + + split_path_into_project_and_integration(&integration_path).map_err( + |_| ScratchError::new(StatusCode::UNPROCESSABLE_ENTITY, "integration_path is invalid".to_string()) + )?; + + if !integration_path.exists() { return Err(ScratchError::new(StatusCode::NOT_FOUND, "integration_path not found".to_string())); } std::fs::remove_file(&integration_path).map_err(|e| { - ScratchError::new(StatusCode::INTERNAL_SERVER_ERROR, format!("Failed to delete file: {}", e)) + ScratchError::new(StatusCode::INTERNAL_SERVER_ERROR, format!("failed to delete integration config: {}", e)) })?; Ok(Response::builder() .status(StatusCode::OK) .header("Content-Type", "application/json") - .body(Body::from(format!(""))) + .body(Body::from("{}")) .unwrap()) } diff --git a/src/http/utils.rs b/src/http/utils.rs index 2b17aead9..38ccde83b 100644 --- a/src/http/utils.rs +++ b/src/http/utils.rs @@ -73,3 +73,19 @@ macro_rules! telemetry_get { }) }; } + +#[macro_export] +macro_rules! telemetry_delete { + ( + $name:ident + ) => { + delete(|path, method, ex, body_bytes| async { + let tmp = |ex: Extension, body_bytes: hyper::body::Bytes| + -> Pin, ScratchError>> + Send>> { + Box::pin($name(ex, body_bytes)) + }; + telemetry_wrapper(tmp, path, method, ex, body_bytes).await + }) + }; +} + From 5e652d100c7974761dc5871ebab8cee0fb9e300a Mon Sep 17 00:00:00 2001 From: JegernOUTT Date: Mon, 16 Dec 2024 17:24:06 +1030 Subject: [PATCH 2/2] Remove obsolete telemetry_delete macro and update integration delete handling in v1 routers - Remove the `telemetry_delete` macro from `utils.rs`, as it is no longer needed. - Update `handle_v1_integration_delete` function to use query parameters instead of parsing JSON from the request body. - Modify the `/integration-delete` route in `v1.rs` to directly use the `delete` handler without the telemetry macro. --- src/http/routers/v1.rs | 4 ++-- src/http/routers/v1/v1_integrations.rs | 8 +++----- src/http/utils.rs | 16 ---------------- 3 files changed, 5 insertions(+), 23 deletions(-) diff --git a/src/http/routers/v1.rs b/src/http/routers/v1.rs index 4a0e98e37..7e7027573 100644 --- a/src/http/routers/v1.rs +++ b/src/http/routers/v1.rs @@ -9,7 +9,7 @@ use hyper::Body; use hyper::Response; use tower_http::cors::CorsLayer; -use crate::{telemetry_delete, telemetry_get, telemetry_post}; +use crate::{telemetry_get, telemetry_post}; use crate::custom_error::ScratchError; use crate::global_context::SharedGlobalContext; use crate::http::routers::v1::code_completion::{handle_v1_code_completion_web, handle_v1_code_completion_prompt}; @@ -129,7 +129,7 @@ pub fn make_v1_router() -> Router { .route("/integrations-filtered/:integr_name", get(handle_v1_integrations_filtered)) .route("/integration-get", telemetry_post!(handle_v1_integration_get)) .route("/integration-save", telemetry_post!(handle_v1_integration_save)) - .route("/integration-delete", telemetry_delete!(handle_v1_integration_delete)) + .route("/integration-delete", delete(handle_v1_integration_delete)) .route("/integration-icon/:icon_name", get(handle_v1_integration_icon)) .route("/docker-container-list", telemetry_post!(handle_v1_docker_container_list)) diff --git a/src/http/routers/v1/v1_integrations.rs b/src/http/routers/v1/v1_integrations.rs index dbf0ec365..14863f172 100644 --- a/src/http/routers/v1/v1_integrations.rs +++ b/src/http/routers/v1/v1_integrations.rs @@ -7,6 +7,7 @@ use serde::Deserialize; use tokio::sync::RwLock as ARwLock; use regex::Regex; use axum::extract::Path; +use axum::extract::Query; use crate::custom_error::ScratchError; @@ -159,12 +160,9 @@ pub struct HTTPIntegrationDeleteQueryParams { } pub async fn handle_v1_integration_delete( - Extension(_gcx): Extension>>, - body_bytes: hyper::body::Bytes, + Query(params): Query, ) -> axum::response::Result, ScratchError> { - let data = serde_json::from_slice::(&body_bytes) - .map_err(|e| ScratchError::new(StatusCode::UNPROCESSABLE_ENTITY, format!("JSON problem: {}", e)))?; - let integration_path = data.integration_path; + let integration_path = params.integration_path; log::info!("Deleting integration path: {:?}", integration_path); split_path_into_project_and_integration(&integration_path).map_err( diff --git a/src/http/utils.rs b/src/http/utils.rs index 38ccde83b..2b17aead9 100644 --- a/src/http/utils.rs +++ b/src/http/utils.rs @@ -73,19 +73,3 @@ macro_rules! telemetry_get { }) }; } - -#[macro_export] -macro_rules! telemetry_delete { - ( - $name:ident - ) => { - delete(|path, method, ex, body_bytes| async { - let tmp = |ex: Extension, body_bytes: hyper::body::Bytes| - -> Pin, ScratchError>> + Send>> { - Box::pin($name(ex, body_bytes)) - }; - telemetry_wrapper(tmp, path, method, ex, body_bytes).await - }) - }; -} -