RequestBuilder

Struct RequestBuilder 

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

Builder pattern for constructing HttpRequest instances.

The RequestBuilder struct facilitates the creation of HttpRequest objects through a series of method calls. It allows for flexible and clear configuration of an HTTP request’s components such as method, URL, headers, and body.

§Fields

  • http_request: A temporary HttpRequest instance used to accumulate changes during the construction process. It holds the current state of the builder.
  • builder: A finalized HttpRequest instance that holds the result after the builder process has been completed. It is returned when the builder is finalized.

This builder simplifies the creation of HttpRequest objects, ensuring thread-safety and immutability of shared references, while providing a fluent API for constructing HTTP requests with various configurations.

Implementations§

Source§

impl RequestBuilder

Source

pub fn new() -> Self

Creates a new RequestBuilder instance.

§Returns
  • RequestBuilder - A new builder instance with default values.
Source

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

Sets the HTTP method to POST and the request URL.

§Arguments
  • str - The request URL.
§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

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

Sets the HTTP method to GET and the request URL.

§Arguments
  • str - The request URL.
§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn http1_1_only(&mut self) -> &mut Self

Forces HTTP/1.1 protocol version.

§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn http2_only(&mut self) -> &mut Self

Forces HTTP/2 protocol version.

§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn headers<K, V>(&mut self, header: HashMapXxHash3_64<K, V>) -> &mut Self
where K: ToString, V: ToString,

Sets request headers.

§Arguments
  • HashMapXxHash3_64<K, V> - The headers to set.
§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn json(&mut self, body: JsonValue) -> &mut Self

Sets JSON request body.

§Arguments
  • JsonValue - The JSON body data.
§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn text<T: ToString>(&mut self, body: T) -> &mut Self

Sets plain text request body.

§Arguments
  • T - The text body data (must implement ToString).
§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn body<T: Into<Vec<u8>>>(&mut self, body: T) -> &mut Self

Sets binary request body.

§Arguments
  • T - The binary body data (must implement Into<Vec>).
§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn timeout(&mut self, timeout: u64) -> &mut Self

Sets the timeout value for the current connection.

This method sets the timeout duration for the connection, which is used to determine how long the system should wait for a response before considering the connection attempt as failed. The timeout value is stored in an Arc to allow it to be shared safely across multiple threads if needed.

§Arguments
  • u64 - The timeout duration in seconds. This value will be used to configure the connection timeout.
§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn redirect(&mut self) -> &mut Self

Enables HTTP redirection for the request.

This method sets the redirect property of the http_request to true.

§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn unredirect(&mut self) -> &mut Self

Unenables HTTP redirection for the request.

This method sets the redirect property of the http_request to false.

§Returns
  • &mut RequestBuilder - The builder for method chaining.
Source

pub fn max_redirect_times(&mut self, num: usize) -> &mut Self

Sets the maximum number of allowed redirections for the HTTP request.

This method updates the max_redirect_times field in the configuration and returns a mutable reference to self to enable method chaining.

§Arguments
  • usize - The maximum number of redirections allowed. A value of 0 disables redirection.
§Returns
  • &mut RequestBuilder - A mutable reference to the current instance for method chaining.
§Notes

Ensure that the value provided to num is within a valid range. Excessively high values may lead to performance issues or unintended behavior.

Source

pub fn buffer(&mut self, buffer: usize) -> &mut Self

Sets the buffer size for the HTTP request configuration.

This method allows you to set the size of the buffer used for reading the HTTP response. It modifies the buffer field of the HTTP request’s configuration, which will be used when processing the response data.

§Arguments
  • usize - The size of the buffer to be used, in bytes.
§Returns
  • &mut RequestBuilder - Returns a mutable reference to self, allowing for method chaining.
Source

pub fn decode(&mut self) -> &mut Self

Enables automatic response decoding.

When enabled, the response body will be automatically decompressed if it is encoded using a supported compression format.

§Returns
  • &mut RequestBuilder - A mutable reference to the current instance, allowing for method chaining.
Source

pub fn undecode(&mut self) -> &mut Self

Disables automatic response decoding.

When disabled, the response body will not be automatically decompressed, and the raw encoded data will be returned as-is.

§Returns
  • &mut RequestBuilder - A mutable reference to the current instance, allowing for method chaining.
Source

pub fn http_proxy(&mut self, host: &str, port: u16) -> &mut Self

Sets an HTTP proxy for the request.

This method configures the request to use an HTTP proxy server.

§Arguments
  • str - The hostname or IP address of the proxy server.
  • u16 - The port number of the proxy server.
§Returns
  • &mut RequestBuilder - A mutable reference to the current instance, allowing for method chaining.
Source

pub fn https_proxy(&mut self, host: &str, port: u16) -> &mut Self

Sets an HTTPS proxy for the request.

This method configures the request to use an HTTPS proxy server.

§Arguments
  • str - The hostname or IP address of the proxy server.
  • u16 - The port number of the proxy server.
§Returns
  • &mut RequestBuilder - A mutable reference to the current instance, allowing for method chaining.
Source

pub fn socks5_proxy(&mut self, host: &str, port: u16) -> &mut Self

Sets a SOCKS5 proxy for the request.

This method configures the request to use a SOCKS5 proxy server.

§Arguments
  • str - The hostname or IP address of the proxy server.
  • u16 - The port number of the proxy server.
§Returns
  • &mut RequestBuilder - A mutable reference to the current instance, allowing for method chaining.
Source

pub fn http_proxy_auth( &mut self, host: &str, port: u16, username: &str, password: &str, ) -> &mut Self

Sets an HTTP proxy with authentication for the request.

This method configures the request to use an HTTP proxy server with username and password authentication.

§Arguments
  • str - The hostname or IP address of the proxy server.
  • u16 - The port number of the proxy server.
  • str - The username for proxy authentication.
  • str - The password for proxy authentication.
§Returns
  • &mut RequestBuilder - A mutable reference to the current instance, allowing for method chaining.
Source

pub fn https_proxy_auth( &mut self, host: &str, port: u16, username: &str, password: &str, ) -> &mut Self

Sets an HTTPS proxy with authentication for the request.

This method configures the request to use an HTTPS proxy server with username and password authentication.

§Arguments
  • str - The hostname or IP address of the proxy server.
  • u16 - The port number of the proxy server.
  • str - The username for proxy authentication.
  • str - The password for proxy authentication.
§Returns
  • &mut RequestBuilder - A mutable reference to the current instance, allowing for method chaining.
Source

pub fn socks5_proxy_auth( &mut self, host: &str, port: u16, username: &str, password: &str, ) -> &mut Self

Sets a SOCKS5 proxy with authentication for the request.

This method configures the request to use a SOCKS5 proxy server with username and password authentication.

§Arguments
  • str - The hostname or IP address of the proxy server.
  • u16 - The port number of the proxy server.
  • str - The username for proxy authentication.
  • str - The password for proxy authentication.
§Returns
  • &mut RequestBuilder - A mutable reference to the current instance, allowing for method chaining.
Source

pub fn build_async(&mut self) -> BoxAsyncRequestTrait

Finalizes the builder and returns a fully constructed async HttpRequest instance.

This method takes the current configuration stored in http_request, creates a new HttpRequest instance with the configuration, and resets the builder’s temporary state for further use.

§Returns
  • BoxAsyncRequestTrait - Returns a fully constructed BoxAsyncRequestTrait instance based on the current builder state.
Source

pub fn build_sync(&mut self) -> BoxRequestTrait

Finalizes the builder and returns a fully constructed synchronous HttpRequest instance.

This method takes the current configuration stored in http_request, creates a new HttpRequest instance with the configuration, and resets the builder’s temporary state for further use.

§Returns
  • BoxRequestTrait - Returns a fully constructed BoxRequestTrait instance based on the current builder state.

Trait Implementations§

Source§

impl Clone for RequestBuilder

Source§

fn clone(&self) -> RequestBuilder

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 RequestBuilder

Source§

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

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

impl Default for RequestBuilder

Source§

fn default() -> Self

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

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> AnySend for T
where T: Any + Send,

Source§

impl<T> AnySendClone for T
where T: Any + Send + Clone,

Source§

impl<T> AnySendSync for T
where T: Any + Send + Sync,

Source§

impl<T> AnySendSyncClone for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> AnySync for T
where T: Any + Sync,

Source§

impl<T> AnySyncClone for T
where T: Any + Sync + Clone,

Source§

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

Source§

impl<T> MaybeSendSync for T