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_url
property in theHttpClient
struct 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_headers
property in theHttpClient
struct 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
: Theclient
property in theHttpClient
struct is of typeClient
. This likely represents an HTTP client that can be used to make HTTP requests to a server. TheClient
type is commonly used in Rust libraries likereqwest
for 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_url
parameter 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_headers
parameter in thenew
function 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
: Theendpoint
parameter in theget
function 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
: Theheaders
parameter in theget
function is an optionalHeaderMap
type. 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 passNone
as 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
: Theendpoint
parameter in thepost
function 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
: Theheaders
parameter in thepost
function is an optionalHeaderMap
type. 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 passNone
as the value for this parameter. Ifbody
: Thebody
parameter in thepost
function 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
: Theendpoint
parameter 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
: Theheaders
parameter is an optionalHeaderMap
type, 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 passNone
as the value for this parameter.body
: Thebody
parameter in theput
function 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 thebody
parameter, 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
: Theendpoint
parameter in thedelete
function 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
: Theheaders
parameter in thedelete
function is an optionalHeaderMap
type. It allows you to pass additional headers to be included in the HTTP request. If no headers are needed, you can passNone
as 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
: Theendpoint
parameter in thehead
function 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
: Theheaders
parameter in thehead
function is an optionalHeaderMap
type. 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 passNone
as 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
: Theendpoint
parameter in thepatch
function 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
: Theheaders
parameter in thepatch
function is an optionalHeaderMap
type. 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 passNone
as the value for this parameter. Ifbody
: Thebody
parameter in thepatch
function 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.