Api

Struct 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§

Source§

impl Api

Source

pub fn new() -> Api

Instantiate a new API.

Source

pub fn new_with_base_path<S: Into<String>>(base_path: S) -> 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.

Source

pub fn add<P: Into<String>>(&mut self, path: P) -> RouteBuilder<'_>

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())
}
Source

pub async fn handle<Body: AsyncReadBody>( &self, req: Request<Body>, ) -> Result<Response<Vec<u8>>, RouteError<Body, ApiError>>

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.

Source

pub fn info(&self) -> Vec<RouteInfo>

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

Auto Trait Implementations§

§

impl Freeze for Api

§

impl !RefUnwindSafe for Api

§

impl Send for Api

§

impl Sync for Api

§

impl Unpin for Api

§

impl !UnwindSafe for Api

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