1 unstable release
0.1.0 | Feb 25, 2025 |
---|
#554 in Authentication
136 downloads per month
14KB
111 lines
Using the obscura-client
Library
This guide will walk you through the steps to install and use the obscura-client
library, and provide examples on how to handle errors and deserialize responses using serde_json
and serde::Value
.
Installation
First, add the obscura-client
to your project by running the following command:
cargo add obscura-client
Usage
Make sure to import the necessary modules in your src/main.rs
file:
use obscura_client::Client;
use obscura_client::ConfigMap;
use serde_json::Value;
Here's an example code snippet demonstrating how to use the Client
and ConfigMap
:
fn main() {
let url = "http://localhost:9797";
let token = "your token here";
let client = Client::new(url, token);
let config_map = ConfigMap::new(client);
// Reading a key
match config_map.read("your path here") {
Ok(Some(data)) => {
// Deserialize using serde_json::from_slice
match serde_json::from_slice::<Value>(&data) {
Ok(json) => println!("Deserialized JSON: {:?}", json),
Err(e) => eprintln!("Failed to deserialize JSON: {}", e),
}
}
Ok(None) => println!("Key not found"),
Err(e) => eprintln!("Error reading key: {}", e),
}
// Reading with prefix
match config_map.read_with_prefix("your path prefix here") {
Ok(Some(data)) => {
// Deserialize using serde_json::from_slice
match serde_json::from_slice::<Value>(&data) {
Ok(json) => println!("Deserialized JSON with prefix: {:?}", json),
Err(e) => eprintln!("Failed to deserialize JSON with prefix: {}", e),
}
}
Ok(None) => println!("No data found with prefix"),
Err(e) => eprintln!("Error reading with prefix: {}", e),
}
}
Error Handling
The example above includes error handling for:
- Failed HTTP requests
- Deserialization errors
- Missing keys
Ensure that you handle these errors appropriately in your application to maintain robustness.
Additional Resources
For more information, refer to the server project repository here.
This guide offers a basic understanding of using the obscura-client
library. For more complex scenarios, consider exploring the library documentation and source code.
Dependencies
~6–20MB
~310K SLoC