main
rs 27 lines 836 Bytes
Raw
1 //! `si api <path>` — the escape hatch for any endpoint without a dedicated
2 //! command, e.g. one the server added since this build of `si`.
3
4 use {
5 crate::{cli::ApiArgs, context::Ctx},
6 anyhow::{anyhow, Result},
7 reqwest::Method,
8 std::str::FromStr,
9 };
10
11 pub async fn run(ctx: &Ctx, args: ApiArgs) -> Result<()> {
12 ctx.require_token()?;
13
14 let method = Method::from_str(&args.method.to_ascii_uppercase())
15 .map_err(|_| anyhow!("Unknown HTTP method: {}", args.method))?;
16
17 let body = match args.body {
18 Some(raw) => {
19 Some(serde_json::from_str(&raw).map_err(|e| anyhow!("--body is not valid JSON: {e}"))?)
20 }
21 None => None,
22 };
23
24 let response = ctx.client.raw(method, &args.path, body).await?;
25 println!("{}", serde_json::to_string_pretty(&response)?);
26 Ok(())
27 }