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

source

pub fn new() -> Self

Create a RouteMethod object.

source

pub fn method<E>(self, method: Method, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for the specified method.

source

pub fn get<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for GET.

source

pub fn post<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for POST.

source

pub fn put<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for PUT.

source

pub fn delete<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for DELETE.

source

pub fn head<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for HEAD.

source

pub fn options<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for OPTIONS.

source

pub fn connect<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for CONNECT.

source

pub fn patch<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for PATCH.

source

pub fn trace<E>(self, ep: E) -> Self
where E: IntoEndpoint, E::Endpoint: 'static,

Sets the endpoint for TRACE.

Trait Implementations§

source§

impl Default for RouteMethod

source§

fn default() -> RouteMethod

Returns the “default value” for a type. Read more
source§

impl Endpoint for RouteMethod

§

type Output = Response

Represents the response of the endpoint.
source§

fn call( &self, req: Request ) -> impl Future<Output = Result<Self::Output>> + Send

Get the response to the request.
source§

fn get_response(&self, req: Request) -> impl Future<Output = Response> + Send

Get the response to the request and return a Response. Read more

Auto Trait Implementations§

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

source§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

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> EndpointExt for T
where T: IntoEndpoint,

source§

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
where T: Middleware<Self::Endpoint>, Self: Sized,

Use middleware to transform this endpoint. Read more
source§

fn with_if<T>( self, enable: bool, middleware: T ) -> EitherEndpoint<Self, T::Output>
where T: Middleware<Self::Endpoint>, Self: Sized,

if enable is true then use middleware to transform this endpoint. Read more
source§

fn data<T>(self, data: T) -> AddDataEndpoint<Self::Endpoint, T>
where T: Clone + Send + Sync + 'static, Self: Sized,

Attach a state data to the endpoint, similar to with(AddData(T)). Read more
source§

fn data_opt<T>( self, data: Option<T> ) -> EitherEndpoint<AddDataEndpoint<Self::Endpoint, T>, Self>
where T: Clone + Send + Sync + 'static, Self: Sized,

if data is Some(T) then attach the value to the endpoint.
source§

fn before<F, Fut>(self, f: F) -> Before<Self, F>
where F: Fn(Request) -> Fut + Send + Sync, Fut: Future<Output = Result<Request>> + Send, Self: Sized,

Maps the request of this endpoint. Read more
source§

fn after<F, Fut, T>(self, f: F) -> After<Self::Endpoint, F>
where F: Fn(Result<<Self::Endpoint as Endpoint>::Output>) -> Fut + Send + Sync, Fut: Future<Output = Result<T>> + Send, T: IntoResponse, Self: Sized,

Maps the output of this endpoint. Read more
source§

fn around<F, Fut, R>(self, f: F) -> Around<Self::Endpoint, F>
where F: Fn(Arc<Self::Endpoint>, Request) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R>> + Send + 'static, R: IntoResponse, Self: Sized,

Maps the request and response of this endpoint. Read more
source§

fn map_to_response(self) -> MapToResponse<Self::Endpoint>
where Self: Sized,

Convert the output of this endpoint into a response. Response. Read more
source§

fn to_response(self) -> ToResponse<Self::Endpoint>
where Self: Sized,

Convert the output of this endpoint into a response. Response. Read more
source§

fn map<F, Fut, R, R2>(self, f: F) -> Map<Self::Endpoint, F>
where F: Fn(R) -> Fut + Send + Sync, Fut: Future<Output = R2> + Send, R: IntoResponse, R2: IntoResponse, Self: Sized, Self::Endpoint: Endpoint<Output = R> + Sized,

Maps the response of this endpoint. Read more
source§

fn and_then<F, Fut, R, R2>(self, f: F) -> AndThen<Self::Endpoint, F>
where F: Fn(R) -> Fut + Send + Sync, Fut: Future<Output = Result<R2>> + Send, R: IntoResponse, R2: IntoResponse, Self: Sized, Self::Endpoint: Endpoint<Output = R> + Sized,

Calls f if the result is Ok, otherwise returns the Err value of self. Read more
source§

fn catch_all_error<F, Fut, R>(self, f: F) -> CatchAllError<Self, F, R>
where F: Fn(Error) -> Fut + Send + Sync, Fut: Future<Output = R> + Send, R: IntoResponse + Send, Self: Sized + Sync,

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>
where F: Fn(ErrType) -> Fut + Send + Sync, Fut: Future<Output = R> + Send, R: IntoResponse + Send + Sync, ErrType: Error + Send + Sync + 'static, Self: Sized,

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>
where F: Fn(&Error) + Send + Sync, Self: Sized,

Does something with each error. Read more
source§

fn inspect_err<F, ErrType>(self, f: F) -> InspectError<Self, F, ErrType>
where F: Fn(&ErrType) + Send + Sync, ErrType: Error + Send + Sync + 'static, Self: Sized,

Does something with each specified error type. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEndpoint for T
where T: Endpoint,

§

type Endpoint = T

Represents the endpoint type.
source§

fn into_endpoint(self) -> <T as IntoEndpoint>::Endpoint

Converts this object into endpoint.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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,

Available on crate feature tower-compat only.
Converts a tower service to a poem endpoint.
source§

impl<L> TowerLayerCompatExt for L

source§

fn compat(self) -> TowerCompatMiddleware<Self>
where Self: Sized,

Available on crate feature tower-compat only.
Converts a tower layer to a poem middleware.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more