pub struct EndpointRequestBuilder<'a, E, S>where
E: Endpoint,{ /* private fields */ }Expand description
Fluent builder for a typed Endpoint.
When E::Params is not [()], the builder starts in NeedsParams and requires
.params() before .send_json().
When E::Query is not [()], .query() is still optional — it only runs when called.
In Ready state, use .query(E::Query) for typed query structs, or the forwarded
methods on this type (.header, .json, etc.). Prefer typed .query() over string keys on Deref.
Implementations§
Source§impl<'a, E> EndpointRequestBuilder<'a, E, NeedsParams>where
E: Endpoint,
impl<'a, E> EndpointRequestBuilder<'a, E, NeedsParams>where
E: Endpoint,
Sourcepub fn params(
self,
params: <E as Endpoint>::Params,
) -> EndpointRequestBuilder<'a, E, <<E as Endpoint>::Body as EndpointBody>::ParamsNext>
pub fn params( self, params: <E as Endpoint>::Params, ) -> EndpointRequestBuilder<'a, E, <<E as Endpoint>::Body as EndpointBody>::ParamsNext>
Applies typed path parameters and transitions to the next builder state.
Source§impl<'a, E> EndpointRequestBuilder<'a, E, NeedsBody>where
E: Endpoint,
impl<'a, E> EndpointRequestBuilder<'a, E, NeedsBody>where
E: Endpoint,
pub fn new_needs_body( inner: RequestBuilder<'a>, ) -> EndpointRequestBuilder<'a, E, NeedsBody>
Sourcepub fn json<T>(
self,
body: &T,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>where
T: Serialize,
pub fn json<T>(
self,
body: &T,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>where
T: Serialize,
JSON request body (transitions to Ready).
Source§impl<'a, E> EndpointRequestBuilder<'a, E, Ready>where
E: Endpoint,
impl<'a, E> EndpointRequestBuilder<'a, E, Ready>where
E: Endpoint,
Sourcepub fn query(
self,
query: <E as Endpoint>::Query,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
pub fn query( self, query: <E as Endpoint>::Query, ) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
Applies typed query parameters for E::Query.
Optional at compile time: you can call .send_json() without this method;
no query string from E::Query is sent unless you call .query(...).
Returns Error::QuerySerialize when serde serialization fails
(since 0.4.0 — failures are no longer ignored).
§Examples
define_params!(ItemParams for "/items/:id" { id: u64 });
#[derive(Default, Serialize)]
struct ItemQuery { tag: Option<String> }
better_fetch::impl_serde_endpoint_query!(ItemQuery);
struct GetItem;
impl Endpoint for GetItem {
const METHOD: Method = Method::GET;
const PATH: &'static str = "/items/:id";
type Response = serde_json::Value;
type Params = ItemParams;
type Query = ItemQuery;
type Body = ();
type Headers = ();
}
let client = Client::new("https://api.example.com")?;
let _ = client
.call::<GetItem>()
.params(ItemParams { id: 1 })
.query(ItemQuery { tag: Some("news".into()) })?
.send_json()
.await?;Sourcepub fn header(
self,
key: impl AsRef<str>,
value: impl AsRef<str>,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
pub fn header( self, key: impl AsRef<str>, value: impl AsRef<str>, ) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
Adds a request header.
Sourcepub fn bearer_token(
self,
token: impl Into<String>,
) -> EndpointRequestBuilder<'a, E, Ready>
pub fn bearer_token( self, token: impl Into<String>, ) -> EndpointRequestBuilder<'a, E, Ready>
Sets bearer authentication.
Sourcepub fn cancellation_token(
self,
token: CancellationToken,
) -> EndpointRequestBuilder<'a, E, Ready>
pub fn cancellation_token( self, token: CancellationToken, ) -> EndpointRequestBuilder<'a, E, Ready>
Attaches a cancellation token.
Sourcepub fn throw_on_error(self, throw: bool) -> EndpointRequestBuilder<'a, E, Ready>
pub fn throw_on_error(self, throw: bool) -> EndpointRequestBuilder<'a, E, Ready>
When true, send returns Err on non-2xx.
Sourcepub fn base_url(
self,
base_url: impl AsRef<str>,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
pub fn base_url( self, base_url: impl AsRef<str>, ) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
Overrides the client base URL for this request (RequestBuilder::base_url).
Sourcepub fn retry(self, policy: RetryPolicy) -> EndpointRequestBuilder<'a, E, Ready>
pub fn retry(self, policy: RetryPolicy) -> EndpointRequestBuilder<'a, E, Ready>
Overrides retry policy (RequestBuilder::retry).
Sourcepub fn timeout(self, timeout: Duration) -> EndpointRequestBuilder<'a, E, Ready>
pub fn timeout(self, timeout: Duration) -> EndpointRequestBuilder<'a, E, Ready>
Overrides timeout (RequestBuilder::timeout).
Sourcepub async fn send_stream(self) -> Result<StreamingResponse, Error>
pub async fn send_stream(self) -> Result<StreamingResponse, Error>
Streaming execution (RequestBuilder::send_stream).
Sourcepub fn max_response_bytes(
self,
limit: u64,
) -> EndpointRequestBuilder<'a, E, Ready>
pub fn max_response_bytes( self, limit: u64, ) -> EndpointRequestBuilder<'a, E, Ready>
Caps response body size (RequestBuilder::max_response_bytes).
Sourcepub fn json<T>(
self,
body: &T,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>where
T: Serialize,
pub fn json<T>(
self,
body: &T,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>where
T: Serialize,
JSON request body (RequestBuilder::json).
Sourcepub fn body(
self,
body: impl Into<Bytes>,
) -> EndpointRequestBuilder<'a, E, Ready>
pub fn body( self, body: impl Into<Bytes>, ) -> EndpointRequestBuilder<'a, E, Ready>
Raw request body (RequestBuilder::body).
Sourcepub fn with_body(
self,
body: <E as Endpoint>::Body,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
pub fn with_body( self, body: <E as Endpoint>::Body, ) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
Applies typed request body for E::Body.
Sourcepub fn with_headers(
self,
headers: <E as Endpoint>::Headers,
) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
pub fn with_headers( self, headers: <E as Endpoint>::Headers, ) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>
Applies typed headers for E::Headers.
Sourcepub async fn send_json(self) -> Result<<E as Endpoint>::Response, Error>
pub async fn send_json(self) -> Result<<E as Endpoint>::Response, Error>
Executes and deserializes E::Response (feature json).
Sourcepub async fn send_api<T, ErrBody>(self) -> Result<Result<T, ErrBody>, Error>where
T: DeserializeOwned,
ErrBody: DeserializeOwned,
pub async fn send_api<T, ErrBody>(self) -> Result<Result<T, ErrBody>, Error>where
T: DeserializeOwned,
ErrBody: DeserializeOwned,
Success/error deserialization by status (feature json).