diff --git a/crates-tauri/yaak-app-client/src/lib.rs b/crates-tauri/yaak-app-client/src/lib.rs index d2cf1494f..395173558 100644 --- a/crates-tauri/yaak-app-client/src/lib.rs +++ b/crates-tauri/yaak-app-client/src/lib.rs @@ -40,6 +40,7 @@ use yaak_models::models::{ CookieJar, Environment, GrpcConnection, GrpcConnectionState, GrpcEvent, GrpcEventType, HttpRequest, HttpResponse, HttpResponseState, Workspace, }; +use yaak_models::queries::any_request::AnyRequest; use yaak_models::util::{BatchUpsertResult, ImportDestination, ImportPlan, UpdateSource}; use yaak_plugins::events::{ Color, ErrorResponse, FilterResponse, InternalEvent, InternalEventPayload, PluginContext, @@ -334,7 +335,8 @@ async fn cmd_grpc_go( let conn = app_handle.with_tx(|tx| { // Capture the stored request, not the rendered one: what a restore should // put back is what the user typed - let version_id = tx.snapshot_request_for_send(&unrendered_request.id); + let version_id = + tx.snapshot_request_for_send(&AnyRequest::GrpcRequest(unrendered_request.clone())); tx.upsert_grpc_connection( &GrpcConnection { workspace_id: request.workspace_id.clone(), diff --git a/crates-tauri/yaak-app-client/src/ws_ext.rs b/crates-tauri/yaak-app-client/src/ws_ext.rs index a1dae13d5..a8a880cef 100644 --- a/crates-tauri/yaak-app-client/src/ws_ext.rs +++ b/crates-tauri/yaak-app-client/src/ws_ext.rs @@ -21,6 +21,7 @@ use yaak_models::models::{ HttpResponseHeader, WebsocketConnection, WebsocketConnectionState, WebsocketEvent, WebsocketEventType, }; +use yaak_models::queries::any_request::AnyRequest; use yaak_models::util::UpdateSource; use yaak_plugins::events::{CallHttpAuthenticationRequest, HttpHeader, RenderPurpose}; use yaak_plugins::template_callback::PluginTemplateCallback; @@ -175,7 +176,8 @@ pub async fn cmd_ws_connect( let connection = app_handle.with_tx(|tx| { // Capture the stored request, not the rendered one: what a restore should // put back is what the user typed - let version_id = tx.snapshot_request_for_send(&unrendered_request.id); + let version_id = + tx.snapshot_request_for_send(&AnyRequest::WebsocketRequest(unrendered_request.clone())); tx.upsert_websocket_connection( &WebsocketConnection { workspace_id: request.workspace_id.clone(), diff --git a/crates/yaak-models/src/queries/model_versions.rs b/crates/yaak-models/src/queries/model_versions.rs index b8b443da8..95d1b466b 100644 --- a/crates/yaak-models/src/queries/model_versions.rs +++ b/crates/yaak-models/src/queries/model_versions.rs @@ -104,30 +104,41 @@ impl<'a> WriteDb<'a> { self.snapshot_request(&self.get_any_request(request_id)?, reason) } - /// What every send calls: capture the request, and don't make a fuss. + /// What a send calls when all it has is the request's id: capture the + /// stored row, and don't make a fuss. /// - /// Takes an id rather than a request because the request a send holds is - /// not always the stored one: a plugin can send a request it has already - /// rendered, and a version of that would hold literal values, secrets - /// included, where the user wrote templates. The stored row is what a - /// restore should put back, so that is what gets captured. - /// - /// A send is not worth failing over history that couldn't be written, and - /// a request with no id or no stored row is ephemeral and has nothing to - /// version. Either way the response just has no version to offer. - pub fn snapshot_request_for_send(&self, request_id: &str) -> Option { + /// For sends whose request may not be the stored one: a plugin can send a + /// request it has already rendered, and a version of that would hold + /// literal values, secrets included, where the user wrote templates. The + /// stored row is what a restore should put back. A request with no id or + /// no stored row is ephemeral and has nothing to version. + pub fn snapshot_stored_request_for_send(&self, request_id: &str) -> Option { if request_id.is_empty() { return None; } - let stored = match self.get_any_request(request_id) { - Ok(stored) => stored, - Err(Error::ModelNotFound(_)) => return None, + match self.get_any_request(request_id) { + Ok(stored) => self.snapshot_request_for_send(&stored), + Err(Error::ModelNotFound(_)) => None, Err(err) => { warn!("Failed to load request to snapshot before send: {err}"); - return None; + None } - }; - match self.snapshot_request(&stored, ModelVersionReason::Send) { + } + } + + /// What a send calls with the stored request it loaded: capture it, and + /// don't make a fuss. + /// + /// The caller must pass the request as stored, never a resolved or rendered + /// copy; with only an id, use [`Self::snapshot_stored_request_for_send`]. + /// A send is not worth failing over history that couldn't be written, and + /// a request with no id is ephemeral and has nothing to version. Either way + /// the response just has no version to offer. + pub fn snapshot_request_for_send(&self, request: &AnyRequest) -> Option { + if request.id().is_empty() { + return None; + } + match self.snapshot_request(request, ModelVersionReason::Send) { Ok(version) => Some(version.id), Err(err) => { warn!("Failed to snapshot request before send: {err}"); diff --git a/crates/yaak/src/send.rs b/crates/yaak/src/send.rs index 8a0278399..43925c7b2 100644 --- a/crates/yaak/src/send.rs +++ b/crates/yaak/src/send.rs @@ -453,7 +453,9 @@ fn snapshot_request_for_send(query_manager: &QueryManager, request_id: &str) -> return None; } query_manager - .with_tx(|tx| Ok::<_, yaak_models::error::Error>(tx.snapshot_request_for_send(request_id))) + .with_tx(|tx| { + Ok::<_, yaak_models::error::Error>(tx.snapshot_stored_request_for_send(request_id)) + }) .unwrap_or_else(|err| { warn!("Failed to snapshot request before send: {err}"); None