1st commit
This commit is contained in:
96
examples/smartreceive_example.rs
Normal file
96
examples/smartreceive_example.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use msghandler::{smartreceive, SmartreceiveOptions};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Simulated NATS message JSON (received from NATS subscription)
|
||||
let msg_json_str = r#"{
|
||||
"correlation_id": "abc123-def456-ghi789",
|
||||
"msg_id": "msg-uuid-001",
|
||||
"timestamp": "2026-05-13T12:00:00.000Z",
|
||||
"send_to": "/agent/wine/api/v1/prompt",
|
||||
"msg_purpose": "chat",
|
||||
"sender_name": "js-webapp",
|
||||
"sender_id": "sender-uuid-001",
|
||||
"receiver_name": "rust-backend",
|
||||
"receiver_id": "",
|
||||
"reply_to": "/agent/wine/api/v1/response",
|
||||
"reply_to_msg_id": "",
|
||||
"broker_url": "nats://localhost:4222",
|
||||
"metadata": {},
|
||||
"payloads": [
|
||||
{
|
||||
"id": "payload-uuid-001",
|
||||
"dataname": "message",
|
||||
"payload_type": "text",
|
||||
"transport": "direct",
|
||||
"encoding": "base64",
|
||||
"size": 29,
|
||||
"data": "SGVsbG8gZnJvbSBKYXZhU2NyaXB0ISE=",
|
||||
"metadata": {"payload_bytes": 29}
|
||||
},
|
||||
{
|
||||
"id": "payload-uuid-002",
|
||||
"dataname": "user_data",
|
||||
"payload_type": "dictionary",
|
||||
"transport": "direct",
|
||||
"encoding": "json",
|
||||
"size": 58,
|
||||
"data": "eyJ0eXBlIjoiY2hhdCIsInNlbmRlciI6InNlcnZpY2VBIiwicmVjZWl2ZXIiOiJzZXJ2aWNlQiJ9",
|
||||
"metadata": {"payload_bytes": 58}
|
||||
}
|
||||
]
|
||||
}"#;
|
||||
|
||||
let options = SmartreceiveOptions::default();
|
||||
|
||||
match smartreceive(msg_json_str, &options).await {
|
||||
Ok(envelope) => {
|
||||
println!("=== Envelope Received ===");
|
||||
println!("Correlation ID: {}", envelope.correlation_id);
|
||||
println!("Message ID: {}", envelope.msg_id);
|
||||
println!("Subject: {}", envelope.send_to);
|
||||
println!("Purpose: {}", envelope.msg_purpose);
|
||||
println!("Sender: {}", envelope.sender_name);
|
||||
println!("Receiver: {}", envelope.receiver_name);
|
||||
println!("Payloads: {}", envelope.payloads.len());
|
||||
println!();
|
||||
|
||||
for payload in &envelope.payloads {
|
||||
println!("--- Payload: {} ---", payload.dataname);
|
||||
println!(" Type: {}", payload.payload_type);
|
||||
println!(" Transport: {}", payload.transport);
|
||||
println!(" Encoding: {}", payload.encoding);
|
||||
println!(" Size: {} bytes", payload.size);
|
||||
|
||||
// In a real scenario, you would deserialize payload.data here
|
||||
// based on payload_type to get the actual data
|
||||
match payload.payload_type.as_str() {
|
||||
"text" => {
|
||||
// For demonstration, decode the base64
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
|
||||
if payload.transport == "direct" {
|
||||
let decoded = BASE64.decode(&payload.data).unwrap();
|
||||
println!(" Data: {}", String::from_utf8_lossy(&decoded));
|
||||
} else {
|
||||
println!(" URL: {}", payload.data);
|
||||
}
|
||||
}
|
||||
"dictionary" => {
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
|
||||
if payload.transport == "direct" {
|
||||
let decoded = BASE64.decode(&payload.data).unwrap();
|
||||
let json: serde_json::Value = serde_json::from_slice(&decoded).unwrap();
|
||||
println!(" Data: {}", serde_json::to_string_pretty(&json).unwrap());
|
||||
}
|
||||
}
|
||||
other => {
|
||||
println!(" Data type: {}", other);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user