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>
impl<'a> HttpResponseBuilder<'a>
Sourcepub fn new() -> Self
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));
Sourcepub fn with_status_code(self, status_code: StatusCode) -> Self
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);
Sourcepub fn with_headers(self, headers: Vec<HeaderField>) -> Self
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())]);
Sourcepub fn with_body(self, body: impl Into<Cow<'a, [u8]>>) -> Self
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!");
Sourcepub fn with_upgrade(self, upgrade: bool) -> Self
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));
Sourcepub fn build(self) -> HttpResponse<'a>
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));
Sourcepub fn build_update(self) -> HttpUpdateResponse<'a>
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>
impl<'a> Clone for HttpResponseBuilder<'a>
Source§fn clone(&self) -> HttpResponseBuilder<'a>
fn clone(&self) -> HttpResponseBuilder<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more