Trait poem::EndpointExt [−][src]
pub trait EndpointExt: IntoEndpoint {
fn boxed(self) -> BoxEndpoint<<Self::Endpoint as Endpoint>::Output>
where
Self: Sized + 'static,
{ ... }
fn with<T>(self, middleware: T) -> T::Output
where
T: Middleware<Self::Endpoint>,
Self: Sized,
{ ... }
fn data<T>(self, data: T) -> AddDataEndpoint<Self::Endpoint, T>
where
T: Clone + Send + Sync + 'static,
Self: Sized,
{ ... }
fn before<F, Fut>(self, f: F) -> Before<Self, F>
where
F: Fn(Request) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Request> + Send + 'static,
Self: Sized,
{ ... }
fn after<F, Fut, R>(self, f: F) -> After<Self::Endpoint, F>
where
F: Fn(<Self::Endpoint as Endpoint>::Output) -> Fut + Send + Sync + 'static,
Fut: Future<Output = R> + Send + 'static,
R: IntoResponse,
Self: Sized,
{ ... }
fn map_to_response(self) -> MapToResponse<Self::Endpoint>
where
Self: Sized,
{ ... }
fn map_to_result(self) -> MapToResult<Self::Endpoint>
where
Self: Sized,
{ ... }
fn and_then<F, Fut, Err, R, R2>(self, f: F) -> AndThen<Self::Endpoint, F>
where
F: Fn(R) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R2, Err>> + Send + 'static,
Err: IntoResponse,
R: IntoResponse,
R2: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = Result<R, Err>> + Sized,
{ ... }
fn map_ok<F, Fut, Err, R, R2>(self, f: F) -> MapOk<Self::Endpoint, F>
where
F: Fn(R) -> Fut + Send + Sync + 'static,
Fut: Future<Output = R2> + Send + 'static,
R: IntoResponse,
R2: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = Result<R, Err>> + Sized,
{ ... }
fn map_err<F, Fut, InErr, OutErr, R>(
self,
f: F
) -> MapErr<Self::Endpoint, F>
where
F: Fn(InErr) -> Fut + Send + Sync + 'static,
Fut: Future<Output = OutErr> + Send + 'static,
InErr: IntoResponse,
OutErr: IntoResponse,
R: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = Result<R, InErr>> + Sized,
{ ... }
}
Expand description
Extension trait for Endpoint
.
Provided methods
Wrap the endpoint in a Box.
Use middleware to transform this endpoint.
Example
use poem::{
get, handler, http::StatusCode, middleware::AddData, web::Data, Endpoint, EndpointExt,
Request, Route,
};
#[handler]
async fn index(Data(data): Data<&i32>) -> String {
format!("{}", data)
}
let app = Route::new().at("/", get(index)).with(AddData::new(100i32));
let resp = app.call(Request::default()).await;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "100");
A helper function, similar to with(AddData(T))
.
Example
use poem::{handler, http::StatusCode, web::Data, Endpoint, EndpointExt, Request};
#[handler]
async fn index(data: Data<&i32>) -> String {
format!("{}", data.0)
}
let mut resp = index.data(100i32).call(Request::default()).await;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.take_body().into_string().await.unwrap(), "100");
Maps the request of this endpoint.
Example
use poem::{handler, http::StatusCode, Endpoint, EndpointExt, Error, Request, Result};
#[handler]
async fn index(data: String) -> String {
data
}
let mut resp = index
.before(|mut req| async move {
req.set_body("abc");
req
})
.call(Request::default())
.await;
assert_eq!(resp.take_body().into_string().await.unwrap(), "abc");
Maps the output of this endpoint.
Example
use poem::{handler, http::StatusCode, Endpoint, EndpointExt, Error, Request, Result};
#[handler]
async fn index() -> &'static str {
"abc"
}
let mut resp = index
.after(|mut resp| async move {
resp.take_body().into_string().await.unwrap() + "def"
})
.call(Request::default())
.await;
assert_eq!(resp, "abcdef");
fn map_to_response(self) -> MapToResponse<Self::Endpoint> where
Self: Sized,
fn map_to_response(self) -> MapToResponse<Self::Endpoint> where
Self: Sized,
Convert the output of this endpoint into a response.
Response
.
Example
use poem::{
endpoint::make, http::StatusCode, Endpoint, EndpointExt, Error, Request, Response, Result,
};
let ep =
make(|_| async { Err::<(), Error>(Error::new(StatusCode::BAD_REQUEST)) }).map_to_response();
let resp: Response = ep.call(Request::default()).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
fn map_to_result(self) -> MapToResult<Self::Endpoint> where
Self: Sized,
fn map_to_result(self) -> MapToResult<Self::Endpoint> where
Self: Sized,
Convert the output of this endpoint into a result Result<Response>
.
Response
.
Example
use poem::{
endpoint::make, http::StatusCode, Endpoint, EndpointExt, Error, Request, Response, Result,
};
let ep = make(|_| async { Response::builder().status(StatusCode::BAD_REQUEST).finish() })
.map_to_result();
let resp: Result<Response> = ep.call(Request::default()).await;
assert_eq!(resp.unwrap_err().status(), StatusCode::BAD_REQUEST);
fn and_then<F, Fut, Err, R, R2>(self, f: F) -> AndThen<Self::Endpoint, F> where
F: Fn(R) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R2, Err>> + Send + 'static,
Err: IntoResponse,
R: IntoResponse,
R2: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = Result<R, Err>> + Sized,
fn and_then<F, Fut, Err, R, R2>(self, f: F) -> AndThen<Self::Endpoint, F> where
F: Fn(R) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<R2, Err>> + Send + 'static,
Err: IntoResponse,
R: IntoResponse,
R2: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = Result<R, Err>> + Sized,
Calls f
if the result is Ok
, otherwise returns the Err
value of
self.
Example
use poem::{
endpoint::make, http::StatusCode, Endpoint, EndpointExt, Error, Request, Response, Result,
};
let ep1 = make(|_| async { Ok::<_, Error>("hello") })
.and_then(|value| async move { Ok(format!("{}, world!", value)) });
let ep2 = make(|_| async { Err::<String, _>(Error::new(StatusCode::BAD_REQUEST)) })
.and_then(|value| async move { Ok(format!("{}, world!", value)) });
let resp: String = ep1.call(Request::default()).await.unwrap();
assert_eq!(resp, "hello, world!");
let err: Error = ep2.call(Request::default()).await.unwrap_err();
assert_eq!(err.status(), StatusCode::BAD_REQUEST);
Maps the response of this endpoint.
Example
use poem::{
endpoint::make, http::StatusCode, Endpoint, EndpointExt, Error, Request, Response, Result,
};
let ep =
make(|_| async { Ok("hello") }).map_ok(|value| async move { format!("{}, world!", value) });
let mut resp: String = ep.call(Request::default()).await.unwrap();
assert_eq!(resp, "hello, world!");
fn map_err<F, Fut, InErr, OutErr, R>(self, f: F) -> MapErr<Self::Endpoint, F> where
F: Fn(InErr) -> Fut + Send + Sync + 'static,
Fut: Future<Output = OutErr> + Send + 'static,
InErr: IntoResponse,
OutErr: IntoResponse,
R: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = Result<R, InErr>> + Sized,
fn map_err<F, Fut, InErr, OutErr, R>(self, f: F) -> MapErr<Self::Endpoint, F> where
F: Fn(InErr) -> Fut + Send + Sync + 'static,
Fut: Future<Output = OutErr> + Send + 'static,
InErr: IntoResponse,
OutErr: IntoResponse,
R: IntoResponse,
Self: Sized,
Self::Endpoint: Endpoint<Output = Result<R, InErr>> + Sized,
Maps the error of this endpoint.
Example
use poem::{
endpoint::make, http::StatusCode, Endpoint, EndpointExt, Error, IntoResponse, Request,
Response, Result,
};
struct CustomError;
impl IntoResponse for CustomError {
fn into_response(self) -> Response {
Response::builder()
.status(StatusCode::UNAUTHORIZED)
.finish()
}
}
let ep = make(|_| async { Err::<(), _>(CustomError) })
.map_err(|_| async move { Error::new(StatusCode::INTERNAL_SERVER_ERROR) });
let err = ep.call(Request::default()).await.unwrap_err();
assert_eq!(err.status(), StatusCode::INTERNAL_SERVER_ERROR);