pub struct HttpClient { /* private fields */ }Expand description
The HttpClient struct in Rust represents an HTTP client with a base URL, optional default headers,
and a client instance.
§Properties:
base_url: Thebase_urlproperty in theHttpClientstruct represents the base URL that will be used for making HTTP requests. This URL serves as the starting point for constructing full URLs for the requests sent by the HTTP client.default_headers: Thedefault_headersproperty in theHttpClientstruct is an optional field that can hold aHeaderMap. This field can be used to store default headers that will be included in every request made by theHttpClient. If no default headers are provided, this field will beNone.client: Theclientproperty in theHttpClientstruct is of typeClient. This likely represents an HTTP client that can be used to make HTTP requests to a server. TheClienttype is commonly used in Rust libraries likereqwestfor sending HTTP requests and handling responses.
Implementations§
Source§impl HttpClient
The impl HttpClient { ... } block in the Rust code snippet is implementing methods for the
HttpClient struct. Here’s a breakdown of what each method is doing:
impl HttpClient
The impl HttpClient { ... } block in the Rust code snippet is implementing methods for the
HttpClient struct. Here’s a breakdown of what each method is doing:
Sourcepub fn new(
base_url: &str,
default_headers: Option<HeaderMap>,
) -> Result<Self, Box<dyn Error>>
pub fn new( base_url: &str, default_headers: Option<HeaderMap>, ) -> Result<Self, Box<dyn Error>>
The function new creates a new instance of an HttpClient with a base URL, default headers,
and a new client.
§Arguments:
base_url: Thebase_urlparameter is a string reference (&str) that represents the base URL for the HTTP client. This is the URL that will be used as the starting point for making HTTP requests.default_headers: Thedefault_headersparameter in thenewfunction is an optional parameter of typeOption<HeaderMap>. It allows you to provide a set of default headers to be included in each HTTP request made by theHttpClient. If no default headers are provided, you can pass `None
§Returns:
The new function is returning a Result containing an instance of HttpClient if the URL
parsing is successful and the HttpClient struct is properly initialized with the provided base
URL, default headers, and a new Client instance.
Sourcepub async fn get(
&self,
endpoint: &str,
headers: Option<HeaderMap>,
) -> Result<Response, Box<dyn Error>>
pub async fn get( &self, endpoint: &str, headers: Option<HeaderMap>, ) -> Result<Response, Box<dyn Error>>
This Rust function performs an asynchronous HTTP GET request with specified headers.
§Arguments:
endpoint: Theendpointparameter in thegetfunction represents the specific endpoint or path that you want to access on the base URL. It is a string that typically corresponds to a specific resource or action on the server.headers: Theheadersparameter in thegetfunction is an optionalHeaderMaptype. It allows you to pass additional headers that will be merged with the default headers before making the HTTP request. If no additional headers are needed, you can passNoneas the value for this parameter
§Returns:
The get function returns a Result containing a Response if the request is successful, or a
Box<dyn Error> if an error occurs during the request.
Sourcepub async fn post(
&self,
endpoint: &str,
headers: Option<HeaderMap>,
body: Option<&str>,
) -> Result<Response, Box<dyn Error>>
pub async fn post( &self, endpoint: &str, headers: Option<HeaderMap>, body: Option<&str>, ) -> Result<Response, Box<dyn Error>>
The function post sends an asynchronous POST request with optional headers and body, returning
a Result containing the response.
§Arguments:
endpoint: Theendpointparameter in thepostfunction represents the specific endpoint or route that you want to send a POST request to. It is a string that typically comes after the base URL of the API you are interacting with.headers: Theheadersparameter in thepostfunction is an optionalHeaderMaptype. It allows you to pass additional headers to be included in the HTTP request. If you don’t need to include any extra headers, you can passNoneas the value for this parameter. Ifbody: Thebodyparameter in thepostfunction represents the payload or data that you want to send in the HTTP request body. It is an optional parameter, meaning you can choose to include a body or not when making a POST request. If you provide a body, it should be a string
§Returns:
The post function returns a Result containing a Response if the operation is successful,
or a Box containing a dynamic error trait object if an error occurs.
Sourcepub async fn put(
&self,
endpoint: &str,
headers: Option<HeaderMap>,
body: Option<&str>,
) -> Result<Response, Box<dyn Error>>
pub async fn put( &self, endpoint: &str, headers: Option<HeaderMap>, body: Option<&str>, ) -> Result<Response, Box<dyn Error>>
The function put sends an HTTP PUT request with optional headers and body, and returns the
response asynchronously.
§Arguments:
endpoint: Theendpointparameter is a string that represents the specific endpoint or route that you want to send a PUT request to. It is typically a part of the URL path after the base URL.headers: Theheadersparameter is an optionalHeaderMaptype, which represents a collection of HTTP headers. It allows you to pass additional headers along with the request. If no headers are needed, you can passNoneas the value for this parameter.body: Thebodyparameter in theputfunction is an optional reference to a string. It represents the body content that will be sent in the HTTP PUT request. If a value is provided for thebodyparameter, it will be included in the request; otherwise, the request will be
§Returns:
The put function returns a Result containing a Response if the operation is successful, or
a Box containing a trait object that implements the Error trait if an error occurs.
Sourcepub async fn delete(
&self,
endpoint: &str,
headers: Option<HeaderMap>,
) -> Result<Response, Box<dyn Error>>
pub async fn delete( &self, endpoint: &str, headers: Option<HeaderMap>, ) -> Result<Response, Box<dyn Error>>
The function delete sends a DELETE request to a specified endpoint with optional headers and
returns the response asynchronously.
§Arguments:
endpoint: Theendpointparameter in thedeletefunction is a reference to a string that represents the specific endpoint or resource path that you want to delete on the server. It is used to construct the complete URL for the DELETE request.headers: Theheadersparameter in thedeletefunction is an optionalHeaderMaptype. It allows you to pass additional headers to be included in the HTTP request. If no headers are needed, you can passNoneas the value for this parameter. If you do need to include
§Returns:
The delete function returns a Result containing a Response if the operation is successful,
or a Box<dyn Error> if an error occurs.
Sourcepub async fn head(
&self,
endpoint: &str,
headers: Option<HeaderMap>,
) -> Result<Response, Box<dyn Error>>
pub async fn head( &self, endpoint: &str, headers: Option<HeaderMap>, ) -> Result<Response, Box<dyn Error>>
This Rust function sends a HEAD request to a specified endpoint with optional headers and returns the response asynchronously.
§Arguments:
endpoint: Theendpointparameter in theheadfunction represents the specific path or resource on the server that you want to send a HTTP HEAD request to. It is typically a string that specifies the endpoint URL relative to the base URL of the API.headers: Theheadersparameter in theheadfunction is an optionalHeaderMaptype. It allows you to pass additional headers to be included in the HTTP request. If you don’t need to include any extra headers, you can passNoneas the value for this parameter. If
§Returns:
The head function returns a Result containing a Response if the operation is successful,
or a Box<dyn Error> if an error occurs.
Sourcepub async fn patch(
&self,
endpoint: &str,
headers: Option<HeaderMap>,
body: Option<&str>,
) -> Result<Response, Box<dyn Error>>
pub async fn patch( &self, endpoint: &str, headers: Option<HeaderMap>, body: Option<&str>, ) -> Result<Response, Box<dyn Error>>
The function patch sends a PATCH request to a specified endpoint with optional headers and body,
and returns the response asynchronously.
§Arguments:
endpoint: Theendpointparameter in thepatchfunction is a string that represents the specific endpoint or route that you want to send a PATCH request to. It is typically a part of the URL after the base URL.headers: Theheadersparameter in thepatchfunction is an optionalHeaderMaptype. It allows you to pass additional headers to be included in the HTTP request. If you don’t need to include any extra headers, you can passNoneas the value for this parameter. Ifbody: Thebodyparameter in thepatchfunction is an optional reference to a string (Option<&str>). It represents the body content that will be sent in the HTTP request when making a PATCH request to the specifiedendpoint. If a value is provided for thebody, it will
§Returns:
The patch function returns a Result containing either a Response or a boxed trait object
implementing the Error trait.