Skip to main content

Crate openapi_clap

Crate openapi_clap 

Source
Expand description

Auto-generate clap CLI commands from OpenAPI specs.

Parses a dereferenced OpenAPI JSON, extracts operations into an IR, builds a clap Command tree, and dispatches HTTP requests.

§Usage

use openapi_clap::{Auth, CliConfig, build_commands, extract_operations, find_operation, dispatch};
use openapi_deref::resolve;
use reqwest::blocking::Client;

let spec_json = r#"{"openapi":"3.0.0","paths":{}}"#;
let raw: serde_json::Value = serde_json::from_str(spec_json).unwrap();
let resolved = resolve(&raw).unwrap();
let ops = extract_operations(&resolved.value);

let config = CliConfig::new("myapi", "My API CLI", "https://api.example.com");
let cmd = build_commands(&config, &ops);
let matches = cmd.get_matches();

let (group, group_matches) = matches.subcommand().expect("subcommand required");
let (op_name, op_matches) = group_matches.subcommand().expect("operation required");

if let Some(op) = find_operation(&ops, group, op_name, &config) {
    let base_url = op_matches.get_one::<String>("base-url").unwrap();
    let api_key = std::env::var("API_KEY").unwrap_or_default();
    let auth = Auth::Bearer(&api_key);
    let client = Client::new();
    match dispatch(&client, base_url, &auth, op, op_matches) {
        Ok(value) => println!("{}", serde_json::to_string_pretty(&value).unwrap()),
        Err(e) => eprintln!("error: {e}"),
    }
}

Re-exports§

pub use builder::build_commands;
pub use builder::find_operation;
pub use builder::normalize_group;
pub use builder::normalize_operation_id;
pub use builder::CliConfig;
pub use builder::CommandNaming;
pub use dispatch::dispatch;
pub use dispatch::Auth;
pub use error::DispatchError;
pub use spec::extract_operations;
pub use spec::is_bool_schema;
pub use spec::ApiOperation;
pub use spec::Param;
pub use clap;
pub use reqwest;

Modules§

builder
IR → clap Command tree builder
dispatch
ArgMatches → HTTP request dispatch
error
Error types for the openapi-clap crate.
spec
OpenAPI spec → internal IR (intermediate representation)