Skip to main content

Endpoint

Trait Endpoint 

Source
pub trait Endpoint {
    type Response: DeserializeOwned;
    type Params: EndpointParams + Default;
    type Query: EndpointQuery + Default;
    type Body: EndpointBody + Default;
    type Headers: EndpointHeaders + Default;

    const METHOD: Method;
    const PATH: &'static str;
}
Expand description

Describes a typed API route.

Implement this trait (or use [endpoint!]) and call Client::call.

Compile-time enforcement: EndpointParams can require .params() via NeedsParams. EndpointBody can require a body via NeedsBody on POST routes. EndpointQuery only types query values — .query() is optional even when Query is not [()]; the Default bound is for struct construction, not auto-apply on send.

§Examples

define_params!(GetTodoParams for "/todos/:id" { id: u64 });

struct GetTodo;

impl Endpoint for GetTodo {
    const METHOD: Method = Method::GET;
    const PATH: &'static str = "/todos/:id";
    type Response = Todo;
    type Params = GetTodoParams;
    type Query = ();
    type Body = ();
    type Headers = ();
}

#[derive(Deserialize)]
struct Todo { id: u64, title: String }

let client = Client::new("https://jsonplaceholder.typicode.com")?;
let todo: Todo = client
    .call::<GetTodo>()
    .params(GetTodoParams { id: 1 })
    .send_json()
    .await?;

Required Associated Constants§

Source

const METHOD: Method

HTTP method for this route.

Source

const PATH: &'static str

Path template (may include :param segments).

Required Associated Types§

Source

type Response: DeserializeOwned

JSON response type for EndpointRequestBuilder::send_json.

Source

type Params: EndpointParams + Default

Path parameters applied via EndpointRequestBuilder::params.

Source

type Query: EndpointQuery + Default

Query parameters serialized by EndpointRequestBuilder::query when you call it.

Not required at compile time: omitting .query() sends no typed query string. Use [()] when the route has no query struct.

Source

type Body: EndpointBody + Default

Optional typed request body ([()] = none).

Source

type Headers: EndpointHeaders + Default

Optional typed request headers ([()] = none).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§