Struct async_nats::Request
source · pub struct Request { /* private fields */ }Expand description
Used for building customized requests.
Implementations§
source§impl Request
impl Request
sourcepub fn new() -> Request
pub fn new() -> Request
Examples found in repository?
src/client.rs (line 283)
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
pub async fn request(&self, subject: String, payload: Bytes) -> Result<Message, Error> {
trace!("request sent to subject: {} ({})", subject, payload.len());
let request = Request::new().payload(payload);
self.send_request(subject, request).await
}
/// Sends the request with headers.
///
/// # Examples
/// ```no_run
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
/// let client = async_nats::connect("demo.nats.io").await?;
/// let mut headers = async_nats::HeaderMap::new();
/// headers.insert("Key", "Value");
/// let response = client.request_with_headers("service".into(), headers, "data".into()).await?;
/// # Ok(())
/// # }
/// ```
pub async fn request_with_headers(
&self,
subject: String,
headers: HeaderMap,
payload: Bytes,
) -> Result<Message, Error> {
let request = Request::new().headers(headers).payload(payload);
self.send_request(subject, request).await
}sourcepub fn payload(self, payload: Bytes) -> Request
pub fn payload(self, payload: Bytes) -> Request
Sets the payload of the request. If not used, empty payload will be sent.
Examples
let client = async_nats::connect("demo.nats.io").await?;
let request = async_nats::Request::new().payload("data".into());
client.send_request("service".into(), request).await?;Examples found in repository?
src/client.rs (line 283)
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
pub async fn request(&self, subject: String, payload: Bytes) -> Result<Message, Error> {
trace!("request sent to subject: {} ({})", subject, payload.len());
let request = Request::new().payload(payload);
self.send_request(subject, request).await
}
/// Sends the request with headers.
///
/// # Examples
/// ```no_run
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), async_nats::Error> {
/// let client = async_nats::connect("demo.nats.io").await?;
/// let mut headers = async_nats::HeaderMap::new();
/// headers.insert("Key", "Value");
/// let response = client.request_with_headers("service".into(), headers, "data".into()).await?;
/// # Ok(())
/// # }
/// ```
pub async fn request_with_headers(
&self,
subject: String,
headers: HeaderMap,
payload: Bytes,
) -> Result<Message, Error> {
let request = Request::new().headers(headers).payload(payload);
self.send_request(subject, request).await
}sourcepub fn headers(self, headers: HeaderMap) -> Request
pub fn headers(self, headers: HeaderMap) -> Request
Sets the headers of the requests.
Examples
use std::str::FromStr;
let client = async_nats::connect("demo.nats.io").await?;
let mut headers = async_nats::HeaderMap::new();
headers.insert("X-Example", async_nats::HeaderValue::from_str("Value").unwrap());
let request = async_nats::Request::new()
.headers(headers)
.payload("data".into());
client.send_request("service".into(), request).await?;sourcepub fn timeout(self, timeout: Option<Duration>) -> Request
pub fn timeout(self, timeout: Option<Duration>) -> Request
Sets the custom timeout of the request. Overrides default Client timeout. Setting it to Option::None disables the timeout entirely which might result in deadlock. To use default timeout, simply do not call this function.
Examples
let client = async_nats::connect("demo.nats.io").await?;
let request = async_nats::Request::new()
.timeout(Some(std::time::Duration::from_secs(15)))
.payload("data".into());
client.send_request("service".into(), request).await?;sourcepub fn inbox(self, inbox: String) -> Request
pub fn inbox(self, inbox: String) -> Request
Sets custom inbox for this request. Overrides both customized and default Client Inbox.
Examples
use std::str::FromStr;
let client = async_nats::connect("demo.nats.io").await?;
let request = async_nats::Request::new()
.inbox("custom_inbox".into())
.payload("data".into());
client.send_request("service".into(), request).await?;