Skip to content

Commit df282da

Browse files
Merge pull request #39 from joshrotenberg/fix/library-readmes-issue-35
feat: update library READMEs and add working examples
2 parents 0bc9af9 + 00aa4b9 commit df282da

File tree

7 files changed

+397
-32
lines changed

7 files changed

+397
-32
lines changed

crates/redis-cloud/README.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,52 @@ A comprehensive Rust client library for the Redis Cloud REST API.
2323
redis-cloud = "0.1.0"
2424
```
2525

26-
## Usage
26+
## Quick Start
2727

2828
```rust
29-
use redis_cloud::{CloudClient, CloudClientConfig};
29+
use redis_cloud::CloudClient;
3030

3131
#[tokio::main]
3232
async fn main() -> Result<(), Box<dyn std::error::Error>> {
33-
let config = CloudClientConfig {
34-
api_key: "your-api-key".to_string(),
35-
secret_key: "your-secret-key".to_string(),
36-
base_url: None, // Uses default https://api.redislabs.com/v1
37-
};
38-
39-
let client = CloudClient::new(config)?;
33+
// Create client using builder pattern
34+
let client = CloudClient::builder()
35+
.api_key("your-api-key")
36+
.api_secret("your-api-secret")
37+
.build()?;
38+
39+
// Get account information
40+
let account = client.account().get().await?;
41+
println!("Account: {:?}", account);
4042

4143
// List all subscriptions
42-
let subscriptions = client.list_subscriptions(None).await?;
44+
let subscriptions = client.subscription().list().await?;
4345
println!("Subscriptions: {:?}", subscriptions);
4446

45-
// Get account information
46-
let account = client.get_account().await?;
47-
println!("Account: {:?}", account);
47+
// List databases in a subscription
48+
let databases = client.database().list("subscription-id").await?;
49+
println!("Databases: {:?}", databases);
4850

4951
Ok(())
5052
}
5153
```
5254

55+
## Examples
56+
57+
The `examples/` directory contains runnable examples demonstrating common use cases:
58+
59+
- [`basic.rs`](examples/basic.rs) - Getting started with the API client
60+
- [`database_management.rs`](examples/database_management.rs) - Managing databases
61+
62+
Run examples with:
63+
```bash
64+
# Set your API credentials
65+
export REDIS_CLOUD_API_KEY="your-api-key"
66+
export REDIS_CLOUD_API_SECRET="your-api-secret"
67+
68+
# Run an example
69+
cargo run --example basic
70+
```
71+
5372
## API Coverage
5473

5574
This library provides comprehensive coverage of the Redis Cloud REST API, including:
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! Basic example of using the Redis Cloud API client
2+
//!
3+
//! This example shows how to:
4+
//! - Connect to the Redis Cloud API
5+
//! - Get account information
6+
//! - List subscriptions
7+
//!
8+
//! Run with: cargo run --example basic
9+
10+
use redis_cloud::CloudClient;
11+
use std::env;
12+
13+
#[tokio::main]
14+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
15+
// Get API credentials from environment variables
16+
let api_key =
17+
env::var("REDIS_CLOUD_API_KEY").expect("REDIS_CLOUD_API_KEY environment variable not set");
18+
let api_secret = env::var("REDIS_CLOUD_API_SECRET")
19+
.expect("REDIS_CLOUD_API_SECRET environment variable not set");
20+
21+
// Create the client using the builder pattern
22+
let client = CloudClient::builder()
23+
.api_key(&api_key)
24+
.api_secret(&api_secret)
25+
.build()?;
26+
27+
// Get account information using raw API
28+
println!("Fetching account information...");
29+
let account = client.get_raw("/account").await?;
30+
println!("Account ID: {}", account["account"]["id"]);
31+
println!("Account Name: {}", account["account"]["name"]);
32+
println!();
33+
34+
// List all subscriptions using raw API
35+
println!("Fetching subscriptions...");
36+
let subscriptions = client.get_raw("/subscriptions").await?;
37+
38+
if let Some(subs) = subscriptions.as_array() {
39+
println!("Found {} subscription(s):", subs.len());
40+
for sub in subs {
41+
println!(
42+
" - ID: {}, Name: {}, Status: {}",
43+
sub["id"], sub["name"], sub["status"]
44+
);
45+
}
46+
} else {
47+
println!("No subscriptions found");
48+
}
49+
50+
Ok(())
51+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//! Example of database management operations
2+
//!
3+
//! This example shows how to:
4+
//! - List databases in a subscription
5+
//! - Get database details
6+
//! - Create a new database
7+
//!
8+
//! Run with: cargo run --example database_management
9+
10+
use redis_cloud::{CloudClient, CloudDatabaseHandler};
11+
use std::env;
12+
13+
// Uncomment when using the database creation example
14+
// use redis_cloud::CreateDatabaseRequest;
15+
16+
#[tokio::main]
17+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
18+
// Get API credentials from environment variables
19+
let api_key = env::var("REDIS_CLOUD_API_KEY")?;
20+
let api_secret = env::var("REDIS_CLOUD_API_SECRET")?;
21+
22+
// Optional: specify subscription ID via env var or use a default
23+
let subscription_id: u32 = env::var("REDIS_CLOUD_SUBSCRIPTION_ID")
24+
.unwrap_or_else(|_| "123456".to_string())
25+
.parse()?;
26+
27+
// Create the client and database handler
28+
let client = CloudClient::builder()
29+
.api_key(&api_key)
30+
.api_secret(&api_secret)
31+
.build()?;
32+
33+
let db_handler = CloudDatabaseHandler::new(client.clone());
34+
35+
// List all databases in the subscription
36+
println!("Listing databases in subscription {}...", subscription_id);
37+
let databases = db_handler.list(subscription_id).await?;
38+
39+
if let Some(dbs) = databases.as_array() {
40+
println!("Found {} database(s):", dbs.len());
41+
for db in dbs {
42+
println!(
43+
" - ID: {}, Name: {}, Status: {}, Memory: {} MB",
44+
db["databaseId"],
45+
db["name"],
46+
db["status"],
47+
db["memoryLimitInGb"].as_f64().unwrap_or(0.0) * 1024.0
48+
);
49+
}
50+
51+
// Get details of the first database
52+
if let Some(first_db) = dbs.first() {
53+
let db_id = first_db["databaseId"].as_u64().unwrap() as u32;
54+
println!("\nGetting details for database {}...", db_id);
55+
56+
let db_details = db_handler.get_raw(subscription_id, db_id).await?;
57+
58+
println!("Database details:");
59+
println!(" Protocol: {}", db_details["protocol"]);
60+
println!(" Endpoint: {}", db_details["publicEndpoint"]);
61+
println!(
62+
" Security: {}",
63+
db_details["security"]["sslClientAuthentication"]
64+
);
65+
}
66+
} else {
67+
println!("No databases found");
68+
}
69+
70+
// Example: Create a new database (commented out to prevent accidental creation)
71+
// Uncomment and modify as needed
72+
/*
73+
println!("\nCreating a new database...");
74+
let new_database = CreateDatabaseRequest {
75+
name: "example-db".to_string(),
76+
memory_limit_in_gb: 0.1, // 100 MB
77+
data_persistence: "none".to_string(),
78+
replication: false,
79+
data_eviction: Some("volatile-lru".to_string()),
80+
password: None,
81+
support_oss_cluster_api: Some(false),
82+
use_external_endpoint_for_oss_cluster_api: None,
83+
};
84+
85+
let created_db = db_handler
86+
.create(subscription_id, new_database)
87+
.await?;
88+
89+
println!("Created database: ID={}, Name={}",
90+
created_db["databaseId"],
91+
created_db["name"]
92+
);
93+
*/
94+
95+
Ok(())
96+
}

crates/redis-enterprise/README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,56 @@ A comprehensive Rust client library for the Redis Enterprise REST API.
2424
redis-enterprise = "0.1.0"
2525
```
2626

27-
## Usage
27+
## Quick Start
2828

2929
```rust
30-
use redis_enterprise::{EnterpriseClient, EnterpriseClientConfig};
30+
use redis_enterprise::EnterpriseClient;
3131

3232
#[tokio::main]
3333
async fn main() -> Result<(), Box<dyn std::error::Error>> {
34-
let config = EnterpriseClientConfig {
35-
base_url: "https://cluster.example.com:9443".to_string(),
36-
username: "admin@example.com".to_string(),
37-
password: "your-password".to_string(),
38-
insecure: false, // Set to true for self-signed certificates
39-
};
40-
41-
let client = EnterpriseClient::new(config)?;
34+
// Create client using builder pattern
35+
let client = EnterpriseClient::builder()
36+
.url("https://cluster.example.com:9443")
37+
.username("admin@example.com")
38+
.password("your-password")
39+
.insecure(false) // Set to true for self-signed certificates
40+
.build()?;
4241

4342
// Get cluster information
44-
let cluster = client.get_cluster_info().await?;
43+
let cluster = client.cluster().info().await?;
4544
println!("Cluster: {:?}", cluster);
4645

47-
// List databases
48-
let databases = client.list_databases().await?;
46+
// List databases (BDBs)
47+
let databases = client.database().list().await?;
4948
println!("Databases: {:?}", databases);
5049

5150
// Get node statistics
52-
let stats = client.get_node_stats("1").await?;
53-
println!("Node stats: {:?}", stats);
51+
let nodes = client.node().list().await?;
52+
println!("Nodes: {:?}", nodes);
5453

5554
Ok(())
5655
}
5756
```
5857

58+
## Examples
59+
60+
The `examples/` directory contains runnable examples demonstrating common use cases:
61+
62+
- [`basic.rs`](examples/basic.rs) - Getting started with cluster connection
63+
- [`database_management.rs`](examples/database_management.rs) - Managing databases and viewing statistics
64+
65+
Run examples with:
66+
```bash
67+
# Set your cluster credentials
68+
export REDIS_ENTERPRISE_URL="https://localhost:9443"
69+
export REDIS_ENTERPRISE_USER="admin@redis.local"
70+
export REDIS_ENTERPRISE_PASSWORD="your-password"
71+
export REDIS_ENTERPRISE_INSECURE="true" # For self-signed certificates
72+
73+
# Run an example
74+
cargo run --example basic
75+
```
76+
5977
## API Coverage
6078

6179
This library provides 100% coverage of the Redis Enterprise REST API, including:
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! Basic example of using the Redis Enterprise API client
2+
//!
3+
//! This example shows how to:
4+
//! - Connect to a Redis Enterprise cluster
5+
//! - Get cluster information
6+
//! - List databases and nodes
7+
//!
8+
//! Run with: cargo run --example basic
9+
10+
use redis_enterprise::{BdbHandler, EnterpriseClient};
11+
use std::env;
12+
13+
#[tokio::main]
14+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
15+
// Get cluster credentials from environment variables
16+
let url =
17+
env::var("REDIS_ENTERPRISE_URL").unwrap_or_else(|_| "https://localhost:9443".to_string());
18+
let username =
19+
env::var("REDIS_ENTERPRISE_USER").unwrap_or_else(|_| "admin@redis.local".to_string());
20+
let password = env::var("REDIS_ENTERPRISE_PASSWORD")
21+
.expect("REDIS_ENTERPRISE_PASSWORD environment variable not set");
22+
23+
// Check if we should skip SSL verification (for development/self-signed certs)
24+
let insecure = env::var("REDIS_ENTERPRISE_INSECURE")
25+
.unwrap_or_else(|_| "false".to_string())
26+
.parse::<bool>()
27+
.unwrap_or(false);
28+
29+
// Create the client using the builder pattern
30+
let client = EnterpriseClient::builder()
31+
.base_url(&url)
32+
.username(&username)
33+
.password(&password)
34+
.insecure(insecure)
35+
.build()?;
36+
37+
// Get cluster information using raw API
38+
println!("Fetching cluster information...");
39+
let cluster: serde_json::Value = client.get("/v1/cluster").await?;
40+
println!("Cluster Name: {}", cluster["name"]);
41+
println!("Cluster Version: {}", cluster["software_version"]);
42+
println!();
43+
44+
// List all nodes using raw API
45+
println!("Fetching nodes...");
46+
let nodes: serde_json::Value = client.get("/v1/nodes").await?;
47+
48+
if let Some(nodes_array) = nodes.as_array() {
49+
println!("Found {} node(s):", nodes_array.len());
50+
for node in nodes_array {
51+
println!(
52+
" - Node {}: {} ({})",
53+
node["uid"], node["addr"], node["status"]
54+
);
55+
}
56+
}
57+
println!();
58+
59+
// List all databases (BDBs) using handler
60+
println!("Fetching databases...");
61+
let db_handler = BdbHandler::new(client.clone());
62+
let databases = db_handler.list().await?;
63+
64+
println!("Found {} database(s):", databases.len());
65+
for db in &databases {
66+
println!(
67+
" - BDB {}: {} (Memory: {} MB, Status: {})",
68+
db.uid,
69+
db.name,
70+
db.memory_size.unwrap_or(0) / (1024 * 1024),
71+
db.status.as_deref().unwrap_or("unknown")
72+
);
73+
}
74+
75+
Ok(())
76+
}

0 commit comments

Comments
 (0)