Skip to main content

HttpClient

Struct HttpClient 

Source
pub struct HttpClient {}
Expand description

An HTTP client interface for asynchronous HTTP requests. All requests run on background threads and return a request id that can be used with cancel() or isRequestActive(). Completion and progress callbacks are dispatched back to the logic thread.

Implementations§

Source§

impl HttpClient

Source

pub fn post_async( url: &str, json: &str, timeout: f32, callback: Box<dyn FnMut(Option<String>)>, ) -> i64

Sends a POST request with a JSON body.

§Arguments
  • url - The URL to send the request to.
  • json - The JSON data to send in the request body.
  • timeout - The timeout in seconds for the request.
  • callback - A callback function invoked when the request finishes. It receives the response body, or nil if the request fails or is cancelled.
§Returns
  • uint64_t - The request id. Returns 0 when the request cannot be scheduled.
Source

pub fn post_with_headers_async( url: &str, headers: &Vec<&str>, json: &str, timeout: f32, callback: Box<dyn FnMut(Option<String>)>, ) -> i64

Sends a POST request with custom headers and a JSON body.

§Arguments
  • url - The URL to send the request to.
  • headers - A vector of headers to include in the request. Each header should be in the format key: value.
  • json - The JSON data to send in the request body.
  • timeout - The timeout in seconds for the request.
  • callback - A callback function invoked when the request finishes. It receives the response body, or nil if the request fails or is cancelled.
§Returns
  • uint64_t - The request id. Returns 0 when the request cannot be scheduled.
Source

pub fn post_with_headers_part_async( url: &str, headers: &Vec<&str>, json: &str, timeout: f32, part_callback: Box<dyn FnMut(&str) -> bool>, callback: Box<dyn FnMut(Option<String>)>, ) -> i64

Sends a POST request with custom headers and a JSON body, while optionally consuming the response stream in chunks.

§Arguments
  • url - The URL to send the request to.
  • headers - A vector of headers to include in the request. Each header should be in the format key: value.
  • json - The JSON data to send in the request body.
  • timeout - The timeout in seconds for the request.
  • part_callback - A callback function that receives response chunks as they arrive. Return true to stop and cancel the request early.
  • callback - A callback function invoked when the request finishes. It receives the full response body, or nil if the request fails or is cancelled.
§Returns
  • uint64_t - The request id. Returns 0 when the request cannot be scheduled.
Source

pub fn get_async( url: &str, timeout: f32, callback: Box<dyn FnMut(Option<String>)>, ) -> i64

Sends a GET request.

§Arguments
  • url - The URL to send the request to.
  • timeout - The timeout in seconds for the request.
  • callback - A callback function invoked when the request finishes. It receives the response body, or nil if the request fails or is cancelled.
§Returns
  • uint64_t - The request id. Returns 0 when the request cannot be scheduled.
Source

pub fn download_async( url: &str, full_path: &str, timeout: f32, progress: Box<dyn FnMut(bool, i64, i64) -> bool>, ) -> i64

Downloads a file asynchronously and saves it to the specified local path.

§Arguments
  • url - The URL of the file to download.
  • full_path - The full path where the downloaded file should be saved.
  • timeout - The timeout in seconds for the request.
  • progress - A callback function that reports download progress. The function receives interrupted (whether the download failed or was cancelled), current (downloaded bytes), and total (total bytes when available). Return true to cancel the download. If the download fails or is cancelled, the partially written file is removed.
§Returns
  • uint64_t - The request id. Returns 0 when the request cannot be scheduled.
Source

pub fn cancel(request_id: i64) -> bool

Requests cancellation for an in-flight HTTP request.

§Arguments
  • request_id - The request id returned by an async HTTP method.
§Returns
  • bool - true if the request was found and cancellation was requested.
Source

pub fn is_request_active(request_id: i64) -> bool

Checks whether a request is still active.

§Arguments
  • request_id - The request id returned by an async HTTP method.
§Returns
  • bool - true if the request is still running.

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, 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, 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.