RequestBuilder

Struct RequestBuilder 

Source
pub struct RequestBuilder<'a, C = Client, B = InMemoryBody> {
    pub version: Version,
    pub method: Method,
    pub uri: Uri,
    pub headers: HeaderMap,
    pub body: Option<B>,
    pub middlewares: Vec<Arc<dyn Middleware>>,
    /* private fields */
}
Expand description

Provide a custom request builder for several reasons:

  • The required reason is have it implement IntoFuture, so that it can be directly awaited.
  • The secondary reasons is directly storing client & middlewares on the RequestBuilder. In theory it could be stored on Request.extensions, but that’s less explicit.
  • It’s also nice to not require implementing an Extension trait to get all the convenience methods on http::request::RequestBuilder

Middlewares are used in order (first to last).

Fields§

§version: Version§method: Method§uri: Uri§headers: HeaderMap§body: Option<B>§middlewares: Vec<Arc<dyn Middleware>>

Implementations§

Source§

impl<'a> RequestBuilder<'a, ()>

Source

pub fn get(url: &str) -> RequestBuilder<'a, ()>

Source

pub fn post(url: &str) -> RequestBuilder<'a, ()>

Source

pub fn put(url: &str) -> RequestBuilder<'a, ()>

Source

pub fn delete(url: &str) -> RequestBuilder<'a, ()>

Source

pub fn head(url: &str) -> RequestBuilder<'a, ()>

Source§

impl<'a, C> RequestBuilder<'a, C>

Source

pub fn new( client: &'a C, method: Method, uri: Uri, ) -> RequestBuilder<'a, C, InMemoryBody>

Source

pub fn form<S: Serialize>(self, obj: S) -> Self

Source

pub fn set_json<S: Serialize>(self, obj: S) -> Self

Overwrite the current body with the provided JSON object.

Source

pub fn json<S: Serialize>(self, obj: S) -> Self

Add the provided JSON object to the current body.

Source

pub fn bytes(self, bytes: Vec<u8>) -> Self

Sets content-type to application/octet-stream and the body to the supplied bytes.

Source

pub fn text(self, text: String) -> Self

Sets content-type to text/plain and the body to the supplied text.

Source

pub fn multipart<B>(self, form: Form<B>) -> Self
where Form<B>: Into<Vec<u8>>,

Source§

impl<'a> RequestBuilder<'a>

Source

pub async fn send(self) -> ProtocolResult<Response>

There are two ways to trigger the request. Immediately using .await will call the IntoFuture implementation which also awaits the body. If you want to await them separately, use this method .send()

Source§

impl<'a, C, B: Default> RequestBuilder<'a, C, B>

Source

pub fn build(self) -> Request<B>

Source

pub fn into_req_and_middleware(self) -> (Request<B>, Vec<Arc<dyn Middleware>>)

Source§

impl<'a, C, B> RequestBuilder<'a, C, B>

Source

pub fn for_client(client: &'a C) -> RequestBuilder<'a, C>

Source

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

Source

pub fn url(self, uri: &str) -> Self

Source

pub fn set_headers<S: AsRef<str>, I: Iterator<Item = (S, S)>>( self, headers: I, ) -> Self

Source

pub fn headers<S: AsRef<str>, I: Iterator<Item = (S, S)>>( self, headers: I, ) -> Self

Source

pub fn header<K: TryInto<HeaderName>>(self, key: K, value: &str) -> Self
where <K as TryInto<HeaderName>>::Error: Debug,

Source

pub fn cookie(self, key: &str, value: &str) -> Self

Source

pub fn bearer_auth(self, token: &str) -> Self

Source

pub fn token_auth(self, token: &str) -> Self

Source

pub fn basic_auth(self, token: &str) -> Self

Source

pub fn set_query<S: Serialize>(self, obj: S) -> Self

Overwrite the query with the provided value.

Source

pub fn query(self, k: &str, v: &str) -> Self

Add a url query parameter, but keep existing parameters.

§Examples
use httpclient::{Client, RequestBuilder, Method};
let client = Client::new();
let mut r = RequestBuilder::new(&client, Method::GET, "http://example.com/foo?a=1".parse().unwrap());
r = r.query("b", "2");
assert_eq!(r.uri.to_string(), "http://example.com/foo?a=1&b=2");
Source

pub fn content_type(self, content_type: &str) -> Self

Source

pub fn body(self, body: B) -> Self

Warning: Does not set content-type!

Source

pub fn set_middlewares(self, middlewares: Vec<Arc<dyn Middleware>>) -> Self

Source

pub fn middleware(self, middleware: Arc<dyn Middleware>) -> Self

Trait Implementations§

Source§

impl<'a, C: Debug, B: Debug> Debug for RequestBuilder<'a, C, B>

Source§

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

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

impl<'a> IntoFuture for RequestBuilder<'a, Client>

Source§

type Output = Result<Response<InMemoryBody>, Error<Response<InMemoryBody>>>

The output that the future will produce on completion.
Source§

type IntoFuture = Pin<Box<dyn Future<Output = <RequestBuilder<'a> as IntoFuture>::Output> + Send + 'a>>

Which kind of future are we turning this into?
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more

Auto Trait Implementations§

§

impl<'a, C = Client, B = InMemoryBody> !Freeze for RequestBuilder<'a, C, B>

§

impl<'a, C = Client, B = InMemoryBody> !RefUnwindSafe for RequestBuilder<'a, C, B>

§

impl<'a, C, B> Send for RequestBuilder<'a, C, B>
where C: Sync, B: Send,

§

impl<'a, C, B> Sync for RequestBuilder<'a, C, B>
where C: Sync, B: Sync,

§

impl<'a, C, B> Unpin for RequestBuilder<'a, C, B>
where B: Unpin,

§

impl<'a, C = Client, B = InMemoryBody> !UnwindSafe for RequestBuilder<'a, C, B>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more