Struct async_nats::Request

source ·
pub struct Request { /* private fields */ }
Expand description

Used for building customized requests.

Implementations§

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
    }

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
    }

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?;
Examples found in repository?
src/client.rs (line 307)
301
302
303
304
305
306
307
308
309
    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
    }

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?;

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?;

Trait Implementations§

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more