use async_nats::Client;
use bytes::Bytes;
use std::time::Duration;
use crate::error::{DinkError, Result};
pub async fn request_with_auth(
client: &Client,
subject: &str,
data: &[u8],
api_key: &str,
timeout: Duration,
) -> Result<async_nats::Message> {
let mut headers = async_nats::HeaderMap::new();
headers.insert("Authorization", format!("Bearer {}", api_key).as_str());
let request = async_nats::Request::new()
.headers(headers)
.payload(Bytes::copy_from_slice(data))
.timeout(Some(timeout));
client
.send_request(subject.to_string(), request)
.await
.map_err(|e| DinkError::Nats(e.to_string()))
}
pub async fn request(
client: &Client,
subject: &str,
data: &[u8],
timeout: Duration,
) -> Result<async_nats::Message> {
let request = async_nats::Request::new()
.payload(Bytes::copy_from_slice(data))
.timeout(Some(timeout));
client
.send_request(subject.to_string(), request)
.await
.map_err(|e| DinkError::Nats(e.to_string()))
}
pub fn edge_service_subject(app_id: &str, edge_id: &str, service: &str, method: &str) -> String {
format!(
"edge.{}.{}.services.{}.{}",
app_id, edge_id, service, method
)
}
pub fn edge_service_stream_subject(
app_id: &str,
edge_id: &str,
service: &str,
method: &str,
) -> String {
format!(
"edge.{}.{}.services.{}.{}.stream",
app_id, edge_id, service, method
)
}
pub fn edge_service_wildcard(app_id: &str, edge_id: &str, service: &str) -> String {
format!("edge.{}.{}.services.{}.>", app_id, edge_id, service)
}
pub fn peer_directed_subject(
app_id: &str,
group_id: &str,
edge_id: &str,
service: &str,
method: &str,
) -> String {
format!(
"edge.{}.group.{}.{}.services.{}.{}",
app_id, group_id, edge_id, service, method
)
}
pub fn peer_broadcast_subject(app_id: &str, group_id: &str, service: &str, method: &str) -> String {
format!(
"edge.{}.group.{}.services.{}.{}",
app_id, group_id, service, method
)
}
pub fn peer_directed_wildcard(
app_id: &str,
group_id: &str,
edge_id: &str,
service: &str,
) -> String {
format!(
"edge.{}.group.{}.{}.services.{}.>",
app_id, group_id, edge_id, service
)
}
pub fn peer_broadcast_wildcard(app_id: &str, group_id: &str, service: &str) -> String {
format!("edge.{}.group.{}.services.{}.>", app_id, group_id, service)
}
pub fn center_group_subject(app_id: &str, group_id: &str, service: &str, method: &str) -> String {
format!(
"center.{}.group.{}.services.{}.{}",
app_id, group_id, service, method
)
}