Skip to main content

EndpointRequestBuilder

Struct EndpointRequestBuilder 

Source
pub struct EndpointRequestBuilder<'a, E: Endpoint, S> { /* 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: Endpoint> EndpointRequestBuilder<'a, E, NeedsParams>

Source

pub fn params( self, params: E::Params, ) -> EndpointRequestBuilder<'a, E, ParamsBuilderStateAfter<E>>
where E::Body: EndpointBody,

Applies typed path parameters and transitions to the next builder state.

Source§

impl<'a, E: Endpoint> EndpointRequestBuilder<'a, E, NeedsBody>

Source

pub fn new_needs_body(inner: RequestBuilder<'a>) -> Self

Source

pub fn json<T: Serialize>( self, body: &T, ) -> Result<EndpointRequestBuilder<'a, E, Ready>>

Available on crate feature json only.

JSON request body (transitions to Ready).

Source

pub fn json_validated<T>( self, body: &T, ) -> Result<EndpointRequestBuilder<'a, E, Ready>>

Available on crate feature validate only.

Validated JSON request body (feature validate).

Source

pub fn with_body( self, body: E::Body, ) -> Result<EndpointRequestBuilder<'a, E, Ready>>

Applies typed request body for E::Body (transitions to Ready).

Source

pub fn body( self, body: impl Into<Bytes>, ) -> EndpointRequestBuilder<'a, E, Ready>

Raw request body (transitions to Ready).

Source§

impl<'a, E: Endpoint> EndpointRequestBuilder<'a, E, Ready>

Source

pub fn query(self, query: E::Query) -> Result<Self>

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?;
Source

pub fn query_validated(self, query: E::Query) -> Result<Self>

Available on crate features json and validate only.

Like Self::query but runs garde::Validate on the query value first (feature validate).

Intended for serde query structs (including those using EndpointQueryDerive).

Source

pub fn header( self, key: impl AsRef<str>, value: impl AsRef<str>, ) -> Result<Self>

Adds a request header.

Source

pub fn bearer_token(self, token: impl Into<String>) -> Self

Sets bearer authentication.

Source

pub fn cancellation_token(self, token: CancellationToken) -> Self

Attaches a cancellation token.

Source

pub fn throw_on_error(self, throw: bool) -> Self

When true, send returns Err on non-2xx.

Source

pub fn base_url(self, base_url: impl AsRef<str>) -> Result<Self>

Overrides the client base URL for this request (RequestBuilder::base_url).

Source

pub fn retry(self, policy: RetryPolicy) -> Self

Overrides retry policy (RequestBuilder::retry).

Source

pub fn timeout(self, timeout: Duration) -> Self

Overrides timeout (RequestBuilder::timeout).

Source

pub async fn send_stream(self) -> Result<StreamingResponse>

Streaming execution (RequestBuilder::send_stream).

Source

pub fn max_response_bytes(self, limit: u64) -> Self

Caps response body size (RequestBuilder::max_response_bytes).

Source

pub fn disable_validation(self, disable: bool) -> Self

Available on crate feature schema-validate only.

Skips registry JSON Schema validation for this request (RequestBuilder::disable_validation).

Source

pub fn json<T: Serialize>(self, body: &T) -> Result<Self>

Available on crate feature json only.

JSON request body (RequestBuilder::json).

Source

pub fn json_validated<T>(self, body: &T) -> Result<Self>

Available on crate feature validate only.

Validated JSON request body (feature validate).

Source

pub fn body(self, body: impl Into<Bytes>) -> Self

Raw request body (RequestBuilder::body).

Source

pub async fn send(self) -> Result<Response>

Executes the request and returns Response.

Source

pub fn with_body(self, body: E::Body) -> Result<Self>

Applies typed request body for E::Body.

Source

pub fn with_headers(self, headers: E::Headers) -> Result<Self>

Applies typed headers for E::Headers.

Source

pub fn with_headers_validated(self, headers: E::Headers) -> Result<Self>

Available on crate feature validate only.

Like Self::with_headers but runs garde::Validate on the headers value first (feature validate).

Source

pub async fn send_json(self) -> Result<E::Response>

Available on crate feature json only.

Executes and deserializes E::Response (feature json).

Source

pub async fn send_api<T, ErrBody>(self) -> Result<Result<T, ErrBody>>

Available on crate feature json only.

Success/error deserialization by status (feature json).

Trait Implementations§

Source§

impl<'a, E: Endpoint> Deref for EndpointRequestBuilder<'a, E, Ready>

Source§

type Target = RequestBuilder<'a>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<'a, E, S> !Freeze for EndpointRequestBuilder<'a, E, S>

§

impl<'a, E, S> !RefUnwindSafe for EndpointRequestBuilder<'a, E, S>

§

impl<'a, E, S> Send for EndpointRequestBuilder<'a, E, S>
where E: Send, S: Send,

§

impl<'a, E, S> Sync for EndpointRequestBuilder<'a, E, S>
where E: Sync, S: Sync,

§

impl<'a, E, S> Unpin for EndpointRequestBuilder<'a, E, S>
where E: Unpin, S: Unpin,

§

impl<'a, E, S> UnsafeUnpin for EndpointRequestBuilder<'a, E, S>

§

impl<'a, E, S> !UnwindSafe for EndpointRequestBuilder<'a, E, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,