Struct HttpRequestBuilder

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

An HTTP request builder.

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

§Examples

use ic_http_certification::{HttpRequestBuilder, Method};

let request = HttpRequestBuilder::new()
    .with_method(Method::GET)
    .with_url("/")
    .with_headers(vec![("X-Custom-Foo".into(), "Bar".into())])
    .with_body(&[1, 2, 3])
    .with_certificate_version(2)
    .build();

assert_eq!(request.method(), Method::GET);
assert_eq!(request.url(), "/");
assert_eq!(request.headers(), &[("X-Custom-Foo".into(), "Bar".into())]);
assert_eq!(request.body(), &[1, 2, 3]);
assert_eq!(request.certificate_version(), Some(2));

Implementations§

Source§

impl<'a> HttpRequestBuilder<'a>

Source

pub fn new() -> Self

Creates a new instance of the HttpRequestBuilder that can be used to construct an HttpRequest.

§Examples
use ic_http_certification::{HttpRequestBuilder, Method};

let request = HttpRequestBuilder::new()
    .with_method(Method::GET)
    .with_url("/")
    .with_headers(vec![("X-Custom-Foo".into(), "Bar".into())])
    .with_body(&[1, 2, 3])
    .with_certificate_version(2)
    .build();

assert_eq!(request.method(), Method::GET);
assert_eq!(request.url(), "/");
assert_eq!(request.headers(), &[("X-Custom-Foo".into(), "Bar".into())]);
assert_eq!(request.body(), &[1, 2, 3]);
assert_eq!(request.certificate_version(), Some(2));
Source

pub fn with_method(self, method: Method) -> Self

Set the HTTP method of the HttpRequest.

This function will accept both owned and borrowed values. By default, the method will be set to "GET".

§Examples
use ic_http_certification::{HttpRequestBuilder, Method};

let request = HttpRequestBuilder::new()
    .with_method(Method::GET)
    .build();

assert_eq!(request.method(), Method::GET);
Source

pub fn with_url(self, url: impl Into<String>) -> Self

Set the HTTP URL of the HttpRequest.

This function will accept both owned and borrowed values. By default, the URL will be set to "/".

§Examples
use ic_http_certification::HttpRequestBuilder;

let request = HttpRequestBuilder::new()
    .with_url("/")
    .build();

assert_eq!(request.url(), "/");
Source

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

Set the HTTP headers of the HttpRequest.

By default the headers will be an empty array.

§Examples
use ic_http_certification::{HttpRequestBuilder, HeaderField};

let request = HttpRequestBuilder::new()
    .with_headers(vec![("X-Custom-Foo".into(), "Bar".into())])
    .build();

assert_eq!(request.headers(), &[("X-Custom-Foo".into(), "Bar".into())]);
Source

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

Set the HTTP body of the HttpRequest.

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

§Examples
use ic_http_certification::HttpRequestBuilder;

let request = HttpRequestBuilder::new()
    .with_body(&[1, 2, 3])
    .build();

assert_eq!(request.body(), &[1, 2, 3]);
Source

pub fn with_certificate_version(self, certificate_version: u16) -> Self

Set the max response verification vwersion to use in the crate::HttpResponse certificate.

By default, the certificate version will be None, which is equivalent to setting it to version 1.

§Examples
use ic_http_certification::HttpRequestBuilder;

let request = HttpRequestBuilder::new()
    .with_certificate_version(2)
    .build();

assert_eq!(request.certificate_version(), Some(2));
Source

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

Build an HttpRequest from the builder.

If the method is not set, it will default to "GET". If the URL is not set, it will default to "/". If the certificate version is not set, it will default to 1. If the headers or body are not set, they will default to empty arrays.

§Examples
use ic_http_certification::{HttpRequestBuilder, Method};

let request = HttpRequestBuilder::new()
    .with_method(Method::GET)
    .with_url("/")
    .with_headers(vec![("X-Custom-Foo".into(), "Bar".into())])
    .with_body(&[1, 2, 3])
    .with_certificate_version(2)
    .build();

assert_eq!(request.method(), Method::GET);
assert_eq!(request.url(), "/");
assert_eq!(request.headers(), &[("X-Custom-Foo".into(), "Bar".into())]);
assert_eq!(request.body(), &[1, 2, 3]);
assert_eq!(request.certificate_version(), Some(2));
Source

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

Build an HttpUpdateRequest from the builder.

If the method is not set, it will default to "GET". If the URL is not set, it will default to "/". If the headers or body are not set, they will default to empty arrays.

§Examples
use ic_http_certification::{HttpRequestBuilder, Method};

let update_request = HttpRequestBuilder::new()
    .with_method(Method::GET)
    .with_url("/")
    .with_headers(vec![("X-Custom-Foo".into(), "Bar".into())])
    .with_body(&[1, 2, 3])
    .build_update();

assert_eq!(update_request.method(), Method::GET);
assert_eq!(update_request.url(), "/");
assert_eq!(update_request.headers(), &[("X-Custom-Foo".into(), "Bar".into())]);
assert_eq!(update_request.body(), &[1, 2, 3]);

Trait Implementations§

Source§

impl<'a> Clone for HttpRequestBuilder<'a>

Source§

fn clone(&self) -> HttpRequestBuilder<'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 HttpRequestBuilder<'a>

Source§

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

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

impl<'a> Default for HttpRequestBuilder<'a>

Source§

fn default() -> HttpRequestBuilder<'a>

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

Auto Trait Implementations§

§

impl<'a> Freeze for HttpRequestBuilder<'a>

§

impl<'a> RefUnwindSafe for HttpRequestBuilder<'a>

§

impl<'a> Send for HttpRequestBuilder<'a>

§

impl<'a> Sync for HttpRequestBuilder<'a>

§

impl<'a> Unpin for HttpRequestBuilder<'a>

§

impl<'a> UnwindSafe for HttpRequestBuilder<'a>

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.