Expand description
§Core Module
The core module provides the main business logic for the Coman API Collection Manager. It handles managing collections of API endpoints and executing HTTP requests, independent of any CLI interface.
§Main Components
- [
CollectionManager]: Manages API collections and their endpoints - [
HttpClient]: Executes HTTP requests with a clean, library-friendly API - [
HttpRequest] and [HttpResponse]: Represent HTTP requests and responses - Error types: [
CollectionError] and [HttpError] for handling failures
§Basic Usage
§Managing Collections
use coman::core::CollectionManager;
use coman::models::collection::{Collection, Request, Method};
// Create a collection manager
let manager = CollectionManager::default();
// Create a new collection
let collection = Collection {
name: "api.example.com".to_string(),
url: "https://api.example.com".to_string(),
headers: vec![
("Authorization".to_string(), "Bearer token".to_string()),
("Content-Type".to_string(), "application/json".to_string()),
],
requests: Some(vec![
Request {
name: "get_users".to_string(),
endpoint: "/users".to_string(),
method: Method::Get,
headers: vec![],
body: None,
},
Request {
name: "create_user".to_string(),
endpoint: "/users".to_string(),
method: Method::Post,
headers: vec![],
body: Some(r#"{"name": "John Doe"}"#.to_string()),
},
]),
};
// Add the collection
manager.update_add_collection(collection).await?;
// Get all collections
let collections = manager.get_collections().await;
println!("Loaded {} collections", collections.len());
// Get a specific collection
if let Some(col) = manager.get_collection("api.example.com").await? {
println!("Found collection: {}", col.name);
}§Making HTTP Requests
use coman::core::{HttpClient, HttpMethod};
use std::time::Duration;
// Create an HTTP client with default settings
let client = HttpClient::new()
.with_timeout(Duration::from_secs(30))
.with_follow_redirects(true)
.with_default_headers(vec![
("User-Agent".to_string(), "Coman/1.0".to_string()),
]);
// Make a simple GET request
let response = client.get("https://httpbin.org/get").send().await?;
println!("Status: {}", response.status);
println!("Response: {}", response.body);
// Make a POST request with JSON body
let response = client
.post("https://httpbin.org/post")
.header("Content-Type", "application/json")
.body(r#"{"key": "value"}"#)
.send()
.await?;
if response.is_success() {
println!("Request successful!");
}§Executing Collection Endpoints
use coman::core::{CollectionManager, HttpClient};
let manager = CollectionManager::default();
let client = HttpClient::new();
// Execute a saved endpoint from a collection
let response = client
.execute_endpoint(manager, "api.example.com", "get_users")
.await?;
println!("Response status: {}", response.status);
println!("Response body: {}", response.body);§Error Handling
The core module uses Result types for all operations that can fail:
CollectionResult<T>for collection operationsHttpResult<T>for HTTP operations
use coman::core::{CollectionManager, HttpClient};
let manager = CollectionManager::default();
let client = HttpClient::new();
match manager.get_collection("nonexistent").await {
Ok(Some(collection)) => println!("Found: {}", collection.name),
Ok(None) => println!("Collection not found"),
Err(e) => eprintln!("Error: {}", e),
}
match client.get("https://invalid.url").send().await {
Ok(response) => println!("Success: {}", response.status),
Err(e) => eprintln!("HTTP Error: {}", e),
}Modules§
- collection_
manager - Collection Manager - Core business logic for managing API collections
- collection_
manager_ ops - endpoint_
ops - errors
- http_
client - HTTP Client - Core HTTP request functionality
- http_
request - http_
response - utils