Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,15 @@ rw workspaces show cardiology --json
# GET /api/clinicians (default profile)
rw api clinicians

# POST with JSON body fields using dot-path keys
# POST with JSON body fields using dot-path keys (sent as Content-Type: application/vnd.api+json)
rw api clinicians --method POST --field data.attributes.name="Alice Avalon" --field data.attributes.email=alice.avalon@mercy.org --field data.relationships.role.data.type=roles --field data.relationships.role.data.id=<uuid>

# Add extra request headers
rw api clinicians --header "Accept: application/json"

# Send the --field body as plain JSON instead of JSON:API
rw api clinicians --method POST --header "Content-Type: application/json" --field data.attributes.name="Alice Avalon"

# Print raw (unpretty) JSON
rw api clinicians --raw

Expand Down
3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ pub struct ApiArgs {
#[arg(short = 'H', long = "header")]
pub headers: Vec<String>,

/// Request body fields as key=value pairs (implies POST if no method set).
/// Request body fields as key=value pairs, sent as application/vnd.api+json
/// unless -H sets Content-Type (implies POST if no method set).
#[arg(short = 'f', long = "field")]
pub fields: Vec<String>,

Expand Down
110 changes: 104 additions & 6 deletions src/commands/api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use anyhow::{bail, Context, Result};
use json_dotpath::DotPaths;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::header::{HeaderMap, HeaderName, HeaderValue, CONTENT_TYPE};

use crate::cli::ApiArgs;
use crate::config::AppContext;
use crate::output::Output;

/// Default media type for `-f` request bodies.
const JSON_API_MEDIA_TYPE: &str = "application/vnd.api+json";

pub async fn dispatch(args: ApiArgs, ctx: &AppContext, _out: &Output) -> Result<()> {
run(
ctx,
Expand Down Expand Up @@ -64,13 +67,20 @@ pub async fn run(
let (name, value) = parse_header(h).with_context(|| format!("invalid header: {:?}", h))?;
header_map.insert(name, value);
}
req = req.headers(header_map);

// Parse -f field=value pairs and attach as JSON body.
// Parse -f field=value pairs and attach as a JSON:API body. The API
// speaks JSON:API, so the default Content-Type is
// `application/vnd.api+json`; a Content-Type supplied via -H wins
// (header names are case-insensitive). The body is serialized here
// because reqwest's `json()` would default to `application/json`.
if !fields.is_empty() {
let body = build_body(fields)?;
req = req.json(&body);
let body =
serde_json::to_vec(&build_body(fields)?).context("failed to serialize request body")?;
header_map
.entry(CONTENT_TYPE)
.or_insert(HeaderValue::from_static(JSON_API_MEDIA_TYPE));
req = req.body(body);
}
req = req.headers(header_map);

let resp = req
.send()
Expand Down Expand Up @@ -144,6 +154,94 @@ fn build_body(fields: &[String]) -> Result<serde_json::Value> {
#[cfg(test)]
mod tests {
use super::*;
use mockito::{Matcher, Server};

// --- run / request tests ---

struct TestAuthGuard {
dir: tempfile::TempDir,
}

impl TestAuthGuard {
fn new() -> Self {
use crate::auth_cache::{save_auth_cache, AuthCache};
let dir = tempfile::TempDir::new().unwrap();
let cache = AuthCache::Bearer {
access_token: "test-token".to_string(),
refresh_token: None,
expires_at: i64::MAX,
};
save_auth_cache(dir.path(), "test", &cache).unwrap();
TestAuthGuard { dir }
}

fn app_context(&self, base_url: &str) -> AppContext {
use crate::cli::Stage;
AppContext {
config_dir: self.dir.path().to_path_buf(),
profile: "test".to_string(),
auth_profile: "test".to_string(),
stage: Stage::Dev,
auth_stage: Stage::Dev,
base_url: base_url.to_string(),
defaults: std::collections::BTreeMap::new(),
}
}
}

/// Send `rw api -X PUT clinicians/1 -f attributes.name=Jane` with the
/// given -H headers and assert the server saw `expected_content_type`
/// alongside the JSON body.
async fn assert_put_content_type(headers: &[&str], expected_content_type: &str) {
let mut server = Server::new_async().await;
let mock = server
.mock("PUT", "/clinicians/1")
.match_header("content-type", expected_content_type)
.match_body(Matcher::Json(
serde_json::json!({"attributes": {"name": "Jane"}}),
))
.with_status(200)
.with_body("{}")
.expect(1)
.create_async()
.await;

let guard = TestAuthGuard::new();
let ctx = guard.app_context(&server.url());
let headers: Vec<String> = headers.iter().map(|h| h.to_string()).collect();
run(
&ctx,
"clinicians/1",
"PUT",
&headers,
&["attributes.name=Jane".to_string()],
true,
)
.await
.unwrap();

mock.assert_async().await;
}

#[tokio::test]
async fn test_run_fields_default_content_type_json_api() {
assert_put_content_type(&[], "application/vnd.api+json").await;
}

#[tokio::test]
async fn test_run_fields_keep_caller_content_type() {
assert_put_content_type(&["Content-Type: application/json"], "application/json").await;
}

#[tokio::test]
async fn test_run_fields_keep_caller_content_type_any_case() {
assert_put_content_type(&["content-TYPE: application/json"], "application/json").await;
}

#[tokio::test]
async fn test_run_fields_keep_other_headers_with_default_content_type() {
assert_put_content_type(&["Accept: application/json"], "application/vnd.api+json").await;
}

// --- build_body / dot-path tests ---

Expand Down
Loading