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<Self>
pub fn new(base_url: impl AsRef<str>) -> Result<Self>
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: ReqwestClient,
base_url: impl AsRef<str>,
) -> Result<Self>
pub fn with_http_client( reqwest_client: ReqwestClient, base_url: impl AsRef<str>, ) -> Result<Self>
Builds a client with a custom reqwest instance. ClientBuilder::base_url is required.
Sourcepub fn call<E: Endpoint>(
&self,
) -> EndpointRequestBuilder<'_, E, <E::Params as EndpointParamsInitial<E>>::State>where
E::Params: EndpointParamsInitial<E>,
pub fn call<E: Endpoint>(
&self,
) -> EndpointRequestBuilder<'_, E, <E::Params as EndpointParamsInitial<E>>::State>where
E::Params: EndpointParamsInitial<E>,
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 = ();
type Body = ();
type Headers = ();
}
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.
Use ClientConfig::effective_hooks for the hook chain used at runtime (client hooks
merged with plugin hooks at build time).
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.