Struct seamless::api::Api

source ·
pub struct Api { /* private fields */ }
Expand description

The entry point; you can create an instance of this and then add API routes to it using Self::add(). You can then get information about the routes that have been added using Self::info(), or handle an http::Request using Self::handle().

Implementations§

Instantiate a new API.

Instantiate a new API that will handle requests that begin with the provided base path.

For example, if the provided base_path is “/foo/bar”, and a route with the path “hi” is added, then an incoming http::Request with the path "/foo/bar/hi" will match it.

Add a new route to the API. You must provide a path to make this route available at, and are given back a RouteBuilder which can be used to give the route a handler and a description.

Examples
// This route expects a JSON formatted string to be provided, and echoes it straight back.
api.add("some/route/name")
   .description("This route takes some Foo's in and returns some Bar's")
   .handler(|body: FromJson<String>| ToJson(body.0));

// This route delegates to an async fn to sum some values, so we can infer more types in the
// handler.
api.add("another.route")
   .description("This route takes an array of values and sums them")
   .handler(|body: FromJson<_>| sum(body.0));

async fn sum(ns: Vec<u64>) -> ToJson<u64> {
    ToJson(ns.into_iter().sum())
}

Match an incoming http::Request against our API routes and run the relevant handler if a matching one is found. We’ll get back bytes representing a JSON response back if all goes ok, else we’ll get back a RouteError, which will either be RouteError::NotFound if no matching route was found, or a RouteError::Err if a matching route was found, but that handler emitted an error.

Return information about the API routes that have been defined so far.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.