Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration delete handler fixes #493

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/http/routers/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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))
Expand Down
31 changes: 18 additions & 13 deletions src/http/routers/v1/v1_integrations.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::PathBuf;
use std::sync::Arc;
use axum::Extension;
use axum::http::{Response, StatusCode};
Expand All @@ -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<Arc<ARwLock<GlobalContext>>>,
Expand Down Expand Up @@ -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<HTTPIntegrationDeleteQueryParams>,
) -> axum::response::Result<Response<Body>, 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<Arc<ARwLock<GlobalContext>>>,
body_bytes: hyper::body::Bytes,
) -> axum::response::Result<Response<Body>, ScratchError> {
let data = serde_json::from_slice::<HTTPIntegrationDeleteQueryParams>(&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())
}
16 changes: 16 additions & 0 deletions src/http/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedGlobalContext>, body_bytes: hyper::body::Bytes|
-> Pin<Box<dyn Future<Output=Result<Response<Body>, ScratchError>> + Send>> {
Box::pin($name(ex, body_bytes))
};
telemetry_wrapper(tmp, path, method, ex, body_bytes).await
})
};
}

Loading