Request body is now an object

This commit is contained in:
Gregory Schier
2023-11-12 11:16:12 -08:00
parent ef23a85577
commit 758154fa14
16 changed files with 73 additions and 54 deletions

View File

@@ -1,6 +1,6 @@
{
"db_name": "SQLite",
"query": "\n SELECT\n id,\n model,\n workspace_id,\n folder_id,\n created_at,\n updated_at,\n name,\n url,\n method,\n body,\n body_type,\n authentication AS \"authentication!: Json<HashMap<String, JsonValue>>\",\n authentication_type,\n sort_priority,\n headers AS \"headers!: sqlx::types::Json<Vec<HttpRequestHeader>>\"\n FROM http_requests\n WHERE workspace_id = ?\n ",
"query": "\n SELECT\n id,\n model,\n workspace_id,\n folder_id,\n created_at,\n updated_at,\n name,\n url,\n method,\n body AS \"body!: Json<HashMap<String, JsonValue>>\",\n body_type,\n authentication AS \"authentication!: Json<HashMap<String, JsonValue>>\",\n authentication_type,\n sort_priority,\n headers AS \"headers!: sqlx::types::Json<Vec<HttpRequestHeader>>\"\n FROM http_requests\n WHERE workspace_id = ?\n ",
"describe": {
"columns": [
{
@@ -49,7 +49,7 @@
"type_info": "Text"
},
{
"name": "body",
"name": "body!: Json<HashMap<String, JsonValue>>",
"ordinal": 9,
"type_info": "Text"
},
@@ -92,7 +92,7 @@
false,
false,
false,
true,
false,
true,
false,
true,
@@ -100,5 +100,5 @@
false
]
},
"hash": "a558e182f40286fe52bed5f03b2dc367b7229ab6bd9cda0a7ce219a438ccd5fd"
"hash": "55e4e8b66c18f85d17ada00b302720e5dcd35ec4288006e4a556448d59b63952"
}

View File

@@ -1,6 +1,6 @@
{
"db_name": "SQLite",
"query": "\n SELECT\n id,\n model,\n workspace_id,\n folder_id,\n created_at,\n updated_at,\n name,\n url,\n method,\n body,\n body_type,\n authentication AS \"authentication!: Json<HashMap<String, JsonValue>>\",\n authentication_type,\n sort_priority,\n headers AS \"headers!: sqlx::types::Json<Vec<HttpRequestHeader>>\"\n FROM http_requests\n WHERE id = ?\n ",
"query": "\n SELECT\n id,\n model,\n workspace_id,\n folder_id,\n created_at,\n updated_at,\n name,\n url,\n method,\n body AS \"body!: Json<HashMap<String, JsonValue>>\",\n body_type,\n authentication AS \"authentication!: Json<HashMap<String, JsonValue>>\",\n authentication_type,\n sort_priority,\n headers AS \"headers!: sqlx::types::Json<Vec<HttpRequestHeader>>\"\n FROM http_requests\n WHERE id = ?\n ",
"describe": {
"columns": [
{
@@ -49,7 +49,7 @@
"type_info": "Text"
},
{
"name": "body",
"name": "body!: Json<HashMap<String, JsonValue>>",
"ordinal": 9,
"type_info": "Text"
},
@@ -92,7 +92,7 @@
false,
false,
false,
true,
false,
true,
false,
true,
@@ -100,5 +100,5 @@
false
]
},
"hash": "854536c80af3f86bb9a63b8ce059ad724374b545cb23481bb3b2ce07d7414220"
"hash": "ae31827b9576ffba83a9de05e30688df3c83e145860f8dd608410a9a9254659d"
}

View File

@@ -0,0 +1,16 @@
-- Rename old column to backup name
ALTER TABLE http_requests
RENAME COLUMN body TO body_old;
-- Create desired new body column
ALTER TABLE http_requests
ADD COLUMN body TEXT NOT NULL DEFAULT '{}';
-- Copy data from old to new body, in new JSON format
UPDATE http_requests
SET body = CASE WHEN body_old IS NULL THEN '{}' ELSE JSON_OBJECT('text', body_old) END
WHERE TRUE;
-- Drop old column
ALTER TABLE http_requests
DROP COLUMN body_old;

View File

@@ -17,7 +17,7 @@ use base64::Engine;
use fern::colors::ColoredLevelConfig;
use http::header::{HeaderName, ACCEPT, USER_AGENT};
use http::{HeaderMap, HeaderValue, Method};
use log::info;
use log::{info, warn};
use rand::random;
use reqwest::redirect::Policy;
use serde::Serialize;
@@ -182,17 +182,23 @@ async fn actually_send_request(
let m = Method::from_bytes(request.method.to_uppercase().as_bytes())
.expect("Failed to create method");
let builder = client.request(m, url_string.to_string()).headers(headers);
let sendable_req_result = match (request.body, request.body_type) {
(Some(raw_body), Some(_)) => {
let body = render::render(&raw_body, &workspace, environment_ref);
builder.body(body).build()
let mut request_builder = client.request(m, url_string.to_string()).headers(headers);
if let Some(t) = &request.body_type {
let empty_value = &serde_json::to_value("").unwrap();
let b = request.body.0;
if t == "basic" {
let raw_text = b.get("text").unwrap_or(empty_value).as_str().unwrap_or("");
let body = render::render(raw_text, &workspace, environment_ref);
request_builder = request_builder.body(body);
} else {
warn!("Unsupported body type: {}", t);
}
_ => builder.build(),
};
}
let sendable_req = match sendable_req_result {
let sendable_req = match request_builder.build() {
Ok(r) => r,
Err(e) => {
return response_err(response, e.to_string(), app_handle, pool).await;
@@ -648,9 +654,11 @@ async fn list_requests(
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
) -> Result<Vec<models::HttpRequest>, String> {
let pool = &*db_instance.lock().await;
models::find_requests(workspace_id, pool)
let requests = models::find_requests(workspace_id, pool)
.await
.map_err(|e| e.to_string())
.expect("Failed to find requests");
// .map_err(|e| e.to_string())
Ok(requests)
}
#[tauri::command]

View File

@@ -72,7 +72,7 @@ pub struct HttpRequest {
pub url: String,
#[serde(default = "default_http_request_method")]
pub method: String,
pub body: Option<String>,
pub body: Json<HashMap<String, JsonValue>>,
pub body_type: Option<String>,
pub authentication: Json<HashMap<String, JsonValue>>,
pub authentication_type: Option<String>,
@@ -497,7 +497,7 @@ pub async fn find_requests(
name,
url,
method,
body,
body AS "body!: Json<HashMap<String, JsonValue>>",
body_type,
authentication AS "authentication!: Json<HashMap<String, JsonValue>>",
authentication_type,
@@ -526,7 +526,7 @@ pub async fn get_request(id: &str, pool: &Pool<Sqlite>) -> Result<HttpRequest, s
name,
url,
method,
body,
body AS "body!: Json<HashMap<String, JsonValue>>",
body_type,
authentication AS "authentication!: Json<HashMap<String, JsonValue>>",
authentication_type,