Skip to content

Commit

Permalink
changed to await
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Whalley <[email protected]>
  • Loading branch information
No9 committed Mar 28, 2021
1 parent 0589694 commit 00770d9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
1 change: 1 addition & 0 deletions cloudevents-sdk-tide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tide = { version = "0.16" }
lazy_static = "1.4.0"
http-types = "2.10.0"
bytes = "^0.5"
async-trait = "0.1.48"

[dev-dependencies]
serde = { version = "1.0.117", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions cloudevents-sdk-tide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! req.insert_header("ce-type", "example.test");
//! req.insert_header("ce-source", "http://localhost/");
//! let bytes = req.body_bytes().await.unwrap();
//! let event : Event = req.to_event(bytes)?;
//! let event : Event = req.to_event(bytes).await?;
//! let resp = Response::builder(200).body(Body::from_json(&event)?).build();
//! Ok(resp)
//! }
Expand All @@ -38,7 +38,7 @@
//! .data("application/json", json!({"hello": "world"}))
//! .build()
//! .expect("No error while building the event"),
//! )?
//! ).await?
//! )
//! }
//! ```
Expand Down
20 changes: 11 additions & 9 deletions cloudevents-sdk-tide/src/server_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::headers;
use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use cloudevents::event::SpecVersion;
use cloudevents::message::{
Expand Down Expand Up @@ -105,7 +106,7 @@ impl<'a> MessageDeserializer for RequestDeserializer {
}

/// Method to transform an incoming [`Request`] to [`Event`].
pub fn request_to_event(
pub async fn request_to_event(
headers: HashMap<String, String>,
body: Vec<u8>,
) -> std::result::Result<Event, tide::Error> {
Expand All @@ -119,18 +120,19 @@ pub fn request_to_event(
///
/// This trait is sealed and cannot be implemented for types outside of this crate.
#[allow(patterns_in_fns_without_body)]
#[async_trait]
pub trait RequestExt: private::Sealed {
/// Convert this [`Request`] into an [`Event`].
fn to_event(&self, mut body: Vec<u8>) -> std::result::Result<Event, tide::Error>;
async fn to_event(&self, mut body: Vec<u8>) -> std::result::Result<Event, tide::Error>;
}

impl<State> RequestExt for Request<State> {
fn to_event(&self, body: Vec<u8>) -> std::result::Result<Event, tide::Error> {
#[async_trait]
impl<State: Clone + Send + Sync + 'static> RequestExt for Request<State> {
async fn to_event(&self, body: Vec<u8>) -> std::result::Result<Event, tide::Error> {
let mut headers = HashMap::new();
for (n, v) in self.iter() {
headers.insert(String::from(n.as_str()), String::from(v.as_str()));
}
request_to_event(headers, body)
request_to_event(headers, body).await
}
}

Expand Down Expand Up @@ -165,7 +167,7 @@ mod tests {
.unwrap();

let body = req.body_bytes().await.unwrap();
let evtresp: Event = req.to_event(body).unwrap();
let evtresp: Event = req.to_event(body).await.unwrap();

assert_eq!(expected, evtresp);
Ok(Body::from_json(&evtresp)?)
Expand Down Expand Up @@ -209,7 +211,7 @@ mod tests {
.build()
.unwrap();

let evtresp: Event = req.to_event(body.to_vec()).unwrap();
let evtresp: Event = req.to_event(body.to_vec()).await.unwrap();
assert_eq!(expected, evtresp);
Ok(Body::from_json(&evtresp)?)
});
Expand Down Expand Up @@ -253,7 +255,7 @@ mod tests {
.build()
.unwrap();

let evtresp: Event = req.to_event(vec).unwrap();
let evtresp: Event = req.to_event(vec).await.unwrap();
assert_eq!(expected, evtresp);
Ok(Body::from_json(&evtresp)?)
});
Expand Down
18 changes: 10 additions & 8 deletions cloudevents-sdk-tide/src/server_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::headers;
use async_trait::async_trait;
use cloudevents::event::SpecVersion;
use cloudevents::message::{
BinaryDeserializer, BinarySerializer, MessageAttributeValue, Result, StructuredSerializer,
Expand Down Expand Up @@ -61,7 +62,7 @@ impl StructuredSerializer<Response> for ResponseSerializer {
}

/// Method to fill an [`Response`] with an [`Event`].
pub fn event_to_response(
pub async fn event_to_response(
event: Event,
response: Response,
) -> std::result::Result<Response, tide::Error> {
Expand All @@ -72,14 +73,15 @@ pub fn event_to_response(
/// Extension Trait for [`Response`] which acts as a wrapper for the function [`event_to_response()`].
///
/// This trait is sealed and cannot be implemented for types outside of this crate.
#[async_trait]
pub trait ResponseBuilderExt: private::Sealed {
/// Fill this [`Response`] with an [`Event`].
fn event(self, event: Event) -> std::result::Result<Response, tide::Error>;
async fn event(self, event: Event) -> std::result::Result<Response, tide::Error>;
}

#[async_trait]
impl ResponseBuilderExt for Response {
fn event(self, event: Event) -> std::result::Result<Response, tide::Error> {
event_to_response(event, self)
async fn event(self, event: Event) -> std::result::Result<Response, tide::Error> {
event_to_response(event, self).await
}
}

Expand All @@ -106,7 +108,7 @@ mod tests {
.build()
.unwrap();

let resp = Response::new(200).event(input).unwrap();
let resp = Response::new(200).event(input).await.unwrap();

assert_eq!(resp.header("ce-specversion").unwrap().as_str(), "1.0");
assert_eq!(resp.header("ce-id").unwrap().as_str(), "0001");
Expand All @@ -131,7 +133,7 @@ mod tests {
.build()
.unwrap();

let resp = Response::new(200).event(input).unwrap();
let resp = Response::new(200).event(input).await.unwrap();

assert_eq!(resp.header("ce-specversion").unwrap().as_str(), "1.0");
assert_eq!(resp.header("ce-id").unwrap().as_str(), "0001");
Expand Down Expand Up @@ -161,7 +163,7 @@ mod tests {
.build()
.unwrap();

let resp = Response::new(200).event(input).unwrap();
let resp = Response::new(200).event(input).await.unwrap();
Ok(resp)
});

Expand Down

0 comments on commit 00770d9

Please sign in to comment.