mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-24 20:34:05 +02:00
Import from a URL in the Import Data dialog (#551)
This commit is contained in:
+3
-1
File diff suppressed because one or more lines are too long
@@ -5,6 +5,7 @@ use std::fs::read_to_string;
|
||||
use std::io::ErrorKind;
|
||||
use tauri::{Manager, Runtime, WebviewWindow};
|
||||
use yaak::import::{self, ImportDataParams};
|
||||
use yaak_api::{ApiClientKind, yaak_api_client};
|
||||
use yaak_core::WorkspaceContext;
|
||||
use yaak_models::util::BatchUpsertResult;
|
||||
use yaak_plugins::manager::PluginManager;
|
||||
@@ -13,10 +14,25 @@ use yaak_tauri_utils::window::WorkspaceWindowTrait;
|
||||
pub(crate) async fn import_data<R: Runtime>(
|
||||
window: &WebviewWindow<R>,
|
||||
file_path: &str,
|
||||
) -> Result<BatchUpsertResult> {
|
||||
let contents = read_import_file(file_path)?;
|
||||
import_contents(window, &contents).await
|
||||
}
|
||||
|
||||
pub(crate) async fn import_url<R: Runtime>(
|
||||
window: &WebviewWindow<R>,
|
||||
url: &str,
|
||||
) -> Result<BatchUpsertResult> {
|
||||
let contents = fetch_import_url(window, url).await?;
|
||||
import_contents(window, &contents).await
|
||||
}
|
||||
|
||||
async fn import_contents<R: Runtime>(
|
||||
window: &WebviewWindow<R>,
|
||||
contents: &str,
|
||||
) -> Result<BatchUpsertResult> {
|
||||
let plugin_manager = window.state::<PluginManager>();
|
||||
let query_manager = window.db_manager();
|
||||
let file = read_import_file(file_path)?;
|
||||
let plugin_context = window.plugin_context();
|
||||
let workspace_context = WorkspaceContext {
|
||||
workspace_id: window.workspace_id(),
|
||||
@@ -30,11 +46,57 @@ pub(crate) async fn import_data<R: Runtime>(
|
||||
plugin_manager: &plugin_manager,
|
||||
plugin_context: &plugin_context,
|
||||
workspace_context,
|
||||
contents: &file,
|
||||
contents,
|
||||
})
|
||||
.await?)
|
||||
}
|
||||
|
||||
/// Download an importable document (OpenAPI, Postman, Insomnia, …) so it can be fed to the same
|
||||
/// pipeline as a file on disk.
|
||||
///
|
||||
/// This uses Yaak's own API client, which follows the OS proxy but not the workspace's proxy,
|
||||
/// client certificate, or certificate-validation settings. Requests are unauthenticated, so
|
||||
/// specs behind auth must still be downloaded manually and imported as a file.
|
||||
async fn fetch_import_url<R: Runtime>(window: &WebviewWindow<R>, url: &str) -> Result<String> {
|
||||
let url = normalize_import_url(url)?;
|
||||
let app_version = window.app_handle().package_info().version.to_string();
|
||||
let response = yaak_api_client(ApiClientKind::App, &app_version)?
|
||||
.get(&url)
|
||||
// The API client defaults to JSON, but specs are just as often YAML
|
||||
.header("Accept", "*/*")
|
||||
.send()
|
||||
.await
|
||||
.map_err(|err| Error::GenericError(format!("Failed to fetch {url}: {err}")))?;
|
||||
|
||||
let status = response.status();
|
||||
if !status.is_success() {
|
||||
return Err(Error::GenericError(format!("Failed to fetch {url}: responded with {status}")));
|
||||
}
|
||||
|
||||
response
|
||||
.text()
|
||||
.await
|
||||
.map_err(|err| Error::GenericError(format!("Failed to read response from {url}: {err}")))
|
||||
}
|
||||
|
||||
fn normalize_import_url(url: &str) -> Result<String> {
|
||||
let url = url.trim();
|
||||
if url.is_empty() {
|
||||
return Err(Error::GenericError("Import URL must not be empty".to_string()));
|
||||
}
|
||||
|
||||
if url.starts_with("http://") || url.starts_with("https://") {
|
||||
return Ok(url.to_string());
|
||||
}
|
||||
|
||||
match url.split_once("://") {
|
||||
Some((scheme, _)) => {
|
||||
Err(Error::GenericError(format!("Import URL must be http or https, but got {scheme}")))
|
||||
}
|
||||
None => Ok(format!("https://{url}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_import_file(file_path: &str) -> Result<String> {
|
||||
read_to_string(file_path).map_err(|err| {
|
||||
if err.kind() == ErrorKind::InvalidData {
|
||||
@@ -71,4 +133,22 @@ mod tests {
|
||||
|
||||
remove_file(path).expect("remove binary fixture");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_import_url_defaults_to_https() {
|
||||
assert_eq!(
|
||||
normalize_import_url(" example.com/openapi.yaml ").unwrap(),
|
||||
"https://example.com/openapi.yaml"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize_import_url("http://example.com/openapi.yaml").unwrap(),
|
||||
"http://example.com/openapi.yaml"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_import_url_rejects_other_schemes() {
|
||||
assert!(normalize_import_url("file:///tmp/openapi.yaml").is_err());
|
||||
assert!(normalize_import_url(" ").is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::error::Error::GenericError;
|
||||
use crate::error::Result;
|
||||
use crate::grpc::{build_metadata, metadata_to_map, resolve_grpc_request};
|
||||
use crate::http_request::{resolve_http_request, send_http_request};
|
||||
use crate::import::import_data;
|
||||
use crate::import::{import_data, import_url};
|
||||
use crate::models_ext::{BlobManagerExt, QueryManagerExt};
|
||||
use crate::notifications::YaakNotifier;
|
||||
use crate::render::{render_grpc_request, render_json_value, render_template};
|
||||
@@ -1165,6 +1165,13 @@ async fn cmd_import_data<R: Runtime>(
|
||||
import_data(&window, file_path).await
|
||||
}
|
||||
|
||||
async fn cmd_import_url<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
url: &str,
|
||||
) -> YaakResult<BatchUpsertResult> {
|
||||
import_url(&window, url).await
|
||||
}
|
||||
|
||||
async fn cmd_http_request_actions<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
plugin_manager: State<'_, PluginManager>,
|
||||
|
||||
@@ -320,6 +320,13 @@ pub(crate) struct CmdImportDataReq {
|
||||
pub file_path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export, export_to = "gen_rpc.ts")]
|
||||
pub(crate) struct CmdImportUrlReq {
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, TS)]
|
||||
#[ts(export, export_to = "gen_rpc.ts")]
|
||||
pub(crate) struct CmdHttpRequestActionsReq {}
|
||||
@@ -1026,6 +1033,10 @@ async fn cmd_import_data<R: Runtime>(ctx: ClientCtx<R>, req: CmdImportDataReq) -
|
||||
Ok(crate::cmd_import_data(ctx.window.clone(), &req.file_path).await?)
|
||||
}
|
||||
|
||||
async fn cmd_import_url<R: Runtime>(ctx: ClientCtx<R>, req: CmdImportUrlReq) -> Result<BatchUpsertResult> {
|
||||
Ok(crate::cmd_import_url(ctx.window.clone(), &req.url).await?)
|
||||
}
|
||||
|
||||
async fn cmd_http_request_actions<R: Runtime>(ctx: ClientCtx<R>, _req: CmdHttpRequestActionsReq) -> Result<Vec<GetHttpRequestActionsResponse>> {
|
||||
Ok(crate::cmd_http_request_actions(ctx.window.clone(), ctx.window.app_handle().state::<PluginManager>()).await?)
|
||||
}
|
||||
@@ -1400,6 +1411,7 @@ rpc_commands! {
|
||||
cmd_get_sse_events(CmdGetSseEventsReq) -> Vec<ServerSentEvent>,
|
||||
cmd_get_http_response_events(CmdGetHttpResponseEventsReq) -> Vec<HttpResponseEvent>,
|
||||
cmd_import_data(CmdImportDataReq) -> BatchUpsertResult,
|
||||
cmd_import_url(CmdImportUrlReq) -> BatchUpsertResult,
|
||||
cmd_http_request_actions(CmdHttpRequestActionsReq) -> Vec<GetHttpRequestActionsResponse>,
|
||||
cmd_websocket_request_actions(CmdWebsocketRequestActionsReq) -> Vec<GetWebsocketRequestActionsResponse>,
|
||||
cmd_call_websocket_request_action(CmdCallWebsocketRequestActionReq) -> (),
|
||||
|
||||
Reference in New Issue
Block a user