Better reading of response body

This commit is contained in:
Gregory Schier
2025-01-20 14:12:49 -08:00
parent 8ad7ac0bef
commit 19403983b7
4 changed files with 28 additions and 8 deletions

5
src-tauri/Cargo.lock generated
View File

@@ -1379,9 +1379,9 @@ checksum = "4ef6b89e5b37196644d8796de5268852ff179b44e96276cf4290264843743bb7"
[[package]]
name = "encoding_rs"
version = "0.8.34"
version = "0.8.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59"
checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3"
dependencies = [
"cfg-if",
]
@@ -7492,6 +7492,7 @@ dependencies = [
"chrono",
"cocoa 0.26.0",
"datetime",
"encoding_rs",
"eventsource-client",
"hex_color",
"http",

View File

@@ -76,6 +76,7 @@ yaak-plugins = { workspace = true }
yaak-sse = { workspace = true }
yaak-sync = { path = "yaak-sync" }
yaak-templates = { path = "yaak-templates" }
encoding_rs = "0.8.35"
[workspace.dependencies]
yaak-models = { path = "yaak-models" }

16
src-tauri/src/encoding.rs Normal file
View File

@@ -0,0 +1,16 @@
use encoding_rs::SHIFT_JIS;
use tokio::fs;
use yaak_models::models::HttpResponse;
pub async fn read_response_body<'a>(
response: HttpResponse,
) -> Option<String> {
let body_path = match response.body_path {
None => return None,
Some(p) => p,
};
let body = fs::read(body_path).await.unwrap();
let (s, _, _) = SHIFT_JIS.decode(body.as_slice());
Some(s.to_string())
}

View File

@@ -2,6 +2,7 @@ extern crate core;
#[cfg(target_os = "macos")]
extern crate objc;
use crate::analytics::{AnalyticsAction, AnalyticsResource};
use crate::encoding::read_response_body;
use crate::grpc::metadata_to_map;
use crate::http_request::send_http_request;
use crate::notifications::YaakNotifier;
@@ -77,6 +78,7 @@ use yaak_templates::format::format_json;
use yaak_templates::{Parser, Tokens};
mod analytics;
mod encoding;
mod grpc;
mod http_request;
mod notifications;
@@ -796,7 +798,7 @@ async fn cmd_filter_response<R: Runtime>(
}
}
let body = read_to_string(response.body_path.unwrap()).await.unwrap();
let body = read_response_body(response).await.unwrap();
// TODO: Have plugins register their own content type (regex?)
plugin_manager
@@ -808,11 +810,11 @@ async fn cmd_filter_response<R: Runtime>(
#[tauri::command]
async fn cmd_get_sse_events(file_path: &str) -> Result<Vec<ServerSentEvent>, String> {
let body = fs::read(file_path).map_err(|e| e.to_string())?;
let mut p = EventParser::new();
p.process_bytes(body.into()).map_err(|e| e.to_string())?;
let mut event_parser = EventParser::new();
event_parser.process_bytes(body.into()).map_err(|e| e.to_string())?;
let mut events = Vec::new();
while let Some(e) = p.get_event() {
while let Some(e) = event_parser.get_event() {
if let SSE::Event(e) = e {
events.push(ServerSentEvent {
event_type: e.event_type,
@@ -2178,8 +2180,8 @@ async fn call_frontend<T: Serialize + Clone, R: Runtime>(
}
window.unlisten(event_id);
let foo = rx.borrow();
foo.clone()
let v = rx.borrow();
v.clone()
}
async fn handle_plugin_event<R: Runtime>(