pub struct Client {
client: Client,
}Expand description
Pre-configured HTTP client for making network requests.
This client should be used as the base for all HTTP requests rather than
constructing raw reqwest clients. It comes with:
- TLS using
webpki-rootscertificates (works on Kobo devices without system cert store) - 30 second request timeout
- User agent header set
§Example
use cadmus_core::http::Client;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new()?;
client.get("https://api.github.com").send()?;
Ok(())
}Fields§
§client: ClientImplementations§
Source§impl Client
impl Client
pub fn new() -> Result<Self, HttpError>
pub fn head(&self, url: &str) -> RequestBuilder
pub fn get(&self, url: &str) -> RequestBuilder
pub fn post(&self, url: &str) -> RequestBuilder
Sourcepub fn into_reqwest(self) -> ReqwestClient
pub fn into_reqwest(self) -> ReqwestClient
Returns the inner reqwest::blocking::Client for use with third-party
libraries that require a raw client (e.g. pyroscope-rs).
Sourcepub fn download<B, F>(
&self,
url: &str,
total_size: u64,
dest: &PathBuf,
request_builder: B,
progress_callback: &mut F,
) -> Result<(), ChunkedDownloadError>
pub fn download<B, F>( &self, url: &str, total_size: u64, dest: &PathBuf, request_builder: B, progress_callback: &mut F, ) -> Result<(), ChunkedDownloadError>
Downloads a file to dest using HTTP Range requests.
request_builder is called once per chunk (and per retry) to produce a
RequestBuilder for the given URL. The caller is responsible for adding
any required headers (e.g. Authorization).
progress_callback is called after each successful chunk with
(bytes_downloaded_so_far, total_bytes).
§Errors
Returns ChunkedDownloadError::Io if the destination file cannot be created
or written. Returns ChunkedDownloadError::Request if all retry attempts for
any chunk fail.
§Example
use cadmus_core::http::Client;
use std::path::PathBuf;
let client = Client::new()?;
let dest = PathBuf::from("/tmp/downloaded_file");
client.download(
"https://example.com/large-file.bin",
1024 * 1024,
&dest,
|url| client.get(url),
&mut |downloaded, total| println!("{}/{}", downloaded, total),
)?;Sourcefn download_chunk_with_retries<B>(
url: &str,
start: u64,
end: u64,
request_builder: &B,
) -> Result<Vec<u8>, ChunkedDownloadError>
fn download_chunk_with_retries<B>( url: &str, start: u64, end: u64, request_builder: &B, ) -> Result<Vec<u8>, ChunkedDownloadError>
Downloads a specific byte range with automatic exponential-backoff retry.
§Errors
Returns an error if all MAX_RETRIES attempts fail.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more