Account for poem 1.2 breaking changes (#166)

* Account for poem 1.2 breaking changes

* Update the poem example, too

Signed-off-by: Jim Crossley <jim@crossleys.org>
This commit is contained in:
Jim Crossley 2022-01-06 00:37:49 -05:00 committed by GitHub
parent 7f538a3f37
commit f3d106b3b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 40 deletions

View File

@ -47,7 +47,7 @@ http = { version = "0.2", optional = true }
hyper = { version = "^0.14", optional = true }
axum-lib = { version = "^0.2", optional = true, package = "axum" }
http-body = { version = "^0.4", optional = true }
poem-lib = { version = "1.0.23", optional = true, package = "poem" }
poem-lib = { version = "1", optional = true, package = "poem" }
[target."cfg(not(target_arch = \"wasm32\"))".dependencies]
hostname = "^0.3"

View File

@ -7,7 +7,7 @@ edition = "2021"
cloudevents-sdk = { path = "../..", features = ["poem"] }
tokio = { version = "1.13", features = ["macros", "rt-multi-thread"] }
tracing = "0.1"
poem = { version = "=1.0.23" }
poem = { version = "1" }
tracing-subscriber = "0.2"
serde_json = "1.0"

View File

@ -46,7 +46,7 @@ async fn main() -> Result<(), std::io::Error> {
}
tracing_subscriber::fmt::init();
let server = Server::new(TcpListener::bind("127.0.0.1:8080")).await?;
let server = Server::new(TcpListener::bind("127.0.0.1:8080"));
server.run(echo_app()).await
}
@ -79,7 +79,7 @@ mod tests {
.header("content-type", "application/json")
.body(Body::from_json(&j).unwrap());
let resp: Response = app.call(request).await;
let resp: Response = app.call(request).await.unwrap();
assert_eq!(
resp.headers()
.get("ce-specversion")

View File

@ -1,43 +1,20 @@
use async_trait::async_trait;
use poem_lib::error::ReadBodyError;
use poem_lib::error::ResponseError;
use poem_lib::http::StatusCode;
use poem_lib::{FromRequest, IntoResponse, Request, RequestBody, Response};
use poem_lib::{FromRequest, Request, RequestBody, Result};
use crate::binding::http::to_event;
use crate::Event;
#[derive(Debug)]
pub enum ParseEventError {
ReadBody(ReadBodyError),
ParseEvent(crate::message::Error),
}
impl From<ReadBodyError> for ParseEventError {
fn from(err: ReadBodyError) -> Self {
ParseEventError::ReadBody(err)
}
}
impl From<crate::message::Error> for ParseEventError {
fn from(err: crate::message::Error) -> Self {
ParseEventError::ParseEvent(err)
}
}
impl IntoResponse for ParseEventError {
fn into_response(self) -> Response {
match self {
ParseEventError::ReadBody(err) => err.into_response(),
ParseEventError::ParseEvent(err) => (StatusCode::BAD_REQUEST, err.to_string()).into(),
}
impl ResponseError for crate::message::Error {
fn status(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
#[async_trait]
impl<'a> FromRequest<'a> for Event {
type Error = ParseEventError;
async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self, Self::Error> {
async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self> {
Ok(to_event(req.headers(), body.take()?.into_vec().await?)?)
}
}
@ -79,12 +56,9 @@ mod tests {
.finish();
let (req, mut body) = req.split();
let resp = Event::from_request(&req, &mut body).await.into_response();
let resp = Event::from_request(&req, &mut body).await.err().unwrap();
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
assert_eq!(
resp.into_body().into_string().await.unwrap(),
"Invalid specversion BAD SPECIFICATION"
);
assert_eq!(resp.to_string(), "Invalid specversion BAD SPECIFICATION");
}
#[tokio::test]

View File

@ -55,5 +55,3 @@
mod extractor;
mod response;
pub use extractor::ParseEventError;