@@ -6,13 +6,13 @@ use reqwest::Client as HttpClient;
|
||||
use serde_json::{Value, json};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::models::TransportStatus;
|
||||
use crate::clients::Destination;
|
||||
use crate::models::TransportStatus;
|
||||
|
||||
/// Lightweight client for Mycelium JSON-RPC (send + query status)
|
||||
#[derive(Clone)]
|
||||
pub struct MyceliumClient {
|
||||
base_url: String, // e.g. http://127.0.0.1:8990
|
||||
base_url: String, // e.g. http://127.0.0.1:8990
|
||||
http: HttpClient,
|
||||
id_counter: Arc<AtomicU64>,
|
||||
}
|
||||
@@ -58,20 +58,30 @@ impl MyceliumClient {
|
||||
let body: Value = resp.json().await?;
|
||||
if let Some(err) = body.get("error") {
|
||||
let code = err.get("code").and_then(|v| v.as_i64()).unwrap_or(0);
|
||||
let msg = err.get("message").and_then(|v| v.as_str()).unwrap_or("unknown error");
|
||||
let msg = err
|
||||
.get("message")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("unknown error");
|
||||
if code == 408 {
|
||||
return Err(MyceliumClientError::TransportTimeout);
|
||||
}
|
||||
return Err(MyceliumClientError::RpcError(format!("code={code} msg={msg}")));
|
||||
return Err(MyceliumClientError::RpcError(format!(
|
||||
"code={code} msg={msg}"
|
||||
)));
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(MyceliumClientError::RpcError(format!("HTTP {status}, body {body}")));
|
||||
return Err(MyceliumClientError::RpcError(format!(
|
||||
"HTTP {status}, body {body}"
|
||||
)));
|
||||
}
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// Call messageStatus with an outbound message id (hex string)
|
||||
pub async fn message_status(&self, id_hex: &str) -> Result<TransportStatus, MyceliumClientError> {
|
||||
pub async fn message_status(
|
||||
&self,
|
||||
id_hex: &str,
|
||||
) -> Result<TransportStatus, MyceliumClientError> {
|
||||
let params = json!({ "id": id_hex });
|
||||
let body = self.jsonrpc("messageStatus", params).await?;
|
||||
let result = body.get("result").ok_or_else(|| {
|
||||
@@ -83,7 +93,9 @@ impl MyceliumClient {
|
||||
} else if let Some(s) = result.as_str() {
|
||||
s.to_string()
|
||||
} else {
|
||||
return Err(MyceliumClientError::InvalidResponse(format!("unexpected result shape: {result}")));
|
||||
return Err(MyceliumClientError::InvalidResponse(format!(
|
||||
"unexpected result shape: {result}"
|
||||
)));
|
||||
};
|
||||
Self::map_status(&status_str).ok_or_else(|| {
|
||||
MyceliumClientError::InvalidResponse(format!("unknown status: {status_str}"))
|
||||
@@ -143,7 +155,10 @@ impl MyceliumClient {
|
||||
|
||||
/// Helper to extract outbound message id from pushMessage result (InboundMessage or PushMessageResponseId)
|
||||
pub fn extract_message_id_from_result(result: &Value) -> Option<String> {
|
||||
result.get("id").and_then(|v| v.as_str()).map(|s| s.to_string())
|
||||
result
|
||||
.get("id")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,24 +177,39 @@ mod tests {
|
||||
Some(10),
|
||||
);
|
||||
let msg1 = p1.get("message").unwrap();
|
||||
assert_eq!(msg1.get("topic").unwrap().as_str().unwrap(), "supervisor.rpc");
|
||||
assert_eq!(
|
||||
msg1.get("topic").unwrap().as_str().unwrap(),
|
||||
"supervisor.rpc"
|
||||
);
|
||||
assert_eq!(msg1.get("payload").unwrap().as_str().unwrap(), "Zm9vYmFy");
|
||||
assert_eq!(
|
||||
msg1.get("dst").unwrap().get("ip").unwrap().as_str().unwrap(),
|
||||
msg1.get("dst")
|
||||
.unwrap()
|
||||
.get("ip")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap(),
|
||||
"2001:db8::1"
|
||||
);
|
||||
assert_eq!(p1.get("reply_timeout").unwrap().as_u64().unwrap(), 10);
|
||||
|
||||
// PK destination without timeout
|
||||
let p2 = MyceliumClient::build_push_params(
|
||||
&Destination::Pk("bb39b4a3a4efd70f3e05e37887677e02efbda14681d0acd3882bc0f754792c32".into()),
|
||||
&Destination::Pk(
|
||||
"bb39b4a3a4efd70f3e05e37887677e02efbda14681d0acd3882bc0f754792c32".into(),
|
||||
),
|
||||
"supervisor.rpc",
|
||||
"YmF6", // "baz"
|
||||
None,
|
||||
);
|
||||
let msg2 = p2.get("message").unwrap();
|
||||
assert_eq!(
|
||||
msg2.get("dst").unwrap().get("pk").unwrap().as_str().unwrap(),
|
||||
msg2.get("dst")
|
||||
.unwrap()
|
||||
.get("pk")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap(),
|
||||
"bb39b4a3a4efd70f3e05e37887677e02efbda14681d0acd3882bc0f754792c32"
|
||||
);
|
||||
assert!(p2.get("reply_timeout").is_none());
|
||||
@@ -205,4 +235,4 @@ mod tests {
|
||||
"fedcba9876543210"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user