Files
openidea/crates/ide-http/src/lib.rs
T
l0ng-ai d2b0bd6e75 feat(http): convert requests to and from cURL, add environment files (#2592)
Convert to cURL and Copy copies the request at the caret as a cURL command with
the selected environment applied; pasting a `curl ...` command into a .http file
inserts the request it sends (headers, data variants, --json, -F multipart, -u,
-b, -A, -e, -G, POSIX and Windows line continuations). Add Environment to
Public / Private File opens the env file that applies to the request file,
creating it next to it when missing. The conversion lives in ide-http and
shares request preparation with running.

Refs #2435
2026-11-11 04:59:34 +08:00

88 lines
3.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! HTTP Client: IntelliJ-style `.http` request files.
//!
//! [`parse`] splits a file into requests, [`env`] resolves `{{variables}}` from
//! `http-client(.private).env.json`, [`exec`] sends a request (cancellable,
//! timed) and [`history`] keeps the responses. [`client`] builds every HTTP client
//! of the backend, with the proxy of System Settings › HTTP Proxy.
pub mod client;
pub mod curl;
pub mod env;
pub mod exec;
pub mod history;
pub mod parse;
pub use env::Environments;
pub use exec::{HttpResponse, PreparedRequest};
pub use history::{History, HistoryEntry, HttpExchange};
pub use parse::HttpRequestBlock;
use ide_core::{CancellationToken, IdeError, IdeResult, IpcPath};
use std::path::Path;
/// Where a request comes from and how to resolve it.
pub struct RunInput<'a> {
pub text: &'a str,
pub index: u32,
/// The request file (`None` for an unsaved buffer).
pub file: Option<&'a Path>,
pub root: Option<&'a Path>,
pub env: Option<&'a str>,
}
/// Prepares and sends request `index` of `text`. Preparation errors (bad
/// variables, missing includes) fail; transport errors are part of the exchange.
pub async fn run(input: RunInput<'_>, cancel: &CancellationToken) -> IdeResult<HttpExchange> {
let request = prepare(&input)?;
let time_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs_f64() * 1000.0)
.unwrap_or_default();
let result = exec::execute(&request, cancel).await;
if let Err(e) = &result
&& e.kind == ide_core::error::ErrorKind::Cancelled
{
return Err(e.clone());
}
let (response, error) = match result {
Ok(r) => (Some(r), None),
Err(e) => (None, Some(e.message)),
};
Ok(HttpExchange {
entry: HistoryEntry {
id: uuid::Uuid::new_v4().simple().to_string(),
time_ms,
name: request.name.clone(),
method: request.method.clone(),
url: request.url.clone(),
status: response.as_ref().map(|r| r.status),
total_ms: response.as_ref().map(|r| r.total_ms),
file: input.file.map(IpcPath::from),
error,
},
request,
response,
})
}
/// Request `index` of `text` with its variables substituted and includes read.
pub fn prepare(input: &RunInput<'_>) -> IdeResult<PreparedRequest> {
let parsed = parse::parse(input.text);
let raw = parsed
.requests
.get(input.index as usize)
.ok_or_else(|| IdeError::not_found(format!("no request #{}", input.index + 1)))?;
let envs = Environments::load(input.file, input.root)?;
exec::prepare(raw, &parsed.variables, &envs, input.env, input.file.and_then(Path::parent))
}
/// Convert to cURL and Copy: request `index` of `text`, resolved, as a cURL command.
pub fn curl(input: &RunInput<'_>) -> IdeResult<String> {
prepare(input).map(|r| curl::to_curl(&r))
}
/// Environment names available to `file`.
pub fn environments(file: Option<&Path>, root: Option<&Path>) -> IdeResult<Vec<String>> {
Ok(Environments::load(file, root)?.names())
}