Skip to main content

EndpointRequestBuilder

Struct EndpointRequestBuilder 

Source
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,

Source

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

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

Source§

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

Source

pub fn new_needs_body( inner: RequestBuilder<'a>, ) -> EndpointRequestBuilder<'a, E, NeedsBody>

Source

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

JSON request body (transitions to Ready).

Source

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

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> EndpointRequestBuilder<'a, E, Ready>
where E: Endpoint,

Source

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

pub fn header( self, key: impl AsRef<str>, value: impl AsRef<str>, ) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>

Adds a request header.

Source

pub fn bearer_token( self, token: impl Into<String>, ) -> EndpointRequestBuilder<'a, E, Ready>

Sets bearer authentication.

Source

pub fn cancellation_token( self, token: CancellationToken, ) -> EndpointRequestBuilder<'a, E, Ready>

Attaches a cancellation token.

Source

pub fn throw_on_error(self, throw: bool) -> EndpointRequestBuilder<'a, E, Ready>

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

Source

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).

Source

pub fn retry(self, policy: RetryPolicy) -> EndpointRequestBuilder<'a, E, Ready>

Overrides retry policy (RequestBuilder::retry).

Source

pub fn timeout(self, timeout: Duration) -> EndpointRequestBuilder<'a, E, Ready>

Overrides timeout (RequestBuilder::timeout).

Source

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

Streaming execution (RequestBuilder::send_stream).

Source

pub fn max_response_bytes( self, limit: u64, ) -> EndpointRequestBuilder<'a, E, Ready>

Caps response body size (RequestBuilder::max_response_bytes).

Source

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

JSON request body (RequestBuilder::json).

Source

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

Raw request body (RequestBuilder::body).

Source

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

Executes the request and returns Response.

Source

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

Applies typed request body for E::Body.

Source

pub fn with_headers( self, headers: <E as Endpoint>::Headers, ) -> Result<EndpointRequestBuilder<'a, E, Ready>, Error>

Applies typed headers for E::Headers.

Source

pub async fn send_json(self) -> Result<<E as Endpoint>::Response, Error>

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

Source

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

Success/error deserialization by status (feature json).

Trait Implementations§

Source§

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

Source§

type Target = RequestBuilder<'a>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<EndpointRequestBuilder<'a, E, Ready> as Deref>::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> 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<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