coman/core/mod.rs
1//! # Core Module
2//!
3//! The core module provides the main business logic for the Coman API Collection Manager.
4//! It handles managing collections of API endpoints and executing HTTP requests,
5//! independent of any CLI interface.
6//!
7//! ## Main Components
8//!
9//! - [`CollectionManager`]: Manages API collections and their endpoints
10//! - [`HttpClient`]: Executes HTTP requests with a clean, library-friendly API
11//! - [`HttpRequest`] and [`HttpResponse`]: Represent HTTP requests and responses
12//! - Error types: [`CollectionError`] and [`HttpError`] for handling failures
13//!
14//! ## Basic Usage
15//!
16//! ### Managing Collections
17//!
18//! ```rust,no_run
19//! use coman::core::CollectionManager;
20//! use coman::models::collection::{Collection, Request, Method};
21//!
22//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
23//! // Create a collection manager
24//! let manager = CollectionManager::default();
25//!
26//! // Create a new collection
27//! let collection = Collection {
28//! name: "api.example.com".to_string(),
29//! url: "https://api.example.com".to_string(),
30//! headers: vec![
31//! ("Authorization".to_string(), "Bearer token".to_string()),
32//! ("Content-Type".to_string(), "application/json".to_string()),
33//! ],
34//! requests: Some(vec![
35//! Request {
36//! name: "get_users".to_string(),
37//! endpoint: "/users".to_string(),
38//! method: Method::Get,
39//! headers: vec![],
40//! body: None,
41//! },
42//! Request {
43//! name: "create_user".to_string(),
44//! endpoint: "/users".to_string(),
45//! method: Method::Post,
46//! headers: vec![],
47//! body: Some(r#"{"name": "John Doe"}"#.to_string()),
48//! },
49//! ]),
50//! };
51//!
52//! // Add the collection
53//! manager.update_add_collection(collection).await?;
54//!
55//! // Get all collections
56//! let collections = manager.get_collections().await;
57//! println!("Loaded {} collections", collections.len());
58//!
59//! // Get a specific collection
60//! if let Some(col) = manager.get_collection("api.example.com").await? {
61//! println!("Found collection: {}", col.name);
62//! }
63//! # Ok(())
64//! # }
65//! ```
66//!
67//! ### Making HTTP Requests
68//!
69//! ```rust,no_run
70//! use coman::core::{HttpClient, HttpMethod};
71//! use std::time::Duration;
72//!
73//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
74//! // Create an HTTP client with default settings
75//! let client = HttpClient::new()
76//! .with_timeout(Duration::from_secs(30))
77//! .with_follow_redirects(true)
78//! .with_default_headers(vec![
79//! ("User-Agent".to_string(), "Coman/1.0".to_string()),
80//! ]);
81//!
82//! // Make a simple GET request
83//! let response = client.get("https://httpbin.org/get").send().await?;
84//! println!("Status: {}", response.status);
85//! println!("Response: {}", response.body);
86//!
87//! // Make a POST request with JSON body
88//! let response = client
89//! .post("https://httpbin.org/post")
90//! .header("Content-Type", "application/json")
91//! .body(r#"{"key": "value"}"#)
92//! .send()
93//! .await?;
94//!
95//! if response.is_success() {
96//! println!("Request successful!");
97//! }
98//! # Ok(())
99//! # }
100//! ```
101//!
102//! ### Executing Collection Endpoints
103//!
104//! ```rust,no_run
105//! use coman::core::{CollectionManager, HttpClient};
106//!
107//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
108//! let manager = CollectionManager::default();
109//! let client = HttpClient::new();
110//!
111//! // Execute a saved endpoint from a collection
112//! let response = client
113//! .execute_endpoint(manager, "api.example.com", "get_users")
114//! .await?;
115//!
116//! println!("Response status: {}", response.status);
117//! println!("Response body: {}", response.body);
118//! # Ok(())
119//! # }
120//! ```
121//!
122//! ## Error Handling
123//!
124//! The core module uses `Result` types for all operations that can fail:
125//!
126//! - `CollectionResult<T>` for collection operations
127//! - `HttpResult<T>` for HTTP operations
128//!
129//! ```rust,no_run
130//! use coman::core::{CollectionManager, HttpClient};
131//!
132//! # async fn example() {
133//! let manager = CollectionManager::default();
134//! let client = HttpClient::new();
135//!
136//! match manager.get_collection("nonexistent").await {
137//! Ok(Some(collection)) => println!("Found: {}", collection.name),
138//! Ok(None) => println!("Collection not found"),
139//! Err(e) => eprintln!("Error: {}", e),
140//! }
141//!
142//! match client.get("https://invalid.url").send().await {
143//! Ok(response) => println!("Success: {}", response.status),
144//! Err(e) => eprintln!("HTTP Error: {}", e),
145//! }
146//! # }
147//! ```
148
149pub mod collection_manager;
150pub mod collection_manager_ops;
151pub mod endpoint_ops;
152pub mod errors;
153pub mod http_client;
154pub mod http_request;
155pub mod http_response;
156pub mod utils;