Skip to main content

RequestFactory

Struct RequestFactory 

Source
pub struct RequestFactory { /* private fields */ }
Expand description

§Request Factory

This is a starting point for building HTTP requests with a configured server address, method, path, query parameters. Handy when you need to create multiple requests tha all require the same base configuration.

For more docs see Request::factory

Implementations§

Source§

impl RequestFactory

Source

pub fn builder(&self) -> RequestBuilder

§Start a new request builder

Initializes a new request builder with the base configuration that was set in the factory.

let factory = Request::factory()
    .address_port(3000)
    .method(Method::Put)
    .header("Content-Type", "application/json")
    .query_param("secret", "key")
    .build();

let request = factory.builder()
    .path("/api/v1/resource")
    .body_text(r#"{"foo":"bar"}"#)
    .build();

assert_eq!(request.method, Method::Put);
assert_eq!(request.path.as_str(), "/api/v1/resource");
assert_eq!(request.headers.first("Content-Type"), Some("application/json"));
assert_eq!(request.query.first("secret"), Some("key"));
assert_eq!(request.body.text(), Some(r#"{"foo":"bar"}"#));
assert_eq!(
    request.build_url().as_str(),
    "http://localhost:3000/api/v1/resource?secret=key"
);

Source

pub fn request(&self, method: Method, path: &str) -> RequestBuilder

Starts a builder with the specified method and path.

let factory = Request::factory().address_port(3000).build();
let req = factory
    .request(Method::Delete, "/api/v1/resource/42")
    .build();

assert_eq!(req.method, Method::Delete);
assert_eq!(req.path.as_str(), "/api/v1/resource/42");
assert_eq!(req.address.port, Some(3000));
assert_eq!(
    req.build_url().as_str(),
    "http://localhost:3000/api/v1/resource/42"
);

Source

pub fn get(&self, path: &str) -> RequestBuilder

GET path

let factory = RequestFactory::default();
let request = factory.get("/api/v1/resource").build();

assert_eq!(request.method, Method::Get);
assert_eq!(request.path.as_str(), "/api/v1/resource");
assert!(request.query.is_empty());

Source

pub fn post(&self, path: &str) -> RequestBuilder

POST path

let factory = Request::factory()
    .header("Content-Type", "application/json")
    .build();

let req = factory
    .post("/widgets")
    .body_text(r#"{"name":"foo"}"#)
    .build();

assert_eq!(req.method, Method::Post);
assert_eq!(req.headers.first("content-type"), Some("application/json"));
assert_eq!(req.path.as_str(), "/widgets");
assert_eq!(req.body.text(), Some(r#"{"name":"foo"}"#));

Source

pub fn put(&self, path: &str) -> RequestBuilder

PUT path

let factory = Request::factory()
    .header("Content-Type", "application/json")
    .build();

let req = factory
    .put("/foo")
    .body_text("fiz-buz")
    .build();

assert_eq!(req.method, Method::Put);
assert_eq!(req.headers.first("content-type"), Some("application/json"));
assert_eq!(req.path.as_str(), "/foo");
assert_eq!(req.body.text(), Some("fiz-buz"));

Source

pub fn delete(&self, path: &str) -> RequestBuilder

DELETE path

let factory = Request::factory()
    .header("Authorization", "Bearer some-token")
    .build();

let req = factory
    .delete("/foo/123")
    .build();

assert_eq!(req.method, Method::Delete);
assert_eq!(req.headers.first("authorization"), Some("Bearer some-token"));
assert_eq!(req.path.as_str(), "/foo/123");
assert!(req.body.is_empty());

Source

pub fn patch(&self, path: &str) -> RequestBuilder

PATCH path

let factory = Request::factory()
    .header("Authorization", "ApiKey $rofl$")
    .build();

let req = factory
    .patch("/my/email")
    .body_text("domain=example.com")
    .build();

assert_eq!(req.method, Method::Patch);
assert_eq!(req.headers.first("authorization"), Some("ApiKey $rofl$"));
assert_eq!(req.path.as_str(), "/my/email");
assert_eq!(req.body.text(), Some("domain=example.com"));

Trait Implementations§

Source§

impl Clone for RequestFactory

Source§

fn clone(&self) -> RequestFactory

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 Debug for RequestFactory

Source§

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

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

impl Default for RequestFactory

Source§

fn default() -> RequestFactory

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

impl From<Request> for RequestFactory

Source§

fn from(request: Request) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for RequestFactory

Source§

fn eq(&self, other: &RequestFactory) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for RequestFactory

Source§

impl StructuralPartialEq for RequestFactory

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> 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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,