Skip to main content

openapi_clap/
lib.rs

1//! Auto-generate clap CLI commands from OpenAPI specs.
2//!
3//! Parses a dereferenced OpenAPI JSON, extracts operations into an IR,
4//! builds a clap `Command` tree, and dispatches HTTP requests.
5//!
6//! # Usage
7//!
8//! ```no_run
9//! use openapi_clap::{Auth, CliConfig, build_commands, extract_operations, find_operation, dispatch};
10//! use openapi_deref::resolve;
11//! use reqwest::blocking::Client;
12//!
13//! let spec_json = r#"{"openapi":"3.0.0","paths":{}}"#;
14//! let raw: serde_json::Value = serde_json::from_str(spec_json).unwrap();
15//! let resolved = resolve(&raw).unwrap();
16//! let ops = extract_operations(&resolved.value);
17//!
18//! let config = CliConfig::new("myapi", "My API CLI", "https://api.example.com");
19//! let cmd = build_commands(&config, &ops);
20//! let matches = cmd.get_matches();
21//!
22//! let (group, group_matches) = matches.subcommand().expect("subcommand required");
23//! let (op_name, op_matches) = group_matches.subcommand().expect("operation required");
24//!
25//! if let Some(op) = find_operation(&ops, group, op_name, &config) {
26//!     let base_url = op_matches.get_one::<String>("base-url").unwrap();
27//!     let api_key = std::env::var("API_KEY").unwrap_or_default();
28//!     let auth = Auth::Bearer(&api_key);
29//!     let client = Client::new();
30//!     match dispatch(&client, base_url, &auth, op, op_matches) {
31//!         Ok(value) => println!("{}", serde_json::to_string_pretty(&value).unwrap()),
32//!         Err(e) => eprintln!("error: {e}"),
33//!     }
34//! }
35//! ```
36
37pub mod builder;
38pub mod dispatch;
39pub mod error;
40pub mod spec;
41
42pub use builder::{
43    build_commands, find_operation, normalize_group, normalize_operation_id, CliConfig,
44    CommandNaming,
45};
46pub use dispatch::{build_body, dispatch, Auth, PreparedRequest, ResolvedAuth, SendResponse};
47pub use error::DispatchError;
48pub use spec::{extract_operations, is_bool_schema, ApiOperation, Param};
49
50// Re-export dependencies for downstream crates
51pub use clap;
52pub use reqwest;