|
| 1 | +use anyhow::Result; |
| 2 | +use clap::{Arg, ArgAction, ArgMatches, Command, ValueHint}; |
| 3 | + |
| 4 | +use crate::api::{Api, Method}; |
| 5 | + |
| 6 | +pub fn make_command(command: Command) -> Command { |
| 7 | + command |
| 8 | + .about("Make a raw API request to the Sentry API.") |
| 9 | + .arg( |
| 10 | + Arg::new("endpoint") |
| 11 | + .value_name("ENDPOINT") |
| 12 | + .required(true) |
| 13 | + .value_hint(ValueHint::Url) |
| 14 | + .help( |
| 15 | + "The API endpoint to request (e.g., 'organizations/' or '/projects/my-org/my-project/releases/').{n}\ |
| 16 | + The endpoint will be prefixed with '/api/0/' automatically.", |
| 17 | + ), |
| 18 | + ) |
| 19 | + .arg( |
| 20 | + Arg::new("method") |
| 21 | + .short('m') |
| 22 | + .long("method") |
| 23 | + .value_name("METHOD") |
| 24 | + .value_parser(["GET", "POST", "PUT", "DELETE", "PATCH"]) |
| 25 | + .default_value("GET") |
| 26 | + .action(ArgAction::Set) |
| 27 | + .help("The HTTP method to use for the request."), |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +pub fn execute(matches: &ArgMatches) -> Result<()> { |
| 32 | + let endpoint = matches.get_one::<String>("endpoint").unwrap(); |
| 33 | + let method_str = matches.get_one::<String>("method").unwrap(); |
| 34 | + |
| 35 | + let method = match method_str.as_str() { |
| 36 | + "GET" => Method::Get, |
| 37 | + "POST" => Method::Post, |
| 38 | + "PUT" => Method::Put, |
| 39 | + "DELETE" => Method::Delete, |
| 40 | + "PATCH" => Method::Patch, |
| 41 | + _ => unreachable!("Invalid method value"), |
| 42 | + }; |
| 43 | + |
| 44 | + let api = Api::current(); |
| 45 | + let resp = api.request(method, endpoint, None)?.send()?; |
| 46 | + |
| 47 | + // Print the response body as-is to stdout |
| 48 | + if let Some(body) = resp.body { |
| 49 | + let body_str = String::from_utf8_lossy(&body); |
| 50 | + println!("{}", body_str); |
| 51 | + } |
| 52 | + |
| 53 | + Ok(()) |
| 54 | +} |
0 commit comments