Struct HttpClient

Source
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: The base_url property in the HttpClient 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: The default_headers property in the HttpClient struct is an optional field that can hold a HeaderMap. This field can be used to store default headers that will be included in every request made by the HttpClient. If no default headers are provided, this field will be None.
  • client: The client property in the HttpClient struct is of type Client. This likely represents an HTTP client that can be used to make HTTP requests to a server. The Client type is commonly used in Rust libraries like reqwest 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:

Source

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: The base_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: The default_headers parameter in the new function is an optional parameter of type Option<HeaderMap>. It allows you to provide a set of default headers to be included in each HTTP request made by the HttpClient. 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.

Source

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: The endpoint parameter in the get 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: The headers parameter in the get function is an optional HeaderMap 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 pass None 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.

Source

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: The endpoint parameter in the post 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: The headers parameter in the post function is an optional HeaderMap 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 pass None as the value for this parameter. If
  • body: The body parameter in the post 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.

Source

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: The endpoint 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: The headers parameter is an optional HeaderMap 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 pass None as the value for this parameter.
  • body: The body parameter in the put 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 the body 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.

Source

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: The endpoint parameter in the delete 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: The headers parameter in the delete function is an optional HeaderMap type. It allows you to pass additional headers to be included in the HTTP request. If no headers are needed, you can pass None 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.

Source

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: The endpoint parameter in the head 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: The headers parameter in the head function is an optional HeaderMap 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 pass None 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.

Source

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: The endpoint parameter in the patch 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: The headers parameter in the patch function is an optional HeaderMap 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 pass None as the value for this parameter. If
  • body: The body parameter in the patch 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 specified endpoint. If a value is provided for the body, it will
§Returns:

The patch function returns a Result containing either a Response or a boxed trait object implementing the Error trait.

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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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> 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
Source§

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