use std::time::Duration; use serde_json::Value; use reqwest::Client; use base64::encode; /// Get information about the Mycelium node /// /// # Arguments /// /// * `api_url` - The URL of the Mycelium API /// /// # Returns /// /// * `Result` - The node information as a JSON value, or an error message pub async fn get_node_info(api_url: &str) -> Result { let client = Client::new(); let url = format!("{}/api/v1/admin", api_url); let response = client .get(&url) .send() .await .map_err(|e| format!("Failed to send request: {}", e))?; let status = response.status(); if !status.is_success() { return Err(format!("Request failed with status: {}", status)); } let result: Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; Ok(result) } /// List all peers connected to the Mycelium node /// /// # Arguments /// /// * `api_url` - The URL of the Mycelium API /// /// # Returns /// /// * `Result` - The list of peers as a JSON value, or an error message pub async fn list_peers(api_url: &str) -> Result { let client = Client::new(); let url = format!("{}/api/v1/admin/peers", api_url); let response = client .get(&url) .send() .await .map_err(|e| format!("Failed to send request: {}", e))?; let status = response.status(); if !status.is_success() { return Err(format!("Request failed with status: {}", status)); } let result: Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; Ok(result) } /// Add a new peer to the Mycelium node /// /// # Arguments /// /// * `api_url` - The URL of the Mycelium API /// * `peer_address` - The address of the peer to add /// /// # Returns /// /// * `Result` - The result of the operation as a JSON value, or an error message pub async fn add_peer(api_url: &str, peer_address: &str) -> Result { let client = Client::new(); let url = format!("{}/api/v1/admin/peers", api_url); let response = client .post(&url) .json(&serde_json::json!({ "address": peer_address })) .send() .await .map_err(|e| format!("Failed to send request: {}", e))?; let status = response.status(); if !status.is_success() { return Err(format!("Request failed with status: {}", status)); } let result: Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; Ok(result) } /// Remove a peer from the Mycelium node /// /// # Arguments /// /// * `api_url` - The URL of the Mycelium API /// * `peer_id` - The ID of the peer to remove /// /// # Returns /// /// * `Result` - The result of the operation as a JSON value, or an error message pub async fn remove_peer(api_url: &str, peer_id: &str) -> Result { let client = Client::new(); let url = format!("{}/api/v1/admin/peers/{}", api_url, peer_id); let response = client .delete(&url) .send() .await .map_err(|e| format!("Failed to send request: {}", e))?; let status = response.status(); if !status.is_success() { return Err(format!("Request failed with status: {}", status)); } let result: Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; Ok(result) } /// List all selected routes in the Mycelium node /// /// # Arguments /// /// * `api_url` - The URL of the Mycelium API /// /// # Returns /// /// * `Result` - The list of selected routes as a JSON value, or an error message pub async fn list_selected_routes(api_url: &str) -> Result { let client = Client::new(); let url = format!("{}/api/v1/admin/routes/selected", api_url); let response = client .get(&url) .send() .await .map_err(|e| format!("Failed to send request: {}", e))?; let status = response.status(); if !status.is_success() { return Err(format!("Request failed with status: {}", status)); } let result: Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; Ok(result) } /// List all fallback routes in the Mycelium node /// /// # Arguments /// /// * `api_url` - The URL of the Mycelium API /// /// # Returns /// /// * `Result` - The list of fallback routes as a JSON value, or an error message pub async fn list_fallback_routes(api_url: &str) -> Result { let client = Client::new(); let url = format!("{}/api/v1/admin/routes/fallback", api_url); let response = client .get(&url) .send() .await .map_err(|e| format!("Failed to send request: {}", e))?; let status = response.status(); if !status.is_success() { return Err(format!("Request failed with status: {}", status)); } let result: Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; Ok(result) } /// Send a message to a destination via the Mycelium node /// /// # Arguments /// /// * `api_url` - The URL of the Mycelium API /// * `destination` - The destination address /// * `topic` - The message topic /// * `message` - The message content /// * `deadline_secs` - The deadline in seconds /// /// # Returns /// /// * `Result` - The result of the operation as a JSON value, or an error message pub async fn send_message(api_url: &str, destination: &str, topic: &str, message: &str, deadline_secs: u64) -> Result { let client = Client::new(); let url = format!("{}/api/v1/messages", api_url); // Convert deadline to seconds let deadline = Duration::from_secs(deadline_secs).as_secs(); let response = client .post(&url) .json(&serde_json::json!({ "destination": destination, "topic": topic, "content": message, "deadline": deadline })) .send() .await .map_err(|e| format!("Failed to send request: {}", e))?; let status = response.status(); if !status.is_success() { return Err(format!("Request failed with status: {}", status)); } let result: Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; Ok(result) } /// Receive messages from a topic via the Mycelium node /// /// # Arguments /// /// * `api_url` - The URL of the Mycelium API /// * `topic` - The message topic /// * `count` - The maximum number of messages to receive /// /// # Returns /// /// * `Result` - The received messages as a JSON value, or an error message pub async fn receive_messages(api_url: &str, topic: &str, count: u32) -> Result { let client = Client::new(); let url = format!("{}/api/v1/messages", api_url); let response = client .get(&url) .query(&[("topic", encode(topic)), ("count", count.to_string())]) .send() .await .map_err(|e| format!("Failed to send request: {}", e))?; let status = response.status(); if !status.is_success() { return Err(format!("Request failed with status: {}", status)); } let result: Value = response .json() .await .map_err(|e| format!("Failed to parse response: {}", e))?; Ok(result) }