working example for mycelium
Some checks failed
Rhai Tests / Run Rhai Tests (pull_request) Has been cancelled

This commit is contained in:
Maxime Van Hees
2025-05-13 11:52:31 +02:00
parent 3225b3f029
commit 3e64a53a83
3 changed files with 106 additions and 82 deletions

View File

@@ -1,7 +1,11 @@
use std::time::Duration;
use serde_json::Value;
use base64::{
alphabet,
engine::{self, general_purpose},
Engine as _,
};
use reqwest::Client;
use base64::encode;
use serde_json::Value;
use std::time::Duration;
/// Get information about the Mycelium node
///
@@ -15,23 +19,23 @@ use base64::encode;
pub async fn get_node_info(api_url: &str) -> Result<Value, String> {
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)
}
@@ -47,23 +51,23 @@ pub async fn get_node_info(api_url: &str) -> Result<Value, String> {
pub async fn list_peers(api_url: &str) -> Result<Value, String> {
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)
}
@@ -80,26 +84,31 @@ pub async fn list_peers(api_url: &str) -> Result<Value, String> {
pub async fn add_peer(api_url: &str, peer_address: &str) -> Result<Value, String> {
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
"endpoint": peer_address
}))
.send()
.await
.map_err(|e| format!("Failed to send request: {}", e))?;
let status = response.status();
if status == reqwest::StatusCode::NO_CONTENT {
// Successfully added, but no content to parse
return Ok(serde_json::json!({"success": true}));
}
if !status.is_success() {
return Err(format!("Request failed with status: {}", status));
}
// For other success statuses that might have a body
let result: Value = response
.json()
.await
.map_err(|e| format!("Failed to parse response: {}", e))?;
Ok(result)
}
@@ -115,24 +124,29 @@ pub async fn add_peer(api_url: &str, peer_address: &str) -> Result<Value, String
/// * `Result<Value, String>` - 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<Value, String> {
let client = Client::new();
let url = format!("{}/api/v1/admin/peers/{}", api_url, peer_id);
let peer_id_url_encoded = urlencoding::encode(peer_id);
let url = format!("{}/api/v1/admin/peers/{}", api_url, peer_id_url_encoded);
let response = client
.delete(&url)
.send()
.await
.map_err(|e| format!("Failed to send request: {}", e))?;
let status = response.status();
if status == reqwest::StatusCode::NO_CONTENT {
// Successfully removed, but no content to parse
return Ok(serde_json::json!({"success": true}));
}
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)
}
@@ -148,23 +162,23 @@ pub async fn remove_peer(api_url: &str, peer_id: &str) -> Result<Value, String>
pub async fn list_selected_routes(api_url: &str) -> Result<Value, String> {
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)
}
@@ -180,23 +194,23 @@ pub async fn list_selected_routes(api_url: &str) -> Result<Value, String> {
pub async fn list_fallback_routes(api_url: &str) -> Result<Value, String> {
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)
}
@@ -213,35 +227,40 @@ pub async fn list_fallback_routes(api_url: &str) -> Result<Value, String> {
/// # Returns
///
/// * `Result<Value, String>` - 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<Value, String> {
pub async fn send_message(
api_url: &str,
destination: &str,
topic: &str,
message: &str,
deadline_secs: u64,
) -> Result<Value, String> {
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
"dst": { "ip": destination },
"topic": general_purpose::STANDARD.encode(topic),
"payload": general_purpose::STANDARD.encode(message)
}))
.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)
}
@@ -259,23 +278,23 @@ pub async fn send_message(api_url: &str, destination: &str, topic: &str, message
pub async fn receive_messages(api_url: &str, topic: &str, count: u32) -> Result<Value, String> {
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())])
.query(&[("topic", general_purpose::STANDARD.encode(topic))])
.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)
}
}