Skip to main content

Endpoint

Trait Endpoint 

Source
pub trait Endpoint {
    type Response: DeserializeOwned;
    type Params: EndpointParams + Default;
    type Query: EndpointQuery + 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. Path and query parameters are typed via EndpointParams and EndpointQuery structs; use .params() and .query().

§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 = ();
}

#[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§

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§