More response info

This commit is contained in:
Gregory Schier
2024-01-28 16:02:49 -08:00
parent fbc878dbe5
commit 959841fb22
16 changed files with 230 additions and 89 deletions

View File

@@ -1,5 +1,5 @@
use std::fs;
use std::fs::{create_dir_all, File};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::str::FromStr;
@@ -7,13 +7,13 @@ use std::sync::Arc;
use std::time::Duration;
use base64::Engine;
use http::header::{ACCEPT, USER_AGENT};
use http::{HeaderMap, HeaderName, HeaderValue, Method};
use http::header::{ACCEPT, USER_AGENT};
use log::{error, info, warn};
use reqwest::redirect::Policy;
use reqwest::{multipart, Url};
use sqlx::types::{Json, JsonValue};
use reqwest::redirect::Policy;
use sqlx::{Pool, Sqlite};
use sqlx::types::{Json, JsonValue};
use tauri::{AppHandle, Wry};
use crate::{emit_side_effect, models, render, response_err};
@@ -27,7 +27,6 @@ pub async fn send_http_request(
pool: &Pool<Sqlite>,
download_path: Option<PathBuf>,
) -> Result<models::HttpResponse, String> {
let start = std::time::Instant::now();
let environment_ref = environment.as_ref();
let workspace = models::get_workspace(&request.workspace_id, pool)
.await
@@ -298,11 +297,13 @@ pub async fn send_http_request(
}
};
let start = std::time::Instant::now();
let raw_response = client.execute(sendable_req).await;
match raw_response {
Ok(v) => {
let mut response = response.clone();
response.elapsed_headers = start.elapsed().as_millis() as i64;
let response_headers = v.headers().clone();
response.status = v.status().as_u16() as i64;
response.status_reason = v.status().canonical_reason().map(|s| s.to_string());
@@ -316,8 +317,25 @@ pub async fn send_http_request(
.collect(),
);
response.url = v.url().to_string();
response.remote_addr = v.remote_addr().map(|a| a.to_string());
response.version = match v.version() {
http::Version::HTTP_09 => Some("HTTP/0.9".to_string()),
http::Version::HTTP_10 => Some("HTTP/1.0".to_string()),
http::Version::HTTP_11 => Some("HTTP/1.1".to_string()),
http::Version::HTTP_2 => Some("HTTP/2".to_string()),
http::Version::HTTP_3 => Some("HTTP/3".to_string()),
_ => None,
};
let content_length = v.content_length();
let body_bytes = v.bytes().await.expect("Failed to get body").to_vec();
response.content_length = Some(body_bytes.len() as i64);
response.elapsed = start.elapsed().as_millis() as i64;
// Use content length if available, otherwise use body length
response.content_length = match content_length {
Some(l) => Some(l as i64),
None => Some(body_bytes.len() as i64),
};
{
// Write body to FS
@@ -344,7 +362,6 @@ pub async fn send_http_request(
);
}
response.elapsed = start.elapsed().as_millis() as i64;
response = models::update_response_if_id(&response, pool)
.await
.expect("Failed to update response");

View File

@@ -10,7 +10,7 @@ extern crate objc;
use std::collections::HashMap;
use std::env::current_dir;
use std::fs::{create_dir_all, File, read_to_string};
use std::fs::{create_dir_all, read_to_string, File};
use std::process::exit;
use fern::colors::ColoredLevelConfig;
@@ -18,13 +18,13 @@ use log::{debug, error, info, warn};
use rand::random;
use serde::Serialize;
use serde_json::{json, Value};
use sqlx::{Pool, Sqlite, SqlitePool};
use sqlx::migrate::Migrator;
use sqlx::types::Json;
use tauri::{AppHandle, RunEvent, State, Window, WindowUrl, Wry};
use tauri::{Manager, WindowEvent};
use sqlx::{Pool, Sqlite, SqlitePool};
#[cfg(target_os = "macos")]
use tauri::TitleBarStyle;
use tauri::{AppHandle, RunEvent, State, Window, WindowUrl, Wry};
use tauri::{Manager, WindowEvent};
use tauri_plugin_log::{fern, LogTarget};
use tauri_plugin_window_state::{StateFlags, WindowExt};
use tokio::sync::Mutex;
@@ -292,9 +292,22 @@ async fn send_request(
None => None,
};
let response = models::create_response(&request.id, 0, "", 0, None, None, None, vec![], pool)
.await
.expect("Failed to create response");
let response = models::create_response(
&request.id,
0,
0,
"",
0,
None,
None,
None,
vec![],
None,
None,
pool,
)
.await
.expect("Failed to create response");
let download_path = if let Some(p) = download_dir {
Some(std::path::Path::new(p).to_path_buf())

View File

@@ -171,7 +171,10 @@ pub struct HttpResponse {
pub error: Option<String>,
pub url: String,
pub content_length: Option<i64>,
pub version: Option<String>,
pub elapsed: i64,
pub elapsed_headers: i64,
pub remote_addr: Option<String>,
pub status: i64,
pub status_reason: Option<String>,
pub body_path: Option<String>,
@@ -851,15 +854,19 @@ pub async fn delete_request(id: &str, pool: &Pool<Sqlite>) -> Result<HttpRequest
Ok(req)
}
#[allow(clippy::too_many_arguments)]
pub async fn create_response(
request_id: &str,
elapsed: i64,
elapsed_headers: i64,
url: &str,
status: i64,
status_reason: Option<&str>,
content_length: Option<i64>,
body_path: Option<&str>,
headers: Vec<HttpResponseHeader>,
version: Option<&str>,
remote_addr: Option<&str>,
pool: &Pool<Sqlite>,
) -> Result<HttpResponse, sqlx::Error> {
let req = get_request(request_id, pool).await?;
@@ -872,25 +879,31 @@ pub async fn create_response(
request_id,
workspace_id,
elapsed,
elapsed_headers,
url,
status,
status_reason,
content_length,
body_path,
headers
headers,
version,
remote_addr
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
"#,
id,
request_id,
req.workspace_id,
elapsed,
elapsed_headers,
url,
status,
status_reason,
content_length,
body_path,
headers_json,
version,
remote_addr,
)
.execute(pool)
.await?;
@@ -975,6 +988,7 @@ pub async fn update_response(
r#"
UPDATE http_responses SET (
elapsed,
elapsed_headers,
url,
status,
status_reason,
@@ -982,10 +996,13 @@ pub async fn update_response(
body_path,
error,
headers,
version,
remote_addr,
updated_at
) = (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) WHERE id = ?;
) = (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) WHERE id = ?;
"#,
response.elapsed,
response.elapsed_headers,
response.url,
response.status,
response.status_reason,
@@ -993,6 +1010,8 @@ pub async fn update_response(
response.body_path,
response.error,
headers_json,
response.version,
response.remote_addr,
response.id,
)
.execute(pool)
@@ -1004,8 +1023,10 @@ pub async fn get_response(id: &str, pool: &Pool<Sqlite>) -> Result<HttpResponse,
sqlx::query_as!(
HttpResponse,
r#"
SELECT id, model, workspace_id, request_id, updated_at, created_at, url,
status, status_reason, content_length, body_path, elapsed, error,
SELECT
id, model, workspace_id, request_id, updated_at, created_at, url, status,
status_reason, content_length, body_path, elapsed, elapsed_headers, error,
version, remote_addr,
headers AS "headers!: sqlx::types::Json<Vec<HttpResponseHeader>>"
FROM http_responses
WHERE id = ?
@@ -1021,15 +1042,14 @@ pub async fn find_responses(
limit: Option<i64>,
pool: &Pool<Sqlite>,
) -> Result<Vec<HttpResponse>, sqlx::Error> {
let limit_unwrapped = match limit {
Some(l) => l,
None => i64::MAX,
};
let limit_unwrapped = limit.unwrap_or_else(|| i64::MAX);
sqlx::query_as!(
HttpResponse,
r#"
SELECT id, model, workspace_id, request_id, updated_at, created_at, url,
status, status_reason, content_length, body_path, elapsed, error,
SELECT
id, model, workspace_id, request_id, updated_at, created_at, url, status,
status_reason, content_length, body_path, elapsed, elapsed_headers, error,
version, remote_addr,
headers AS "headers!: sqlx::types::Json<Vec<HttpResponseHeader>>"
FROM http_responses
WHERE request_id = ?
@@ -1050,8 +1070,10 @@ pub async fn find_responses_by_workspace_id(
sqlx::query_as!(
HttpResponse,
r#"
SELECT id, model, workspace_id, request_id, updated_at, created_at, url,
status, status_reason, content_length, body_path, elapsed, error,
SELECT
id, model, workspace_id, request_id, updated_at, created_at, url, status,
status_reason, content_length, body_path, elapsed, elapsed_headers, error,
version, remote_addr,
headers AS "headers!: sqlx::types::Json<Vec<HttpResponseHeader>>"
FROM http_responses
WHERE workspace_id = ?