pub struct Client { /* private fields */ }
Expand description
An HTTP client, capable of sending Request
s
Users should only interact with this type from middlewares - normal crux code should
make use of the Http
capability type instead.
§Examples
use futures_util::future::BoxFuture;
use crux_http::middleware::{Next, Middleware};
use crux_http::{client::Client, Request, RequestBuilder, ResponseAsync, Result};
use std::time;
use std::sync::Arc;
// Fetches an authorization token prior to making a request
fn fetch_auth<'a>(mut req: Request, client: Client, next: Next<'a>) -> BoxFuture<'a, Result<ResponseAsync>> {
Box::pin(async move {
let auth_token = client.get("https://httpbin.org/get")
.await?
.body_string()
.await?;
req.append_header("Authorization", format!("Bearer {auth_token}"));
next.run(req, client).await
})
}
Implementations§
source§impl Client
impl Client
sourcepub async fn send(&self, req: impl Into<Request>) -> Result<ResponseAsync>
pub async fn send(&self, req: impl Into<Request>) -> Result<ResponseAsync>
Send a Request
using this client.
sourcepub async fn recv_bytes(&self, req: impl Into<Request>) -> Result<Vec<u8>>
pub async fn recv_bytes(&self, req: impl Into<Request>) -> Result<Vec<u8>>
Submit a Request
and get the response body as bytes.
sourcepub async fn recv_string(&self, req: impl Into<Request>) -> Result<String>
pub async fn recv_string(&self, req: impl Into<Request>) -> Result<String>
Submit a Request
and get the response body as a string.
sourcepub async fn recv_json<T: DeserializeOwned>(
&self,
req: impl Into<Request>,
) -> Result<T>
pub async fn recv_json<T: DeserializeOwned>( &self, req: impl Into<Request>, ) -> Result<T>
Submit a Request
and decode the response body from json into a struct.
sourcepub async fn recv_form<T: DeserializeOwned>(
&self,
req: impl Into<Request>,
) -> Result<T>
pub async fn recv_form<T: DeserializeOwned>( &self, req: impl Into<Request>, ) -> Result<T>
Submit a Request
and decode the response body from form encoding into a struct.
§Errors
Any I/O error encountered while reading the body is immediately returned
as an Err
.
If the body cannot be interpreted as valid json for the target type T
,
an Err
is returned.
Trait Implementations§
source§impl Clone for Client
impl Clone for Client
source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the Client.
This copies the middleware stack from the original, but shares
the HttpClient
and http client config of the original.
Note that individual middleware in the middleware stack are
still shared by reference.
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin 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,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)