status_example/status_example.rs
1//! # Cortex API Status Example
2//!
3//! This example demonstrates how to use the Rust client for the Cortex API
4//! to fetch and display general status information.
5//!
6//! ## Functionality
7//!
8//! 1. **Configuration**: Sets up the API client configuration using `common::setup_configuration()`.
9//! 2. **Fetch Status**: Calls `status_api::get_status()` to retrieve general system status
10//! and configuration information from the `/status` endpoint.
11//! 3. **Health Status (Note)**: The call to `status_api::get_health_status()` (for `/status/health`)
12//! has been commented out in this example. This endpoint may not be available in all
13//! Cortex versions or configurations, potentially leading to a 404 Not Found error.
14//! The OpenAPI specification itself notes this endpoint as a "TODO".
15//!
16//! ## Prerequisites
17//!
18//! - A running Cortex instance accessible via the network.
19//! - Environment variables `CORTEX_ENDPOINT` and `CORTEX_API_KEY` must be set.
20//!
21//! ## Usage
22//!
23//! ```sh
24//! # Set your Cortex API endpoint and key
25//! export CORTEX_ENDPOINT="http://your-cortex-host:9000/api"
26//! export CORTEX_API_KEY="your_cortex_api_key"
27//!
28//! # Run the example
29//! cargo run --example status_example
30//! ```
31use cortex_client::apis::status_api;
32
33mod common;
34
35#[tokio::main]
36async fn main() -> Result<(), Box<dyn std::error::Error>> {
37 // Setup configuration using the common helper
38 let config = match common::setup_configuration() {
39 Ok(cfg) => cfg,
40 Err(e) => {
41 eprintln!("Configuration error: {}", e);
42 eprintln!("Please ensure CORTEX_ENDPOINT and CORTEX_API_KEY environment variables are set.");
43 eprintln!("Example usage:");
44 eprintln!(" export CORTEX_ENDPOINT=\"http://localhost:9000/api\"");
45 eprintln!(" export CORTEX_API_KEY=\"your_api_key_here\"");
46 eprintln!(" cargo run --example status_example");
47 return Err(e.into());
48 }
49 };
50
51 println!("Fetching API status (/status)...");
52 match status_api::get_status(&config).await {
53 Ok(status_response) => {
54 println!("Successfully fetched API status:");
55 println!("{:#?}", status_response);
56
57 if let Some(versions) = status_response.versions {
58 println!("\nVersions:");
59 println!(" Cortex: {}", versions.cortex.as_deref().unwrap_or("N/A"));
60 println!(" Elastic4Play: {}", versions.elastic4_play.as_deref().unwrap_or("N/A"));
61 }
62 if let Some(api_config) = status_response.config {
63 println!("\nAPI Configuration:");
64 println!(" Auth Type: {:?}", api_config.auth_type);
65 println!(" Capabilities: {:?}", api_config.capabilities.unwrap_or_default());
66 }
67 }
68 Err(e) => {
69 eprintln!("Error fetching API status: {:?}", e);
70 }
71 }
72
73 Ok(())
74}