pub struct Client { /* private fields */ }Expand description
Typed HTTP client built on reqwest.
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(base_url: impl AsRef<str>) -> Result<Client, Error>
pub fn new(base_url: impl AsRef<str>) -> Result<Client, Error>
Creates a client with default reqwest settings and the given base URL.
§Examples
let client = Client::new("https://api.example.com")?;
let _ = client.get("/health").send().await?;Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Returns a ClientBuilder for advanced configuration.
Sourcepub fn with_http_client(
reqwest_client: Client,
base_url: impl AsRef<str>,
) -> Result<Client, Error>
pub fn with_http_client( reqwest_client: Client, base_url: impl AsRef<str>, ) -> Result<Client, Error>
Builds a client with a custom reqwest instance. ClientBuilder::base_url is required.
Sourcepub fn call<E>(
&self,
) -> EndpointRequestBuilder<'_, E, <<E as Endpoint>::Params as EndpointParams>::BuilderState>
pub fn call<E>( &self, ) -> EndpointRequestBuilder<'_, E, <<E as Endpoint>::Params as EndpointParams>::BuilderState>
Starts a typed request for Endpoint E.
When E::Params is not unit, returns a builder in NeedsParams state
that requires .params() before
.send_json().
For ad-hoc requests with string paths, use Self::get / Self::post instead.
§Examples
define_params!(GetTodoParams for "/todos/:id" { id: u64 });
struct GetTodo;
impl Endpoint for GetTodo {
const METHOD: http::Method = http::Method::GET;
const PATH: &'static str = "/todos/:id";
type Response = Todo;
type Params = GetTodoParams;
type Query = ();
}
let client = Client::new("https://api.example.com")?;
let todo = client
.call::<GetTodo>()
.params(GetTodoParams { id: 1 })
.send_json()
.await?;Sourcepub fn config(&self) -> &ClientConfig
pub fn config(&self) -> &ClientConfig
Returns a snapshot of this client’s configuration.
Sourcepub fn get(&self, path: impl Into<String>) -> RequestBuilder<'_>
pub fn get(&self, path: impl Into<String>) -> RequestBuilder<'_>
Starts a GET request for path (supports :param templates).
Sourcepub fn post(&self, path: impl Into<String>) -> RequestBuilder<'_>
pub fn post(&self, path: impl Into<String>) -> RequestBuilder<'_>
Starts a POST request for path.
Sourcepub fn put(&self, path: impl Into<String>) -> RequestBuilder<'_>
pub fn put(&self, path: impl Into<String>) -> RequestBuilder<'_>
Starts a PUT request for path.
Sourcepub fn patch(&self, path: impl Into<String>) -> RequestBuilder<'_>
pub fn patch(&self, path: impl Into<String>) -> RequestBuilder<'_>
Starts a PATCH request for path.
Sourcepub fn delete(&self, path: impl Into<String>) -> RequestBuilder<'_>
pub fn delete(&self, path: impl Into<String>) -> RequestBuilder<'_>
Starts a DELETE request for path.
Sourcepub fn head(&self, path: impl Into<String>) -> RequestBuilder<'_>
pub fn head(&self, path: impl Into<String>) -> RequestBuilder<'_>
Starts a HEAD request for path.
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
Mutably borrows from an owned value. Read more