Generate typed API models for HAProxy, Caddy, Firewall, VLAN, LAGG,
WireGuard (client/server/general), and regenerate Dnsmasq. All core
modules validated against a live OPNsense 26.1.2 instance.
Codegen improvements:
- Add --module-name and --api-key CLI flags for controlling output
filenames and API response envelope keys
- Fix enum variant names starting with digits (prefix with V)
- Use value="" XML attribute for wire values instead of element names
- Handle unknown *Field types as opn_string (select widget safe)
- Forgiving enum deserialization (warn instead of error on unknown)
- Handle empty arrays in opn_string deserializer
Add per-module examples (list_haproxy, list_caddy, list_vlan, etc.)
and utility examples (raw_get, check_package, install_and_wait).
Extract shared client setup into examples/common/mod.rs.
Fix post_typed sending empty JSON body ({}) instead of no body,
which was causing 400 errors on firmware endpoints.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
39 lines
1.3 KiB
Rust
39 lines
1.3 KiB
Rust
//! Shared helpers for examples.
|
|
|
|
use opnsense_api::client::OpnsenseClient;
|
|
use std::env;
|
|
|
|
/// Build an [`OpnsenseClient`] from environment variables.
|
|
///
|
|
/// Required:
|
|
/// - `OPNSENSE_API_KEY`
|
|
/// - `OPNSENSE_API_SECRET`
|
|
///
|
|
/// Optional:
|
|
/// - `OPNSENSE_BASE_URL` (defaults to `https://192.168.1.1/api`)
|
|
pub fn client_from_env() -> OpnsenseClient {
|
|
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
|
|
|
|
let base_url =
|
|
env::var("OPNSENSE_BASE_URL").unwrap_or_else(|_| "https://192.168.1.1/api".to_string());
|
|
|
|
match (
|
|
env::var("OPNSENSE_API_KEY").ok(),
|
|
env::var("OPNSENSE_API_SECRET").ok(),
|
|
) {
|
|
(Some(key), Some(secret)) => OpnsenseClient::builder()
|
|
.base_url(&base_url)
|
|
.auth_from_key_secret(&key, &secret)
|
|
.skip_tls_verify()
|
|
.build()
|
|
.expect("failed to build HTTP client"),
|
|
_ => {
|
|
eprintln!("ERROR: OPNSENSE_API_KEY and OPNSENSE_API_SECRET must be set.");
|
|
eprintln!(" export OPNSENSE_API_KEY=your_key");
|
|
eprintln!(" export OPNSENSE_API_SECRET=your_secret");
|
|
eprintln!(" export OPNSENSE_BASE_URL=https://your-firewall/api");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
}
|