Struct kwap::resp::Resp

source · []
pub struct Resp<P: Platform> { /* private fields */ }
Expand description

Resp that uses Vec as the backing collection type

use kwap::platform::Std;
use kwap::resp::Resp;

fn main() {
  start_server(|req| {
    let mut resp = Resp::<Std>::for_request(&req).unwrap();

    resp.set_code(kwap::resp::code::CONTENT);
    resp.set_option(12, Some(50)); // Content-Format: application/json

    let payload = r#"""{
      "foo": "bar",
      "baz": "quux"
    }"""#;
    resp.set_payload(payload.bytes());

    resp
  });
}

fn start_server(f: impl FnOnce(kwap::req::Req<Std>) -> kwap::resp::Resp<Std>) {
  // servery things
}

Implementations

Create a new response for a given request.

If the request is CONfirmable, this will return Some(ACK).

If the request is NONconfirmable, this will return Some(NON).

If the request is EMPTY or RESET, this will return None.

use kwap::platform::{Message, Std};
use kwap::req::Req;
use kwap::resp::Resp;

// pretend this is an incoming request
let mut req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
req.set_msg_id(kwap_msg::Id(0));
req.set_msg_token(kwap_msg::Token(Default::default()));

let resp = Resp::<Std>::for_request(&req).unwrap();

let req_msg = Message::<Std>::from(req);
let resp_msg = Message::<Std>::from(resp);

// note that Req's default type is CON, so the response will be an ACK.
// this means that the token and id of the response will be the same
// as the incoming request.
assert_eq!(resp_msg.ty, kwap_msg::Type::Ack);
assert_eq!(req_msg.id, resp_msg.id);
assert_eq!(req_msg.token, resp_msg.token);

Create a response ACKnowledging an incoming request.

An ack response must be used when you receive a CON request.

You may choose to include the response payload in an ACK, but keep in mind that you might receive duplicate If you do need to ensure they receive your response, you

Create a CONfirmable response for an incoming request.

A confirmable response should be used when you receive a NON request and want to ensure the client receives your response

Note that it would be odd to respond to a CON request with an ACK followed by a CON response, because the client will keep resending the request until they receive the ACK.

The kwap runtime will continually retry sending this until an ACKnowledgement from the client is received.

Create a NONconfirmable response for an incoming request.

A non-confirmable response should be used when:

  • you receive a NON request and don’t need to ensure the client received the response
  • you receive a CON request and don’t need to ensure the client received the response (you must ACK this type of request separately)

Get the payload’s raw bytes

use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::Resp;

let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");

// pretend this is an incoming response
let resp = Resp::<Std>::for_request(&req).unwrap();

let data: Vec<u8> = resp.payload().copied().collect();

Get the message type

See kwap_msg::Type for more info

Get the message id

See kwap_msg::Id for more info

Get the message token

See kwap_msg::Token for more info

Get the payload and attempt to interpret it as an ASCII string

use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::Resp;

let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");

// pretend this is an incoming response
let mut resp = Resp::<Std>::for_request(&req).unwrap();
resp.set_payload("hello!".bytes());

let data: String = resp.payload_string().unwrap();

Get the response code

use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::{code, Resp};

// pretend this is an incoming request
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
let resp = Resp::<Std>::for_request(&req).unwrap();

assert_eq!(resp.code(), code::CONTENT);

Change the response code

use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::{code, Resp};

// pretend this is an incoming request
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
let mut resp = Resp::<Std>::for_request(&req).unwrap();

resp.set_code(code::INTERNAL_SERVER_ERROR);

Add a custom option to the response

If there was no room in the collection, returns the arguments back as Some(number, value). Otherwise, returns None.

use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::Resp;

// pretend this is an incoming request
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
let mut resp = Resp::<Std>::for_request(&req).unwrap();

resp.set_option(17, Some(50)); // Accept: application/json

Add a payload to this response

use kwap::platform::Std;
use kwap::req::Req;
use kwap::resp::Resp;

// pretend this is an incoming request
let req = Req::<Std>::get("1.1.1.1:5683".parse().unwrap(), "/hello");
let mut resp = Resp::<Std>::for_request(&req).unwrap();

// Maybe you have some bytes:
resp.set_payload(vec![1, 2, 3]);

// Or a string:
resp.set_payload("hello!".bytes());

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Try to convert into a collection of bytes 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.

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 resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

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.