Struct poem::RouteMethod
source · pub struct RouteMethod { /* private fields */ }
Expand description
Routing object for HTTP methods
§Errors
§Example
use poem::{
handler,
http::{Method, StatusCode},
Endpoint, Request, RouteMethod,
};
#[handler]
fn handle_get() -> &'static str {
"get"
}
#[handler]
fn handle_post() -> &'static str {
"post"
}
let route_method = RouteMethod::new().get(handle_get).post(handle_post);
let resp = route_method
.get_response(Request::builder().method(Method::GET).finish())
.await;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "get");
let resp = route_method
.get_response(Request::builder().method(Method::POST).finish())
.await;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "post");
let resp = route_method
.get_response(Request::builder().method(Method::PUT).finish())
.await;
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
Implementations§
source§impl RouteMethod
impl RouteMethod
sourcepub fn method<E>(self, method: Method, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn method<E>(self, method: Method, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for the specified method
.
sourcepub fn get<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn get<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for GET
.
sourcepub fn post<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn post<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for POST
.
sourcepub fn put<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn put<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for PUT
.
sourcepub fn delete<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn delete<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for DELETE
.
sourcepub fn head<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn head<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for HEAD
.
sourcepub fn options<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn options<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for OPTIONS
.
sourcepub fn connect<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn connect<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for CONNECT
.
sourcepub fn patch<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn patch<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for PATCH
.
sourcepub fn trace<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
pub fn trace<E>(self, ep: E) -> Selfwhere
E: IntoEndpoint,
E::Endpoint: 'static,
Sets the endpoint for TRACE
.
Trait Implementations§
source§impl Default for RouteMethod
impl Default for RouteMethod
source§fn default() -> RouteMethod
fn default() -> RouteMethod
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for RouteMethod
impl !RefUnwindSafe for RouteMethod
impl Send for RouteMethod
impl Sync for RouteMethod
impl Unpin for RouteMethod
impl !UnwindSafe for RouteMethod
Blanket Implementations§
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Mutably borrows from an owned value. Read more
source§impl<T> EndpointExt for Twhere
T: IntoEndpoint,
impl<T> EndpointExt for Twhere
T: IntoEndpoint,
source§fn boxed<'a>(self) -> BoxEndpoint<'a, <Self::Endpoint as Endpoint>::Output>where
Self: Sized + 'a,
fn boxed<'a>(self) -> BoxEndpoint<'a, <Self::Endpoint as Endpoint>::Output>where
Self: Sized + 'a,
Wrap the endpoint in a Box.
source§fn with<T>(self, middleware: T) -> T::Output
fn with<T>(self, middleware: T) -> T::Output
Use middleware to transform this endpoint. Read more
source§fn data<T>(self, data: T) -> AddDataEndpoint<Self::Endpoint, T>
fn data<T>(self, data: T) -> AddDataEndpoint<Self::Endpoint, T>
Attach a state data to the endpoint, similar to
with(AddData(T))
. Read moresource§fn data_opt<T>(
self,
data: Option<T>
) -> EitherEndpoint<AddDataEndpoint<Self::Endpoint, T>, Self>
fn data_opt<T>( self, data: Option<T> ) -> EitherEndpoint<AddDataEndpoint<Self::Endpoint, T>, Self>
if
data
is Some(T)
then attach the value to the endpoint.source§fn after<F, Fut, T>(self, f: F) -> After<Self::Endpoint, F>
fn after<F, Fut, T>(self, f: F) -> After<Self::Endpoint, F>
Maps the output of this endpoint. Read more
source§fn around<F, Fut, R>(self, f: F) -> Around<Self::Endpoint, F>
fn around<F, Fut, R>(self, f: F) -> Around<Self::Endpoint, F>
Maps the request and response of this endpoint. Read more
source§fn map_to_response(self) -> MapToResponse<Self::Endpoint>where
Self: Sized,
fn map_to_response(self) -> MapToResponse<Self::Endpoint>where
Self: Sized,
source§fn to_response(self) -> ToResponse<Self::Endpoint>where
Self: Sized,
fn to_response(self) -> ToResponse<Self::Endpoint>where
Self: Sized,
source§fn map<F, Fut, R, R2>(self, f: F) -> Map<Self::Endpoint, F>
fn map<F, Fut, R, R2>(self, f: F) -> Map<Self::Endpoint, F>
Maps the response of this endpoint. Read more
source§fn catch_all_error<F, Fut, R>(self, f: F) -> CatchAllError<Self, F, R>
fn catch_all_error<F, Fut, R>(self, f: F) -> CatchAllError<Self, F, R>
Catch all errors and convert it into a response. Read more
source§fn catch_error<F, Fut, R, ErrType>(
self,
f: F
) -> CatchError<Self, F, R, ErrType>
fn catch_error<F, Fut, R, ErrType>( self, f: F ) -> CatchError<Self, F, R, ErrType>
Catch the specified type of error and convert it into a response. Read more
source§fn inspect_all_err<F>(self, f: F) -> InspectAllError<Self, F>
fn inspect_all_err<F>(self, f: F) -> InspectAllError<Self, F>
Does something with each error. Read more
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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> IntoEndpoint for Twhere
T: Endpoint,
impl<T> IntoEndpoint for Twhere
T: Endpoint,
source§fn into_endpoint(self) -> <T as IntoEndpoint>::Endpoint
fn into_endpoint(self) -> <T as IntoEndpoint>::Endpoint
Converts this object into endpoint.
source§impl<T> TowerCompatExt for T
impl<T> TowerCompatExt for T
source§fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
Available on crate feature
tower-compat
only.Converts a tower service to a poem endpoint.