Struct HttpResponseBuilder

Source
pub struct HttpResponseBuilder<'a> { /* private fields */ }
Expand description

An HTTP response builder.

This type can be used to construct an instance of an HttpResponse using a builder-like pattern.

§Examples

use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::builder()
    .with_status_code(StatusCode::OK)
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .with_body(b"Hello, World!")
    .with_upgrade(false)
    .build();

assert_eq!(response.status_code(), StatusCode::OK);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Hello, World!");
assert_eq!(response.upgrade(), Some(false));

Implementations§

Source§

impl<'a> HttpResponseBuilder<'a>

Source

pub fn new() -> Self

Creates a new instance of the HttpResponseBuilder that can be used to constract an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::builder()
    .with_status_code(StatusCode::OK)
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .with_body(b"Hello, World!")
    .with_upgrade(false)
    .build();

assert_eq!(response.status_code(), StatusCode::OK);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Hello, World!");
assert_eq!(response.upgrade(), Some(false));
Source

pub fn with_status_code(self, status_code: StatusCode) -> Self

Sets the status code of the HTTP response.

By default, the status code will be set to 200.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::builder()
    .with_status_code(StatusCode::OK)
    .build();

assert_eq!(response.status_code(), StatusCode::OK);
Source

pub fn with_headers(self, headers: Vec<HeaderField>) -> Self

Sets the headers of the HTTP response.

By default, the headers will be set to an empty array.

§Examples
use ic_http_certification::HttpResponse;

let response = HttpResponse::builder()
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .build();

assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
Source

pub fn with_body(self, body: impl Into<Cow<'a, [u8]>>) -> Self

Sets the body of the HTTP response.

This function will accept both owned and borrowed values. By default, the body will be set to an empty array.

§Examples
use ic_http_certification::HttpResponse;

let response = HttpResponse::builder()
    .with_body(b"Hello, World!")
    .build();

assert_eq!(response.body(), b"Hello, World!");
Source

pub fn with_upgrade(self, upgrade: bool) -> Self

Sets the upgrade flag of the HTTP response. This will determine if the HTTP Gateway will upgrade the request to an update call.

By default, the upgrade flag will be set to None, which is the same as Some(false).

§Examples
use ic_http_certification::HttpResponse;

let response = HttpResponse::builder()
    .with_upgrade(true)
    .build();

assert_eq!(response.upgrade(), Some(true));
Source

pub fn build(self) -> HttpResponse<'a>

Build an HttpResponse from the builder.

If the status code is not set, it will default to 200. If the upgrade flag is not set, it will default to None. If the headers or body are not set, they will default to empty arrays.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::builder()
    .with_status_code(StatusCode::OK)
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .with_body(b"Hello, World!")
    .with_upgrade(false)
    .build();

assert_eq!(response.status_code(), StatusCode::OK);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Hello, World!");
assert_eq!(response.upgrade(), Some(false));
Source

pub fn build_update(self) -> HttpUpdateResponse<'a>

Build an HttpUpdateResponse from the builder.

If the status code is not set, it will default to 200. If the headers or body are not set, they will default to empty arrays.

§Examples
use ic_http_certification::{HttpResponse, HttpUpdateResponse, StatusCode};

let response = HttpResponse::builder()
    .with_status_code(StatusCode::OK)
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .with_body(b"Hello, World!")
    .build();

let update_response = HttpUpdateResponse::from(response);

assert_eq!(update_response.status_code(), StatusCode::OK);
assert_eq!(update_response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(update_response.body(), b"Hello, World!");

Trait Implementations§

Source§

impl<'a> Clone for HttpResponseBuilder<'a>

Source§

fn clone(&self) -> HttpResponseBuilder<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for HttpResponseBuilder<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for HttpResponseBuilder<'a>

Source§

fn default() -> HttpResponseBuilder<'a>

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

impl<'a> From<HttpResponse<'a>> for HttpResponseBuilder<'a>

Source§

fn from(response: HttpResponse<'a>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.