Struct seamless::api::Api [−][src]
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
impl Api
[src]
pub fn new() -> Api
[src]
Instantiate a new API.
pub fn new_with_base_path<S: Into<String>>(base_path: S) -> Api
[src]
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.
pub fn add<P: Into<String>>(&mut self, path: P) -> RouteBuilder<'_>
[src]
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: Json<String>| async move { Ok::<_,std::convert::Infallible>(body.json) }); // 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: Json<_>| sum(body.json)); async fn sum(ns: Vec<u64>) -> Result<u64, Infallible> { Ok(ns.into_iter().sum()) }
pub async fn handle(
&self,
req: Request<Vec<u8>>
) -> Result<Response<Vec<u8>>, RouteError<ApiError>>
[src]
&self,
req: Request<Vec<u8>>
) -> Result<Response<Vec<u8>>, RouteError<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.
pub fn info(&self) -> Vec<RouteInfo>
[src]
Return information about the API routes that have been defined so far.
Auto Trait Implementations
impl !RefUnwindSafe for Api
[src]
impl Send for Api
[src]
impl Sync for Api
[src]
impl Unpin for Api
[src]
impl !UnwindSafe for Api
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,