pub struct ApiMethodRouter { /* private fields */ }openapi only.Expand description
A version of MethodRouter that
supports OpenAPI.
This is a core construct for building APIs that can be automatically described using OpenAPI.
This router allows you to register different handlers for different HTTP methods at the same path. When a request is received, the router will dispatch it to the handler registered for that HTTP method. Similarly, for OpenAPI specs, it will generate the appropriate operation for each method.
If no handler is registered for a particular method, the router will return
a 405 Method Not Allowed response. If no handler is registered for
HEAD requests, the router will return the response generated by the
handler for GET requests.
See crate::openapi module documentation for more details on how to
generate OpenAPI specs automatically.
§Examples
use cot::json::Json;
use cot::router::method::openapi::api_post;
use cot::router::{Route, Router};
use cot::test::TestRequestBuilder;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, schemars::JsonSchema)]
struct AddRequest {
a: i32,
b: i32,
}
#[derive(Serialize, schemars::JsonSchema)]
struct AddResponse {
result: i32,
}
async fn add(Json(add_request): Json<AddRequest>) -> Json<AddResponse> {
Json(AddResponse {
result: add_request.a + add_request.b,
})
}
let router = Router::with_urls([Route::with_api_handler("/", api_post(add))]);
let request = TestRequestBuilder::post("/")
.json(&AddRequest { a: 123, b: 123 })
.router(router.clone())
.build();
assert_eq!(
router
.handle(request)
.await?
.into_body()
.into_bytes()
.await?,
"{\"result\":246}"
);Implementations§
Source§impl ApiMethodRouter
impl ApiMethodRouter
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new ApiMethodRouter.
You might consider using api_get, api_post, or one of the other
functions defined in cot::router::method::openapi which serve as
convenient constructors for a ApiMethodRouter with a specific
handler.
§Examples
use cot::json::Json;
use cot::router::method::MethodRouter;
use cot::router::method::openapi::ApiMethodRouter;
use cot::router::{Route, Router};
use cot::test::TestRequestBuilder;
async fn test_handler() -> Json<()> {
Json(())
}
let method_router = ApiMethodRouter::new().get(test_handler);
let router = Router::with_urls([Route::with_api_handler("/", method_router)]);
let request = TestRequestBuilder::get("/").router(router.clone()).build();
assert_eq!(
router
.handle(request)
.await?
.into_body()
.into_bytes()
.await?,
"null"
);Sourcepub fn get<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
pub fn get<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
Sourcepub fn head<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
pub fn head<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
Sourcepub fn delete<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
pub fn delete<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
Sourcepub fn options<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
pub fn options<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
Sourcepub fn patch<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
pub fn patch<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
Sourcepub fn post<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
pub fn post<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
Sourcepub fn put<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
pub fn put<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
Sourcepub fn trace<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
pub fn trace<HandlerParams, ApiParams, H>(self, handler: H) -> Selfwhere
HandlerParams: 'static,
ApiParams: 'static,
H: RequestHandler<HandlerParams> + AsApiOperation<ApiParams> + Send + Sync + 'static,
Sourcepub fn connect<HandlerParams, H>(self, handler: H) -> Self
pub fn connect<HandlerParams, H>(self, handler: H) -> Self
Set a handler for the CONNECT HTTP method with OpenAPI
specification.
Note that CONNECT is not supported in OpenAPI and hence this method
does not require the handler to implement AsApiOperation.
§Examples
use cot::json::Json;
use cot::router::method::openapi::ApiMethodRouter;
async fn test_handler() -> Json<()> {
Json(())
}
let method_router = ApiMethodRouter::new().connect(test_handler);Sourcepub fn fallback<HandlerParams, H>(self, handler: H) -> Self
pub fn fallback<HandlerParams, H>(self, handler: H) -> Self
Set a fallback handler that gets called when no other handler matches.
§Examples
use cot::html::Html;
use cot::router::method::openapi::ApiMethodRouter;
use cot::router::{Route, Router};
use cot::test::TestRequestBuilder;
use cot::{Body, StatusCode};
async fn fallback_handler() -> Html {
Html::new("fallback")
}
let method_router = ApiMethodRouter::new().fallback(fallback_handler);
let router = Router::with_urls([Route::with_api_handler("/", method_router)]);
let request = TestRequestBuilder::get("/").router(router.clone()).build();
assert_eq!(
router
.handle(request)
.await?
.into_body()
.into_bytes()
.await?,
"fallback"
);Trait Implementations§
Source§impl AsApiRoute for ApiMethodRouter
impl AsApiRoute for ApiMethodRouter
Source§fn as_api_route(
&self,
route_context: &RouteContext<'_>,
schema_generator: &mut SchemaGenerator,
) -> PathItem
fn as_api_route( &self, route_context: &RouteContext<'_>, schema_generator: &mut SchemaGenerator, ) -> PathItem
Source§impl Debug for ApiMethodRouter
impl Debug for ApiMethodRouter
Source§impl Default for ApiMethodRouter
impl Default for ApiMethodRouter
Auto Trait Implementations§
impl !RefUnwindSafe for ApiMethodRouter
impl !UnwindSafe for ApiMethodRouter
impl Forget for ApiMethodRouter
impl Freeze for ApiMethodRouter
impl Send for ApiMethodRouter
impl Sync for ApiMethodRouter
impl Unpin for ApiMethodRouter
impl UnsafeUnpin for ApiMethodRouter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
db only.