diff --git a/src-tauri/src/analytics.rs b/src-tauri/src/analytics.rs index 94ba0210..9992ab6a 100644 --- a/src-tauri/src/analytics.rs +++ b/src-tauri/src/analytics.rs @@ -2,7 +2,6 @@ use log::{debug, warn}; use serde::{Deserialize, Serialize}; use serde_json::json; use sqlx::types::JsonValue; -use sqlx::{Pool, Sqlite}; use tauri::{AppHandle, Manager}; use crate::{is_dev, models}; @@ -128,15 +127,16 @@ pub struct LaunchEventInfo { pub num_launches: i32, } -pub async fn track_launch_event(app_handle: &AppHandle, db: &Pool) -> LaunchEventInfo { +pub async fn track_launch_event(app_handle: &AppHandle) -> LaunchEventInfo { let namespace = "analytics"; let last_tracked_version_key = "last_tracked_version"; let mut info = LaunchEventInfo::default(); - info.num_launches = models::get_key_value_int(db, namespace, "num_launches", 0).await + 1; + info.num_launches = + models::get_key_value_int(app_handle, namespace, "num_launches", 0).await + 1; info.previous_version = - models::get_key_value_string(db, namespace, last_tracked_version_key, "").await; + models::get_key_value_string(app_handle, namespace, last_tracked_version_key, "").await; info.current_version = app_handle.package_info().version.to_string(); if info.previous_version.is_empty() { @@ -172,13 +172,13 @@ pub async fn track_launch_event(app_handle: &AppHandle, db: &Pool) -> La // Update key values models::set_key_value_string( - db, + app_handle, namespace, last_tracked_version_key, info.current_version.as_str(), ) .await; - models::set_key_value_int(db, namespace, "num_launches", info.num_launches).await; + models::set_key_value_int(app_handle, namespace, "num_launches", info.num_launches).await; info } diff --git a/src-tauri/src/http.rs b/src-tauri/src/http.rs index cc670229..1214cb46 100644 --- a/src-tauri/src/http.rs +++ b/src-tauri/src/http.rs @@ -13,14 +13,12 @@ use log::{error, info, warn}; use reqwest::redirect::Policy; use reqwest::{multipart, Url}; use sqlx::types::{Json, JsonValue}; -use sqlx::{Pool, Sqlite}; -use tauri::{AppHandle, Wry}; +use tauri::AppHandle; use crate::{emit_side_effect, models, render, response_err}; pub async fn send_http_request( - app_handle: AppHandle, - db: &Pool, + app_handle: &AppHandle, request: models::HttpRequest, response: &models::HttpResponse, environment: Option, @@ -28,7 +26,7 @@ pub async fn send_http_request( download_path: Option, ) -> Result { let environment_ref = environment.as_ref(); - let workspace = models::get_workspace(db, &request.workspace_id) + let workspace = models::get_workspace(app_handle, &request.workspace_id) .await .expect("Failed to get Workspace"); @@ -88,7 +86,7 @@ pub async fn send_http_request( let url = match Url::from_str(url_string.as_str()) { Ok(u) => u, Err(e) => { - return response_err(response, e.to_string(), app_handle.clone(), db).await; + return response_err(response, e.to_string(), app_handle).await; } }; @@ -293,7 +291,7 @@ pub async fn send_http_request( let sendable_req = match request_builder.build() { Ok(r) => r, Err(e) => { - return response_err(response, e.to_string(), app_handle.clone(), db).await; + return response_err(response, e.to_string(), app_handle).await; } }; @@ -362,11 +360,11 @@ pub async fn send_http_request( ); } - response = models::update_response_if_id(db, &response) + response = models::update_response_if_id(app_handle, &response) .await .expect("Failed to update response"); if !request.id.is_empty() { - emit_side_effect(app_handle.clone(), "upserted_model", &response); + emit_side_effect(app_handle, "upserted_model", &response); } // Copy response to download path, if specified @@ -397,7 +395,7 @@ pub async fn send_http_request( .collect::>(), ); cookie_jar.cookies = json_cookies; - match models::upsert_cookie_jar(db, &cookie_jar).await { + match models::upsert_cookie_jar(&app_handle, &cookie_jar).await { Ok(updated_jar) => { emit_side_effect(app_handle, "upserted_model", &updated_jar); } @@ -409,6 +407,6 @@ pub async fn send_http_request( Ok(response) } - Err(e) => response_err(response, e.to_string(), app_handle, db).await, + Err(e) => response_err(response, e.to_string(), app_handle).await, } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1621693e..5df21a48 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -78,7 +78,7 @@ pub struct CustomResponse { pub status_reason: Option<&'static str>, } -async fn migrate_db(app_handle: AppHandle, db: &Mutex>) -> Result<(), String> { +async fn migrate_db(app_handle: AppHandle, db: &Mutex>) -> Result<(), String> { let pool = &*db.lock().await; let p = app_handle .path_resolver() @@ -100,7 +100,7 @@ async fn cmd_grpc_reflect(endpoint: &str) -> Result, Stri #[tauri::command] async fn cmd_grpc_call_unary( request_id: &str, - app_handle: AppHandle, + app_handle: AppHandle, grpc_handle: State<'_, Mutex>, ) -> Result { let req = get_grpc_request(&app_handle, request_id) @@ -119,7 +119,7 @@ async fn cmd_grpc_call_unary( .await .map_err(|e| e.to_string())? }; - emit_side_effect(app_handle.clone(), "upserted_model", conn.clone()); + emit_side_effect(&app_handle, "upserted_model", conn.clone()); { let req = req.clone(); @@ -176,7 +176,7 @@ async fn cmd_grpc_call_unary( #[tauri::command] async fn cmd_grpc_client_streaming( request_id: &str, - app_handle: AppHandle, + app_handle: AppHandle, ) -> Result { let req = get_grpc_request(&app_handle, request_id) .await @@ -194,7 +194,7 @@ async fn cmd_grpc_client_streaming( .await .map_err(|e| e.to_string())? }; - emit_side_effect(app_handle.clone(), "upserted_model", conn.clone()); + emit_side_effect(&app_handle, "upserted_model", conn.clone()); { let conn = conn.clone(); @@ -369,7 +369,7 @@ async fn cmd_grpc_client_streaming( #[tauri::command] async fn cmd_grpc_streaming( request_id: &str, - app_handle: AppHandle, + app_handle: AppHandle, grpc_handle: State<'_, Mutex>, ) -> Result { let req = get_grpc_request(&app_handle, request_id) @@ -388,7 +388,7 @@ async fn cmd_grpc_streaming( .await .map_err(|e| e.to_string())? }; - emit_side_effect(app_handle.clone(), "upserted_model", conn.clone()); + emit_side_effect(&app_handle, "upserted_model", conn.clone()); { let conn = conn.clone(); @@ -565,7 +565,7 @@ async fn cmd_grpc_streaming( #[tauri::command] async fn cmd_grpc_server_streaming( request_id: &str, - app_handle: AppHandle, + app_handle: AppHandle, grpc_handle: State<'_, Mutex>, ) -> Result { let req = get_grpc_request(&app_handle, request_id) @@ -585,7 +585,7 @@ async fn cmd_grpc_server_streaming( .await .map_err(|e| e.to_string())? }; - emit_side_effect(app_handle.clone(), "upserted_model", conn.clone()); + emit_side_effect(&app_handle, "upserted_model", conn.clone()); { let req = req.clone(); @@ -734,15 +734,13 @@ async fn cmd_send_ephemeral_request( mut request: HttpRequest, environment_id: Option<&str>, cookie_jar_id: Option<&str>, - app_handle: AppHandle, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; let response = HttpResponse::new(); request.id = "".to_string(); let environment = match environment_id { Some(id) => Some( - get_environment(db, id) + get_environment(&app_handle, id) .await .expect("Failed to get environment"), ), @@ -750,7 +748,7 @@ async fn cmd_send_ephemeral_request( }; let cookie_jar = match cookie_jar_id { Some(id) => Some( - get_cookie_jar(db, id) + get_cookie_jar(&app_handle, id) .await .expect("Failed to get cookie jar"), ), @@ -759,8 +757,7 @@ async fn cmd_send_ephemeral_request( // let cookie_jar_id2 = cookie_jar_id.unwrap_or("").to_string(); send_http_request( - app_handle, - db, + &app_handle, request, &response, environment, @@ -773,12 +770,11 @@ async fn cmd_send_ephemeral_request( #[tauri::command] async fn cmd_filter_response( window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, response_id: &str, filter: &str, ) -> Result { - let db = &*db_state.lock().await; - let response = get_response(db, response_id) + let response = get_response(&app_handle, response_id) .await .expect("Failed to get response"); @@ -811,10 +807,9 @@ async fn cmd_filter_response( #[tauri::command] async fn cmd_import_data( window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, file_paths: Vec<&str>, ) -> Result { - let db = &*db_state.lock().await; let mut result: Option = None; let plugins = vec!["importer-yaak", "importer-insomnia", "importer-postman"]; for plugin_name in plugins { @@ -844,7 +839,7 @@ async fn cmd_import_data( info!("Importing resources"); for w in r.resources.workspaces { - let x = upsert_workspace(db, w) + let x = upsert_workspace(&app_handle, w) .await .expect("Failed to create workspace"); imported_resources.workspaces.push(x.clone()); @@ -852,7 +847,7 @@ async fn cmd_import_data( } for e in r.resources.environments { - let x = upsert_environment(db, e) + let x = upsert_environment(&app_handle, e) .await .expect("Failed to create environment"); imported_resources.environments.push(x.clone()); @@ -860,13 +855,15 @@ async fn cmd_import_data( } for f in r.resources.folders { - let x = upsert_folder(db, f).await.expect("Failed to create folder"); + let x = upsert_folder(&app_handle, f) + .await + .expect("Failed to create folder"); imported_resources.folders.push(x.clone()); info!("Imported folder: {}", x.name); } for r in r.resources.requests { - let x = upsert_http_request(db, r) + let x = upsert_http_request(&app_handle, r) .await .expect("Failed to create request"); imported_resources.requests.push(x.clone()); @@ -880,13 +877,11 @@ async fn cmd_import_data( #[tauri::command] async fn cmd_export_data( - app_handle: AppHandle, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, export_path: &str, workspace_id: &str, ) -> Result<(), String> { - let db = &*db_state.lock().await; - let export_data = get_workspace_export_resources(&app_handle, db, workspace_id).await; + let export_data = get_workspace_export_resources(&app_handle, workspace_id).await; let f = File::options() .create(true) .truncate(true) @@ -912,22 +907,19 @@ async fn cmd_export_data( #[tauri::command] async fn cmd_send_request( - app_handle: AppHandle, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, request_id: &str, environment_id: Option<&str>, cookie_jar_id: Option<&str>, download_dir: Option<&str>, ) -> Result { - let db = &*db_state.lock().await; - - let request = get_http_request(db, request_id) + let request = get_http_request(&app_handle, request_id) .await .expect("Failed to get request"); let environment = match environment_id { Some(id) => Some( - get_environment(db, id) + get_environment(&app_handle, id) .await .expect("Failed to get environment"), ), @@ -936,7 +928,7 @@ async fn cmd_send_request( let cookie_jar = match cookie_jar_id { Some(id) => Some( - get_cookie_jar(db, id) + get_cookie_jar(&app_handle, id) .await .expect("Failed to get cookie jar"), ), @@ -944,7 +936,7 @@ async fn cmd_send_request( }; let response = create_response( - db, + &app_handle, &request.id, 0, 0, @@ -966,11 +958,10 @@ async fn cmd_send_request( None }; - emit_side_effect(app_handle.clone(), "upserted_model", response.clone()); + emit_side_effect(&app_handle, "upserted_model", response.clone()); send_http_request( - app_handle, - db, + &app_handle, request.clone(), &response, environment, @@ -983,16 +974,15 @@ async fn cmd_send_request( async fn response_err( response: &HttpResponse, error: String, - app_handle: AppHandle, - db: &Pool, + app_handle: &AppHandle, ) -> Result { let mut response = response.clone(); response.elapsed = -1; response.error = Some(error.clone()); - response = update_response_if_id(db, &response) + response = update_response_if_id(&app_handle, &response) .await .expect("Failed to update response"); - emit_side_effect(app_handle, "upserted_model", &response); + emit_side_effect(&app_handle, "upserted_model", &response); Ok(response) } @@ -1022,19 +1012,18 @@ async fn cmd_track_event( async fn cmd_set_update_mode( update_mode: &str, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - cmd_set_key_value("app", "update_mode", update_mode, window, db_state).await + cmd_set_key_value("app", "update_mode", update_mode, window, app_handle).await } #[tauri::command] async fn cmd_get_key_value( namespace: &str, key: &str, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, ()> { - let db = &*db_state.lock().await; - let result = get_key_value_raw(db, namespace, key).await; + let result = get_key_value_raw(&app_handle, namespace, key).await; Ok(result) } @@ -1044,10 +1033,9 @@ async fn cmd_set_key_value( key: &str, value: &str, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let (key_value, created) = set_key_value_raw(db, namespace, key, value).await; + let (key_value, created) = set_key_value_raw(&app_handle, namespace, key, value).await; if created { emit_and_return(&window, "upserted_model", key_value) @@ -1060,10 +1048,9 @@ async fn cmd_set_key_value( async fn cmd_create_workspace( name: &str, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let created_workspace = upsert_workspace(db, Workspace::new(name.to_string())) + let created_workspace = upsert_workspace(&app_handle, Workspace::new(name.to_string())) .await .expect("Failed to create Workspace"); @@ -1074,11 +1061,9 @@ async fn cmd_create_workspace( async fn cmd_update_cookie_jar( cookie_jar: CookieJar, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - - let updated = upsert_cookie_jar(db, &cookie_jar) + let updated = upsert_cookie_jar(&app_handle, &cookie_jar) .await .expect("Failed to update cookie jar"); @@ -1088,11 +1073,10 @@ async fn cmd_update_cookie_jar( #[tauri::command] async fn cmd_delete_cookie_jar( window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, cookie_jar_id: &str, ) -> Result { - let db = &*db_state.lock().await; - let req = delete_cookie_jar(db, cookie_jar_id) + let req = delete_cookie_jar(&app_handle, cookie_jar_id) .await .expect("Failed to delete cookie jar"); emit_and_return(&window, "deleted_model", req) @@ -1103,11 +1087,10 @@ async fn cmd_create_cookie_jar( workspace_id: &str, name: &str, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; let created_cookie_jar = upsert_cookie_jar( - db, + &app_handle, &CookieJar { name: name.to_string(), workspace_id: workspace_id.to_string(), @@ -1126,11 +1109,10 @@ async fn cmd_create_environment( name: &str, variables: Vec, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; let created_environment = upsert_environment( - db, + &app_handle, Environment { workspace_id: workspace_id.to_string(), name: name.to_string(), @@ -1188,11 +1170,10 @@ async fn cmd_create_http_request( sort_priority: f64, folder_id: Option<&str>, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; let created_request = upsert_http_request( - db, + &app_handle, HttpRequest { workspace_id: workspace_id.to_string(), name: name.to_string(), @@ -1212,10 +1193,9 @@ async fn cmd_create_http_request( async fn cmd_duplicate_http_request( id: &str, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let request = duplicate_http_request(db, id) + let request = duplicate_http_request(&app_handle, id) .await .expect("Failed to duplicate http request"); emit_and_return(&window, "upserted_model", request) @@ -1225,10 +1205,9 @@ async fn cmd_duplicate_http_request( async fn cmd_update_workspace( workspace: Workspace, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let updated_workspace = upsert_workspace(db, workspace) + let updated_workspace = upsert_workspace(&app_handle, workspace) .await .expect("Failed to update request"); @@ -1239,10 +1218,9 @@ async fn cmd_update_workspace( async fn cmd_update_environment( environment: Environment, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let updated_environment = upsert_environment(db, environment) + let updated_environment = upsert_environment(&app_handle, environment) .await .expect("Failed to update environment"); @@ -1265,10 +1243,9 @@ async fn cmd_update_grpc_request( async fn cmd_update_http_request( request: HttpRequest, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let updated_request = upsert_http_request(db, request) + let updated_request = upsert_http_request(&app_handle, request) .await .expect("Failed to update request"); emit_and_return(&window, "upserted_model", updated_request) @@ -1277,11 +1254,10 @@ async fn cmd_update_http_request( #[tauri::command] async fn cmd_delete_grpc_request( window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, request_id: &str, ) -> Result { - let db = &*db_state.lock().await; - let req = delete_request(db, request_id) + let req = delete_request(&app_handle, request_id) .await .expect("Failed to delete request"); emit_and_return(&window, "deleted_model", req) @@ -1290,11 +1266,10 @@ async fn cmd_delete_grpc_request( #[tauri::command] async fn cmd_delete_http_request( window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, request_id: &str, ) -> Result { - let db = &*db_state.lock().await; - let req = delete_request(db, request_id) + let req = delete_request(&app_handle, request_id) .await .expect("Failed to delete request"); emit_and_return(&window, "deleted_model", req) @@ -1303,10 +1278,9 @@ async fn cmd_delete_http_request( #[tauri::command] async fn cmd_list_folders( workspace_id: &str, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, String> { - let db = &*db_state.lock().await; - list_folders(db, workspace_id) + list_folders(&app_handle, workspace_id) .await .map_err(|e| e.to_string()) } @@ -1318,11 +1292,10 @@ async fn cmd_create_folder( sort_priority: f64, folder_id: Option<&str>, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; let created_request = upsert_folder( - db, + &app_handle, Folder { workspace_id: workspace_id.to_string(), name: name.to_string(), @@ -1341,10 +1314,9 @@ async fn cmd_create_folder( async fn cmd_update_folder( folder: Folder, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let updated_folder = upsert_folder(db, folder) + let updated_folder = upsert_folder(&app_handle, folder) .await .expect("Failed to update request"); emit_and_return(&window, "upserted_model", updated_folder) @@ -1353,11 +1325,10 @@ async fn cmd_update_folder( #[tauri::command] async fn cmd_delete_folder( window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, folder_id: &str, ) -> Result { - let db = &*db_state.lock().await; - let req = delete_folder(db, folder_id) + let req = delete_folder(&app_handle, folder_id) .await .expect("Failed to delete folder"); emit_and_return(&window, "deleted_model", req) @@ -1366,11 +1337,10 @@ async fn cmd_delete_folder( #[tauri::command] async fn cmd_delete_environment( window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, environment_id: &str, ) -> Result { - let db = &*db_state.lock().await; - let req = delete_environment(db, environment_id) + let req = delete_environment(&app_handle, environment_id) .await .expect("Failed to delete environment"); emit_and_return(&window, "deleted_model", req) @@ -1379,10 +1349,9 @@ async fn cmd_delete_environment( #[tauri::command] async fn cmd_list_grpc_connections( request_id: &str, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, String> { - let db = &*db_state.lock().await; - list_grpc_connections(db, request_id) + list_grpc_connections(&app_handle, request_id) .await .map_err(|e| e.to_string()) } @@ -1390,10 +1359,9 @@ async fn cmd_list_grpc_connections( #[tauri::command] async fn cmd_list_grpc_messages( connection_id: &str, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, String> { - let db = &*db_state.lock().await; - list_grpc_messages(db, connection_id) + list_grpc_messages(&app_handle, connection_id) .await .map_err(|e| e.to_string()) } @@ -1401,10 +1369,9 @@ async fn cmd_list_grpc_messages( #[tauri::command] async fn cmd_list_grpc_requests( workspace_id: &str, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, String> { - let db = &*db_state.lock().await; - let requests = list_grpc_requests(db, workspace_id) + let requests = list_grpc_requests(&app_handle, workspace_id) .await .map_err(|e| e.to_string())?; Ok(requests) @@ -1413,10 +1380,9 @@ async fn cmd_list_grpc_requests( #[tauri::command] async fn cmd_list_http_requests( workspace_id: &str, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, String> { - let db = &*db_state.lock().await; - let requests = list_requests(db, workspace_id) + let requests = list_requests(&app_handle, workspace_id) .await .expect("Failed to find requests"); // .map_err(|e| e.to_string()) @@ -1426,10 +1392,9 @@ async fn cmd_list_http_requests( #[tauri::command] async fn cmd_list_environments( workspace_id: &str, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, String> { - let db = &*db_state.lock().await; - let environments = list_environments(db, workspace_id) + let environments = list_environments(&app_handle, workspace_id) .await .expect("Failed to find environments"); @@ -1437,19 +1402,17 @@ async fn cmd_list_environments( } #[tauri::command] -async fn cmd_get_settings(db_state: State<'_, Mutex>>) -> Result { - let db = &*db_state.lock().await; - Ok(get_or_create_settings(db).await) +async fn cmd_get_settings(app_handle: AppHandle) -> Result { + Ok(get_or_create_settings(&app_handle).await) } #[tauri::command] async fn cmd_update_settings( settings: Settings, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let updated_settings = update_settings(db, settings) + let updated_settings = update_settings(&app_handle, settings) .await .expect("Failed to update settings"); @@ -1457,52 +1420,43 @@ async fn cmd_update_settings( } #[tauri::command] -async fn cmd_get_folder( - id: &str, - db_state: State<'_, Mutex>>, -) -> Result { - let db = &*db_state.lock().await; - get_folder(db, id).await.map_err(|e| e.to_string()) +async fn cmd_get_folder(id: &str, app_handle: AppHandle) -> Result { + get_folder(&app_handle, id).await.map_err(|e| e.to_string()) } #[tauri::command] -async fn cmd_get_grpc_request(id: &str, app_handle: AppHandle) -> Result { +async fn cmd_get_grpc_request(id: &str, app_handle: AppHandle) -> Result { get_grpc_request(&app_handle, id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn cmd_get_http_request( - id: &str, - db_state: State<'_, Mutex>>, -) -> Result { - let db = &*db_state.lock().await; - get_http_request(db, id).await.map_err(|e| e.to_string()) +async fn cmd_get_http_request(id: &str, app_handle: AppHandle) -> Result { + get_http_request(&app_handle, id) + .await + .map_err(|e| e.to_string()) } #[tauri::command] -async fn cmd_get_cookie_jar( - id: &str, - db_state: State<'_, Mutex>>, -) -> Result { - let db = &*db_state.lock().await; - get_cookie_jar(db, id).await.map_err(|e| e.to_string()) +async fn cmd_get_cookie_jar(id: &str, app_handle: AppHandle) -> Result { + get_cookie_jar(&app_handle, id) + .await + .map_err(|e| e.to_string()) } #[tauri::command] async fn cmd_list_cookie_jars( workspace_id: &str, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, String> { - let db = &*db_state.lock().await; - let cookie_jars = list_cookie_jars(db, workspace_id) + let cookie_jars = list_cookie_jars(&app_handle, workspace_id) .await .expect("Failed to find cookie jars"); if cookie_jars.is_empty() { let cookie_jar = upsert_cookie_jar( - db, + &app_handle, &CookieJar { name: "Default".to_string(), workspace_id: workspace_id.to_string(), @@ -1518,31 +1472,26 @@ async fn cmd_list_cookie_jars( } #[tauri::command] -async fn cmd_get_environment( - id: &str, - db_state: State<'_, Mutex>>, -) -> Result { - let db = &*db_state.lock().await; - get_environment(db, id).await.map_err(|e| e.to_string()) +async fn cmd_get_environment(id: &str, app_handle: AppHandle) -> Result { + get_environment(&app_handle, id) + .await + .map_err(|e| e.to_string()) } #[tauri::command] -async fn cmd_get_workspace( - id: &str, - db_state: State<'_, Mutex>>, -) -> Result { - let db = &*db_state.lock().await; - get_workspace(db, id).await.map_err(|e| e.to_string()) +async fn cmd_get_workspace(id: &str, app_handle: AppHandle) -> Result { + get_workspace(&app_handle, id) + .await + .map_err(|e| e.to_string()) } #[tauri::command] async fn cmd_list_http_responses( request_id: &str, limit: Option, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result, String> { - let db = &*db_state.lock().await; - list_responses(db, request_id, limit) + list_responses(&app_handle, request_id, limit) .await .map_err(|e| e.to_string()) } @@ -1551,37 +1500,29 @@ async fn cmd_list_http_responses( async fn cmd_delete_response( id: &str, window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, ) -> Result { - let db = &*db_state.lock().await; - let response = delete_response(db, id) + let response = delete_response(&app_handle, id) .await .expect("Failed to delete response"); emit_and_return(&window, "deleted_model", response) } #[tauri::command] -async fn cmd_delete_all_responses( - request_id: &str, - db_state: State<'_, Mutex>>, -) -> Result<(), String> { - let db = &*db_state.lock().await; - delete_all_responses(db, request_id) +async fn cmd_delete_all_responses(request_id: &str, app_handle: AppHandle) -> Result<(), String> { + delete_all_responses(&app_handle, request_id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn cmd_list_workspaces( - db_state: State<'_, Mutex>>, -) -> Result, String> { - let db = &*db_state.lock().await; - let workspaces = list_workspaces(db) +async fn cmd_list_workspaces(app_handle: AppHandle) -> Result, String> { + let workspaces = list_workspaces(&app_handle) .await .expect("Failed to find workspaces"); if workspaces.is_empty() { let workspace = upsert_workspace( - db, + &app_handle, Workspace { name: "Yaak".to_string(), ..Default::default() @@ -1604,11 +1545,10 @@ async fn cmd_new_window(window: Window, url: &str) -> Result<(), String> { #[tauri::command] async fn cmd_delete_workspace( window: Window, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, workspace_id: &str, ) -> Result { - let db = &*db_state.lock().await; - let workspace = delete_workspace(db, workspace_id) + let workspace = delete_workspace(&app_handle, workspace_id) .await .expect("Failed to delete Workspace"); emit_and_return(&window, "deleted_model", workspace) @@ -1616,12 +1556,10 @@ async fn cmd_delete_workspace( #[tauri::command] async fn cmd_check_for_updates( - app_handle: AppHandle, - db_state: State<'_, Mutex>>, + app_handle: AppHandle, yaak_updater: State<'_, Mutex>, ) -> Result { - let db = &*db_state.lock().await; - let update_mode = get_update_mode(db).await; + let update_mode = get_update_mode(&app_handle).await; yaak_updater .lock() .await @@ -1694,7 +1632,8 @@ fn main() { .await .expect("Failed to migrate database"); app.manage(m); - let _ = cancel_pending_responses(&pool).await; + let h = app.handle(); + let _ = cancel_pending_responses(&h).await; }); Ok(()) @@ -1790,9 +1729,7 @@ fn main() { let h = app_handle.clone(); tauri::async_runtime::spawn(async move { - let db_state: State<'_, Mutex>> = h.state(); - let db = &*db_state.lock().await; - let info = analytics::track_launch_event(&h, db).await; + let info = analytics::track_launch_event(&h).await; info!("Launched Yaak {:?}", info); // Wait for window render and give a chance for the user to notice @@ -1811,9 +1748,7 @@ fn main() { // Run update check whenever window is focused tauri::async_runtime::spawn(async move { let val: State<'_, Mutex> = h.state(); - let db_state: State<'_, Mutex>> = h.state(); - let db = &*db_state.lock().await; - let update_mode = get_update_mode(&db).await; + let update_mode = get_update_mode(&h).await; _ = val.lock().await.check(&h, update_mode).await; }); } @@ -1833,7 +1768,7 @@ fn is_dev() -> bool { } } -fn create_window(handle: &AppHandle, url: Option<&str>) -> Window { +fn create_window(handle: &AppHandle, url: Option<&str>) -> Window { let app_menu = window_menu::os_default("Yaak".to_string().as_str()); let window_num = handle.windows().len(); let window_id = format!("wnd_{}", window_num); @@ -1934,12 +1869,12 @@ fn emit_and_return( } /// Emit an event to all windows, used for side-effects where there is no source window to attribute. This -fn emit_side_effect(app_handle: AppHandle, event: &str, payload: S) { +fn emit_side_effect(app_handle: &AppHandle, event: &str, payload: S) { app_handle.emit_all(event, &payload).unwrap(); } -async fn get_update_mode(db: &Pool) -> UpdateMode { - let settings = get_or_create_settings(db).await; +async fn get_update_mode(app_handle: &AppHandle) -> UpdateMode { + let settings = get_or_create_settings(app_handle).await; update_mode_from_str(settings.update_channel.as_str()) } diff --git a/src-tauri/src/models.rs b/src-tauri/src/models.rs index 41576d6a..7f70cce1 100644 --- a/src-tauri/src/models.rs +++ b/src-tauri/src/models.rs @@ -258,32 +258,32 @@ pub struct KeyValue { } pub async fn set_key_value_string( - db: &Pool, + app_handle: &AppHandle, namespace: &str, key: &str, value: &str, ) -> (KeyValue, bool) { let encoded = serde_json::to_string(value); - set_key_value_raw(db, namespace, key, &encoded.unwrap()).await + set_key_value_raw(app_handle, namespace, key, &encoded.unwrap()).await } pub async fn set_key_value_int( - db: &Pool, + app_handle: &AppHandle, namespace: &str, key: &str, value: i32, ) -> (KeyValue, bool) { let encoded = serde_json::to_string(&value); - set_key_value_raw(db, namespace, key, &encoded.unwrap()).await + set_key_value_raw(app_handle, namespace, key, &encoded.unwrap()).await } pub async fn get_key_value_string( - db: &Pool, + app_handle: &AppHandle, namespace: &str, key: &str, default: &str, ) -> String { - match get_key_value_raw(db, namespace, key).await { + match get_key_value_raw(app_handle, namespace, key).await { None => default.to_string(), Some(v) => { let result = serde_json::from_str(&v.value); @@ -298,8 +298,13 @@ pub async fn get_key_value_string( } } -pub async fn get_key_value_int(db: &Pool, namespace: &str, key: &str, default: i32) -> i32 { - match get_key_value_raw(db, namespace, key).await { +pub async fn get_key_value_int( + app_handle: &AppHandle, + namespace: &str, + key: &str, + default: i32, +) -> i32 { + match get_key_value_raw(app_handle, namespace, key).await { None => default.clone(), Some(v) => { let result = serde_json::from_str(&v.value); @@ -315,12 +320,13 @@ pub async fn get_key_value_int(db: &Pool, namespace: &str, key: &str, de } pub async fn set_key_value_raw( - db: &Pool, + app_handle: &AppHandle, namespace: &str, key: &str, value: &str, ) -> (KeyValue, bool) { - let existing = get_key_value_raw(db, namespace, key).await; + let db = get_db(app_handle).await; + let existing = get_key_value_raw(app_handle, namespace, key).await; sqlx::query!( r#" INSERT INTO key_values (namespace, key, value) @@ -332,17 +338,22 @@ pub async fn set_key_value_raw( key, value, ) - .execute(db) + .execute(&db) .await .expect("Failed to insert key value"); - let kv = get_key_value_raw(db, namespace, key) + let kv = get_key_value_raw(app_handle, namespace, key) .await .expect("Failed to get key value"); (kv, existing.is_none()) } -pub async fn get_key_value_raw(db: &Pool, namespace: &str, key: &str) -> Option { +pub async fn get_key_value_raw( + app_handle: &AppHandle, + namespace: &str, + key: &str, +) -> Option { + let db = get_db(app_handle).await; sqlx::query_as!( KeyValue, r#" @@ -353,12 +364,13 @@ pub async fn get_key_value_raw(db: &Pool, namespace: &str, key: &str) -> namespace, key, ) - .fetch_one(db) + .fetch_one(&db) .await .ok() } -pub async fn list_workspaces(db: &Pool) -> Result, sqlx::Error> { +pub async fn list_workspaces(app_handle: &AppHandle) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( Workspace, r#" @@ -369,11 +381,12 @@ pub async fn list_workspaces(db: &Pool) -> Result, sqlx:: FROM workspaces "#, ) - .fetch_all(db) + .fetch_all(&db) .await } -pub async fn get_workspace(db: &Pool, id: &str) -> Result { +pub async fn get_workspace(app_handle: &AppHandle, id: &str) -> Result { + let db = get_db(app_handle).await; sqlx::query_as!( Workspace, r#" @@ -385,12 +398,13 @@ pub async fn get_workspace(db: &Pool, id: &str) -> Result, id: &str) -> Result { - let workspace = get_workspace(db, id).await?; +pub async fn delete_workspace(app_handle: &AppHandle, id: &str) -> Result { + let db = get_db(app_handle).await; + let workspace = get_workspace(app_handle, id).await?; let _ = sqlx::query!( r#" DELETE FROM workspaces @@ -398,17 +412,18 @@ pub async fn delete_workspace(db: &Pool, id: &str) -> Result, id: &str) -> Result { +pub async fn get_cookie_jar(app_handle: &AppHandle, id: &str) -> Result { + let db = get_db(app_handle).await; sqlx::query_as!( CookieJar, r#" @@ -419,14 +434,15 @@ pub async fn get_cookie_jar(db: &Pool, id: &str) -> Result, + app_handle: &AppHandle, workspace_id: &str, ) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( CookieJar, r#" @@ -437,12 +453,13 @@ pub async fn list_cookie_jars( "#, workspace_id, ) - .fetch_all(db) + .fetch_all(&db) .await } -pub async fn delete_cookie_jar(db: &Pool, id: &str) -> Result { - let cookie_jar = get_cookie_jar(db, id).await?; +pub async fn delete_cookie_jar(app_handle: &AppHandle, id: &str) -> Result { + let cookie_jar = get_cookie_jar(app_handle, id).await?; + let db = get_db(app_handle).await; let _ = sqlx::query!( r#" @@ -451,7 +468,7 @@ pub async fn delete_cookie_jar(db: &Pool, id: &str) -> Result Result { - let db_state = app_handle.state::>>(); - let db = &*db_state.lock().await; - + let db = get_db(app_handle).await; sqlx::query_as!( GrpcRequest, r#" @@ -526,14 +541,15 @@ pub async fn get_grpc_request( "#, id, ) - .fetch_one(db) + .fetch_one(&db) .await } pub async fn list_grpc_requests( - db: &Pool, + app_handle: &AppHandle, workspace_id: &str, ) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( GrpcRequest, r#" @@ -545,7 +561,7 @@ pub async fn list_grpc_requests( "#, workspace_id, ) - .fetch_all(db) + .fetch_all(&db) .await } @@ -600,9 +616,10 @@ pub async fn get_grpc_connection( } pub async fn list_grpc_connections( - db: &Pool, + app_handle: &AppHandle, request_id: &str, ) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( GrpcConnection, r#" @@ -613,7 +630,7 @@ pub async fn list_grpc_connections( "#, request_id, ) - .fetch_all(db) + .fetch_all(&db) .await } @@ -677,9 +694,10 @@ pub async fn get_grpc_message( } pub async fn list_grpc_messages( - db: &Pool, + app_handle: &AppHandle, connection_id: &str, ) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( GrpcMessage, r#" @@ -691,12 +709,12 @@ pub async fn list_grpc_messages( "#, connection_id, ) - .fetch_all(db) + .fetch_all(&db) .await } pub async fn upsert_cookie_jar( - db: &Pool, + app_handle: &AppHandle, cookie_jar: &CookieJar, ) -> Result { let id = match cookie_jar.id.as_str() { @@ -704,6 +722,8 @@ pub async fn upsert_cookie_jar( _ => cookie_jar.id.to_string(), }; let trimmed_name = cookie_jar.name.trim(); + + let db = get_db(app_handle).await; sqlx::query!( r#" INSERT INTO cookie_jars ( @@ -720,16 +740,17 @@ pub async fn upsert_cookie_jar( trimmed_name, cookie_jar.cookies, ) - .execute(db) + .execute(&db) .await?; - get_cookie_jar(db, &id).await + get_cookie_jar(&app_handle, &id).await } pub async fn list_environments( - db: &Pool, + app_handle: &AppHandle, workspace_id: &str, ) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( Environment, r#" @@ -740,12 +761,16 @@ pub async fn list_environments( "#, workspace_id, ) - .fetch_all(db) + .fetch_all(&db) .await } -pub async fn delete_environment(db: &Pool, id: &str) -> Result { - let env = get_environment(db, id).await?; +pub async fn delete_environment( + app_handle: &AppHandle, + id: &str, +) -> Result { + let db = get_db(app_handle).await; + let env = get_environment(app_handle, id).await?; let _ = sqlx::query!( r#" DELETE FROM environments @@ -753,13 +778,14 @@ pub async fn delete_environment(db: &Pool, id: &str) -> Result) -> Result { +async fn get_settings(app_handle: &AppHandle) -> Result { + let db = get_db(app_handle).await; sqlx::query_as!( Settings, r#" @@ -769,32 +795,36 @@ async fn get_settings(db: &Pool) -> Result { WHERE id = 'default' "#, ) - .fetch_one(db) + .fetch_one(&db) .await } -pub async fn get_or_create_settings(db: &Pool) -> Settings { - if let Ok(settings) = get_settings(db).await { +pub async fn get_or_create_settings(app_handle: &AppHandle) -> Settings { + if let Ok(settings) = get_settings(app_handle).await { return settings; } + let db = get_db(app_handle).await; sqlx::query!( r#" INSERT INTO settings (id) VALUES ('default') "#, ) - .execute(db) + .execute(&db) .await .expect("Failed to insert settings"); - get_settings(db).await.expect("Failed to get settings") + get_settings(&app_handle) + .await + .expect("Failed to get settings") } pub async fn update_settings( - db: &Pool, + app_handle: &AppHandle, settings: Settings, ) -> Result { + let db = get_db(app_handle).await; sqlx::query!( r#" UPDATE settings SET ( @@ -805,13 +835,13 @@ pub async fn update_settings( settings.appearance, settings.update_channel ) - .execute(db) + .execute(&db) .await?; - get_settings(db).await + get_settings(app_handle).await } pub async fn upsert_environment( - db: &Pool, + app_handle: &AppHandle, environment: Environment, ) -> Result { let id = match environment.id.as_str() { @@ -819,6 +849,7 @@ pub async fn upsert_environment( _ => environment.id.to_string(), }; let trimmed_name = environment.name.trim(); + let db = get_db(app_handle).await; sqlx::query!( r#" INSERT INTO environments ( @@ -835,12 +866,13 @@ pub async fn upsert_environment( trimmed_name, environment.variables, ) - .execute(db) + .execute(&db) .await?; - get_environment(db, &id).await + get_environment(app_handle, &id).await } -pub async fn get_environment(db: &Pool, id: &str) -> Result { +pub async fn get_environment(app_handle: &AppHandle, id: &str) -> Result { + let db = get_db(app_handle).await; sqlx::query_as!( Environment, r#" @@ -852,11 +884,12 @@ pub async fn get_environment(db: &Pool, id: &str) -> Result, id: &str) -> Result { +pub async fn get_folder(app_handle: &AppHandle, id: &str) -> Result { + let db = get_db(app_handle).await; sqlx::query_as!( Folder, r#" @@ -867,14 +900,15 @@ pub async fn get_folder(db: &Pool, id: &str) -> Result, + app_handle: &AppHandle, workspace_id: &str, ) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( Folder, r#" @@ -885,12 +919,13 @@ pub async fn list_folders( "#, workspace_id, ) - .fetch_all(db) + .fetch_all(&db) .await } -pub async fn delete_folder(db: &Pool, id: &str) -> Result { - let env = get_folder(db, id).await?; +pub async fn delete_folder(app_handle: &AppHandle, id: &str) -> Result { + let env = get_folder(app_handle, id).await?; + let db = get_db(app_handle).await; let _ = sqlx::query!( r#" DELETE FROM folders @@ -898,19 +933,20 @@ pub async fn delete_folder(db: &Pool, id: &str) -> Result, r: Folder) -> Result { +pub async fn upsert_folder(app_handle: &AppHandle, r: Folder) -> Result { let id = match r.id.as_str() { "" => generate_id(Some("fl")), _ => r.id.to_string(), }; let trimmed_name = r.name.trim(); + let db = get_db(app_handle).await; sqlx::query!( r#" INSERT INTO folders ( @@ -929,23 +965,23 @@ pub async fn upsert_folder(db: &Pool, r: Folder) -> Result, + app_handle: &AppHandle, id: &str, ) -> Result { - let mut request = get_http_request(db, id).await?.clone(); + let mut request = get_http_request(app_handle, id).await?.clone(); request.id = "".to_string(); - upsert_http_request(db, request).await + upsert_http_request(app_handle, request).await } pub async fn upsert_http_request( - db: &Pool, + app_handle: &AppHandle, r: HttpRequest, ) -> Result { let id = match r.id.as_str() { @@ -956,6 +992,8 @@ pub async fn upsert_http_request( let auth_json = Json(r.authentication); let trimmed_name = r.name.trim(); + let db = get_db(app_handle).await; + sqlx::query!( r#" INSERT INTO http_requests ( @@ -991,16 +1029,17 @@ pub async fn upsert_http_request( headers_json, r.sort_priority, ) - .execute(db) + .execute(&db) .await?; - get_http_request(db, &id).await + get_http_request(app_handle, &id).await } pub async fn list_requests( - db: &Pool, + app_handle: &AppHandle, workspace_id: &str, ) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( HttpRequest, r#" @@ -1016,11 +1055,16 @@ pub async fn list_requests( "#, workspace_id, ) - .fetch_all(db) + .fetch_all(&db) .await } -pub async fn get_http_request(db: &Pool, id: &str) -> Result { +pub async fn get_http_request( + app_handle: &AppHandle, + id: &str, +) -> Result { + let db = get_db(app_handle).await; + sqlx::query_as!( HttpRequest, r#" @@ -1036,16 +1080,17 @@ pub async fn get_http_request(db: &Pool, id: &str) -> Result, id: &str) -> Result { - let req = get_http_request(db, id).await?; +pub async fn delete_request(app_handle: &AppHandle, id: &str) -> Result { + let req = get_http_request(app_handle, id).await?; // DB deletes will cascade but this will delete the files - delete_all_responses(db, id).await?; + delete_all_responses(app_handle, id).await?; + let db = get_db(app_handle).await; let _ = sqlx::query!( r#" DELETE FROM http_requests @@ -1053,7 +1098,7 @@ pub async fn delete_request(db: &Pool, id: &str) -> Result, id: &str) -> Result, + app_handle: &AppHandle, request_id: &str, elapsed: i64, elapsed_headers: i64, @@ -1074,9 +1119,10 @@ pub async fn create_response( version: Option<&str>, remote_addr: Option<&str>, ) -> Result { - let req = get_http_request(db, request_id).await?; + let req = get_http_request(app_handle, request_id).await?; let id = generate_id(Some("rp")); let headers_json = Json(headers); + let db = get_db(app_handle).await; sqlx::query!( r#" INSERT INTO http_responses ( @@ -1099,13 +1145,14 @@ pub async fn create_response( version, remote_addr, ) - .execute(db) + .execute(&db) .await?; - get_response(db, &id).await + get_response(app_handle, &id).await } -pub async fn cancel_pending_responses(db: &Pool) -> Result<(), sqlx::Error> { +pub async fn cancel_pending_responses(app_handle: &AppHandle) -> Result<(), sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query!( r#" UPDATE http_responses @@ -1113,24 +1160,24 @@ pub async fn cancel_pending_responses(db: &Pool) -> Result<(), sqlx::Err WHERE elapsed = 0; "#, ) - .execute(db) + .execute(&db) .await?; Ok(()) } pub async fn update_response_if_id( - db: &Pool, + app_handle: &AppHandle, response: &HttpResponse, ) -> Result { if response.id.is_empty() { Ok(response.clone()) } else { - update_response(db, response).await + update_response(app_handle, response).await } } pub async fn upsert_workspace( - db: &Pool, + app_handle: &AppHandle, workspace: Workspace, ) -> Result { let id = match workspace.id.as_str() { @@ -1138,6 +1185,8 @@ pub async fn upsert_workspace( _ => workspace.id.to_string(), }; let trimmed_name = workspace.name.trim(); + + let db = get_db(app_handle).await; sqlx::query!( r#" INSERT INTO workspaces ( @@ -1162,17 +1211,18 @@ pub async fn upsert_workspace( workspace.setting_follow_redirects, workspace.setting_validate_certificates, ) - .execute(db) + .execute(&db) .await?; - get_workspace(db, &id).await + get_workspace(app_handle, &id).await } pub async fn update_response( - db: &Pool, + app_handle: &AppHandle, response: &HttpResponse, ) -> Result { let headers_json = Json(&response.headers); + let db = get_db(app_handle).await; sqlx::query!( r#" UPDATE http_responses SET ( @@ -1193,12 +1243,13 @@ pub async fn update_response( response.remote_addr, response.id, ) - .execute(db) + .execute(&db) .await?; - get_response(db, &response.id).await + get_response(app_handle, &response.id).await } -pub async fn get_response(db: &Pool, id: &str) -> Result { +pub async fn get_response(app_handle: &AppHandle, id: &str) -> Result { + let db = get_db(app_handle).await; sqlx::query_as!( HttpResponse, r#" @@ -1212,16 +1263,17 @@ pub async fn get_response(db: &Pool, id: &str) -> Result, + app_handle: &AppHandle, request_id: &str, limit: Option, ) -> Result, sqlx::Error> { let limit_unwrapped = limit.unwrap_or_else(|| i64::MAX); + let db = get_db(app_handle).await; sqlx::query_as!( HttpResponse, r#" @@ -1238,14 +1290,15 @@ pub async fn list_responses( request_id, limit_unwrapped, ) - .fetch_all(db) + .fetch_all(&db) .await } pub async fn list_responses_by_workspace_id( - db: &Pool, + app_handle: &AppHandle, workspace_id: &str, ) -> Result, sqlx::Error> { + let db = get_db(app_handle).await; sqlx::query_as!( HttpResponse, r#" @@ -1260,12 +1313,15 @@ pub async fn list_responses_by_workspace_id( "#, workspace_id, ) - .fetch_all(db) + .fetch_all(&db) .await } -pub async fn delete_response(db: &Pool, id: &str) -> Result { - let resp = get_response(db, id).await?; +pub async fn delete_response( + app_handle: &AppHandle, + id: &str, +) -> Result { + let resp = get_response(app_handle, id).await?; // Delete the body file if it exists if let Some(p) = resp.body_path.clone() { @@ -1274,6 +1330,7 @@ pub async fn delete_response(db: &Pool, id: &str) -> Result, id: &str) -> Result, request_id: &str) -> Result<(), sqlx::Error> { - for r in list_responses(db, request_id, None).await? { - delete_response(db, &r.id).await?; +pub async fn delete_all_responses( + app_handle: &AppHandle, + request_id: &str, +) -> Result<(), sqlx::Error> { + for r in list_responses(app_handle, request_id, None).await? { + delete_response(app_handle, &r.id).await?; } Ok(()) } @@ -1322,10 +1382,9 @@ pub struct WorkspaceExportResources { pub async fn get_workspace_export_resources( app_handle: &AppHandle, - db: &Pool, workspace_id: &str, ) -> WorkspaceExport { - let workspace = get_workspace(db, workspace_id) + let workspace = get_workspace(app_handle, workspace_id) .await .expect("Failed to get workspace"); return WorkspaceExport { @@ -1334,13 +1393,13 @@ pub async fn get_workspace_export_resources( timestamp: chrono::Utc::now().naive_utc(), resources: WorkspaceExportResources { workspaces: vec![workspace], - environments: list_environments(db, workspace_id) + environments: list_environments(app_handle, workspace_id) .await .expect("Failed to get environments"), - folders: list_folders(db, workspace_id) + folders: list_folders(app_handle, workspace_id) .await .expect("Failed to get folders"), - requests: list_requests(db, workspace_id) + requests: list_requests(app_handle, workspace_id) .await .expect("Failed to get requests"), }, diff --git a/src-tauri/src/updates.rs b/src-tauri/src/updates.rs index 2e65321f..074d973b 100644 --- a/src-tauri/src/updates.rs +++ b/src-tauri/src/updates.rs @@ -1,8 +1,8 @@ use std::time::SystemTime; use log::info; -use tauri::{AppHandle, updater, Window, Wry}; use tauri::api::dialog; +use tauri::{updater, AppHandle, Window}; use crate::is_dev; @@ -27,14 +27,17 @@ impl YaakUpdater { } pub async fn force_check( &mut self, - app_handle: &AppHandle, + app_handle: &AppHandle, mode: UpdateMode, ) -> Result { self.last_update_check = SystemTime::now(); let update_mode = get_update_mode_str(mode); let enabled = !is_dev(); - info!("Checking for updates mode={} enabled={}", update_mode, enabled); + info!( + "Checking for updates mode={} enabled={}", + update_mode, enabled + ); if !enabled { return Ok(false); @@ -89,10 +92,11 @@ impl YaakUpdater { } pub async fn check( &mut self, - app_handle: &AppHandle, + app_handle: &AppHandle, mode: UpdateMode, ) -> Result { - let ignore_check = self.last_update_check.elapsed().unwrap().as_secs() < MAX_UPDATE_CHECK_SECONDS; + let ignore_check = + self.last_update_check.elapsed().unwrap().as_secs() < MAX_UPDATE_CHECK_SECONDS; if ignore_check { return Ok(false); } diff --git a/src-web/components/GrpcConnectionLayout.tsx b/src-web/components/GrpcConnectionLayout.tsx index e7259206..6df09f20 100644 --- a/src-web/components/GrpcConnectionLayout.tsx +++ b/src-web/components/GrpcConnectionLayout.tsx @@ -19,7 +19,6 @@ import { RadioDropdown } from './core/RadioDropdown'; import { Separator } from './core/Separator'; import { SplitLayout } from './core/SplitLayout'; import { HStack, VStack } from './core/Stacks'; -import { StatusTag } from './core/StatusTag'; import { GrpcEditor } from './GrpcEditor'; import { UrlBar } from './UrlBar'; @@ -276,6 +275,7 @@ export function GrpcConnectionLayout({ style }: Props) { ) : messages.length >= 0 ? ( (() => { return { ...style, - gridTemplate: vertical - ? ` + gridTemplate: + forceVertical || vertical + ? ` ' ${areaL.gridArea}' minmax(0,${1 - height}fr) ' ${areaD.gridArea}' 0 ' ${areaR.gridArea}' minmax(${minHeightPx}px,${height}fr) / 1fr ` - : ` + : ` ' ${areaL.gridArea} ${areaD.gridArea} ${areaR.gridArea}' minmax(0,1fr) / ${1 - width}fr 0 ${width}fr `,