http_request

Struct HttpRequestBuilder

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

Builder pattern for constructing HttpRequest instances.

The HttpRequestBuilder 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.

§Traits Implemented

  • Debug: Enables the ability to format and print the HttpRequestBuilder for debugging purposes.
  • Clone: Allows for cloning of the HttpRequestBuilder and its internal components.
  • PartialEq: Provides equality comparison between two HttpRequestBuilder instances.

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 HttpRequestBuilder

Source

pub fn new() -> Self

Creates a new instance of the builder with default values.

This method initializes the HttpRequestBuilder with default values for all fields.

§Returns

Returns a new instance of HttpRequestBuilder.

Source

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

Sets the HTTP method for the request.

This method allows you to specify the HTTP method (e.g., GET, POST) for the request being built.

§Parameters
  • methods: The HTTP method to be set for the request.
§Returns

Returns a mutable reference to the HttpRequestBuilder to allow method chaining.

Source

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

Sets the HTTP method for the request.

This method allows you to specify the HTTP method (e.g., GET, POST) for the request being built.

§Parameters
  • methods: The HTTP method to be set for the request.
§Returns

Returns a mutable reference to the HttpRequestBuilder to allow method chaining.

Source

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

Sets the HTTP version to 1.1 for the request configuration.

This method updates the HTTP version to HTTP1_1 for the current http_request configuration. It allows the user to force the request to use HTTP 1.1 only, overriding any other version that may have been previously set.

§Returns

Returns a mutable reference to self to allow method chaining.

Source

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

Sets the HTTP version to 2.0 for the request configuration.

This method updates the HTTP version to HTTP2 for the current http_request configuration. It allows the user to force the request to use HTTP 2.0 only, overriding any other version that may have been previously set.

§Returns

Returns a mutable reference to self to allow method chaining.

Source

pub fn headers( &mut self, header: HashMap<&'static str, &'static str>, ) -> &mut Self

Sets the headers for the request.

This method allows you to specify the headers for the request being built. Existing headers may be merged with the provided ones.

§Parameters
  • header: The headers to be set for the request.
§Returns

Returns a mutable reference to the HttpRequestBuilder to allow method chaining.

Source

pub fn json(&mut self, body: HashMap<&'static str, &'static str>) -> &mut Self

Sets the JSON body of the request.

This method allows you to set the body of the request as JSON data. If there is existing body data, it will be replaced with the provided JSON data.

§Parameters
  • body: The JSON body data to be set for the request.
§Returns

Returns a mutable reference to the HttpRequestBuilder to allow method chaining.

Source

pub fn text(&mut self, body: &'static str) -> &mut Self

Sets the text body of the request.

This method allows you to set the body of the request as plain text. If there is existing body data, it will be replaced with the provided text data.

§Parameters
  • body: The text body data to be set for the request.
§Returns

Returns a mutable reference to the HttpRequestBuilder to allow method chaining.

Source

pub fn body(&mut self, body: &'static [u8]) -> &mut Self

Sets the HTTP request body to the given binary content.

This method assigns the provided binary data to the body of the HTTP request. The body is wrapped in an Arc to enable shared ownership and safe concurrency.

§Parameters
  • body - A BodyBinary representing the binary content to be used as the HTTP request body.
§Returns

Returns a mutable reference to the current instance of the struct, allowing method chaining.

§Notes

This method overrides any previously set body. Use it when you want to assign binary content specifically as the body of the HTTP request.

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.

§Parameters
  • timeout: The timeout duration in seconds. This value will be used to configure the connection timeout.
§Returns

Returns a mutable reference to the HttpRequestBuilder to allow 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. It returns a mutable reference to the current instance, allowing 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. It returns a mutable reference to the current instance, allowing 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.

§Parameters
  • num - The maximum number of redirections allowed. A value of 0 disables redirection.
§Returns

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.

§Parameters
  • buffer: The size of the buffer to be used, in bytes.
§Returns

Returns a mutable reference to self, allowing for method chaining.

Source

pub fn builder(&mut self) -> HttpRequest

Finalizes the builder and returns a fully constructed 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

Returns a fully constructed HttpRequest instance based on the current builder state.

Trait Implementations§

Source§

impl Clone for HttpRequestBuilder

Source§

fn clone(&self) -> HttpRequestBuilder

Returns a copy 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 HttpRequestBuilder

Source§

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

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

impl Default for HttpRequestBuilder

Provides a builder pattern implementation for constructing HttpRequest instances.

The HttpRequestBuilder struct is used to create and configure HttpRequest objects through a series of method calls, enabling a flexible and clear way to construct requests.

§Traits Implemented

  • Default: Provides a default instance of the builder, initializing all fields with default values.

§Methods

  • new: Creates a new instance of the builder with default values.
  • methods: Sets the HTTP method for the request (e.g., GET, POST).
  • url: Sets the target URL of the request.
  • headers: Updates the headers of the request. Existing headers may be merged with the provided ones.
  • body: Updates the body of the request. Existing body data may be merged with the provided data.
  • builder: Finalizes the configuration and returns a fully constructed HttpRequest instance. Resets the builder’s temporary state for subsequent use.

This builder simplifies the construction of HttpRequest objects while maintaining thread safety and ensuring immutability for shared references where applicable.

Source§

fn default() -> HttpRequestBuilder

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

impl PartialEq for HttpRequestBuilder

Source§

fn eq(&self, other: &HttpRequestBuilder) -> 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 StructuralPartialEq for HttpRequestBuilder

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

impl<T> MaybeSendSync for T