Reflected changes to actix-web module
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
parent
534cf01fd2
commit
4814505b92
|
@ -4,8 +4,8 @@ use actix_web::web::{Bytes, BytesMut};
|
||||||
use actix_web::{web, HttpMessage, HttpRequest};
|
use actix_web::{web, HttpMessage, HttpRequest};
|
||||||
use cloudevents::event::SpecVersion;
|
use cloudevents::event::SpecVersion;
|
||||||
use cloudevents::message::{
|
use cloudevents::message::{
|
||||||
BinaryDeserializer, BinarySerializer, Encoding, Error, MessageAttributeValue,
|
BinaryDeserializer, BinarySerializer, Encoding, MessageAttributeValue,
|
||||||
MessageDeserializer, StructuredDeserializer, StructuredSerializer,
|
MessageDeserializer, StructuredDeserializer, StructuredSerializer, Result
|
||||||
};
|
};
|
||||||
use cloudevents::{message, Event};
|
use cloudevents::{message, Event};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
@ -27,7 +27,7 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
|
||||||
fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(
|
fn deserialize_binary<R: Sized, V: BinarySerializer<R>>(
|
||||||
self,
|
self,
|
||||||
mut visitor: V,
|
mut visitor: V,
|
||||||
) -> Result<R, Error> {
|
) -> Result<R> {
|
||||||
if self.encoding() != Encoding::BINARY {
|
if self.encoding() != Encoding::BINARY {
|
||||||
return Err(message::Error::WrongEncoding {});
|
return Err(message::Error::WrongEncoding {});
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
|
||||||
unwrap_optional_header!(self.req.headers(), headers::SPEC_VERSION_HEADER).unwrap()?,
|
unwrap_optional_header!(self.req.headers(), headers::SPEC_VERSION_HEADER).unwrap()?,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
visitor.set_spec_version(spec_version.clone())?;
|
visitor = visitor.set_spec_version(spec_version.clone())?;
|
||||||
|
|
||||||
let attributes = cloudevents::event::spec_version::ATTRIBUTE_NAMES
|
let attributes = cloudevents::event::spec_version::ATTRIBUTE_NAMES
|
||||||
.get(&spec_version)
|
.get(&spec_version)
|
||||||
|
@ -50,12 +50,12 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
|
||||||
let name = &hn.as_str()["ce-".len()..];
|
let name = &hn.as_str()["ce-".len()..];
|
||||||
|
|
||||||
if attributes.contains(&name) {
|
if attributes.contains(&name) {
|
||||||
visitor.set_attribute(
|
visitor = visitor.set_attribute(
|
||||||
name,
|
name,
|
||||||
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
||||||
)?
|
)?
|
||||||
} else {
|
} else {
|
||||||
visitor.set_extension(
|
visitor = visitor.set_extension(
|
||||||
name,
|
name,
|
||||||
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
||||||
)?
|
)?
|
||||||
|
@ -63,7 +63,7 @@ impl<'a> BinaryDeserializer for HttpRequestDeserializer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(hv) = self.req.headers().get("content-type") {
|
if let Some(hv) = self.req.headers().get("content-type") {
|
||||||
visitor.set_attribute(
|
visitor = visitor.set_attribute(
|
||||||
"datacontenttype",
|
"datacontenttype",
|
||||||
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
MessageAttributeValue::String(String::from(header_value_to_str!(hv)?)),
|
||||||
)?
|
)?
|
||||||
|
@ -81,7 +81,7 @@ impl<'a> StructuredDeserializer for HttpRequestDeserializer<'a> {
|
||||||
fn deserialize_structured<R: Sized, V: StructuredSerializer<R>>(
|
fn deserialize_structured<R: Sized, V: StructuredSerializer<R>>(
|
||||||
self,
|
self,
|
||||||
visitor: V,
|
visitor: V,
|
||||||
) -> Result<R, Error> {
|
) -> Result<R> {
|
||||||
if self.encoding() != Encoding::STRUCTURED {
|
if self.encoding() != Encoding::STRUCTURED {
|
||||||
return Err(message::Error::WrongEncoding {});
|
return Err(message::Error::WrongEncoding {});
|
||||||
}
|
}
|
||||||
|
@ -110,7 +110,7 @@ impl<'a> MessageDeserializer for HttpRequestDeserializer<'a> {
|
||||||
pub async fn request_to_event(
|
pub async fn request_to_event(
|
||||||
req: &HttpRequest,
|
req: &HttpRequest,
|
||||||
mut payload: web::Payload,
|
mut payload: web::Payload,
|
||||||
) -> Result<Event, actix_web::error::Error> {
|
) -> std::result::Result<Event, actix_web::error::Error> {
|
||||||
let mut bytes = BytesMut::new();
|
let mut bytes = BytesMut::new();
|
||||||
while let Some(item) = payload.next().await {
|
while let Some(item) = payload.next().await {
|
||||||
bytes.extend_from_slice(&item?);
|
bytes.extend_from_slice(&item?);
|
||||||
|
|
|
@ -4,7 +4,7 @@ use actix_web::http::{HeaderName, HeaderValue};
|
||||||
use actix_web::HttpResponse;
|
use actix_web::HttpResponse;
|
||||||
use cloudevents::event::SpecVersion;
|
use cloudevents::event::SpecVersion;
|
||||||
use cloudevents::message::{
|
use cloudevents::message::{
|
||||||
BinaryDeserializer, BinarySerializer, Error, MessageAttributeValue, SerializationResult,
|
BinaryDeserializer, BinarySerializer, MessageAttributeValue, Result,
|
||||||
StructuredSerializer,
|
StructuredSerializer,
|
||||||
};
|
};
|
||||||
use cloudevents::Event;
|
use cloudevents::Event;
|
||||||
|
@ -21,41 +21,41 @@ impl HttpResponseSerializer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BinarySerializer<HttpResponse> for HttpResponseSerializer {
|
impl BinarySerializer<HttpResponse> for HttpResponseSerializer {
|
||||||
fn set_spec_version(&mut self, spec_version: SpecVersion) -> SerializationResult {
|
fn set_spec_version(mut self, spec_version: SpecVersion) -> Result<Self> {
|
||||||
self.builder.set_header(
|
self.builder.set_header(
|
||||||
headers::SPEC_VERSION_HEADER.clone(),
|
headers::SPEC_VERSION_HEADER.clone(),
|
||||||
str_to_header_value!(spec_version.as_str())?,
|
str_to_header_value!(spec_version.as_str())?,
|
||||||
);
|
);
|
||||||
SerializationResult::Ok(())
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_attribute(&mut self, name: &str, value: MessageAttributeValue) -> SerializationResult {
|
fn set_attribute(mut self, name: &str, value: MessageAttributeValue) -> Result<Self> {
|
||||||
self.builder.set_header(
|
self.builder.set_header(
|
||||||
headers::ATTRIBUTES_TO_HEADERS.get(name).unwrap().clone(),
|
headers::ATTRIBUTES_TO_HEADERS.get(name).unwrap().clone(),
|
||||||
str_to_header_value!(value.to_string().as_str())?,
|
str_to_header_value!(value.to_string().as_str())?,
|
||||||
);
|
);
|
||||||
SerializationResult::Ok(())
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_extension(&mut self, name: &str, value: MessageAttributeValue) -> SerializationResult {
|
fn set_extension(mut self, name: &str, value: MessageAttributeValue) -> Result<Self> {
|
||||||
self.builder.set_header(
|
self.builder.set_header(
|
||||||
attribute_name_to_header!(name)?,
|
attribute_name_to_header!(name)?,
|
||||||
str_to_header_value!(value.to_string().as_str())?,
|
str_to_header_value!(value.to_string().as_str())?,
|
||||||
);
|
);
|
||||||
SerializationResult::Ok(())
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end_with_data(mut self, bytes: Vec<u8>) -> Result<HttpResponse, Error> {
|
fn end_with_data(mut self, bytes: Vec<u8>) -> Result<HttpResponse> {
|
||||||
Ok(self.builder.body(bytes))
|
Ok(self.builder.body(bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(mut self) -> Result<HttpResponse, Error> {
|
fn end(mut self) -> Result<HttpResponse> {
|
||||||
Ok(self.builder.finish())
|
Ok(self.builder.finish())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StructuredSerializer<HttpResponse> for HttpResponseSerializer {
|
impl StructuredSerializer<HttpResponse> for HttpResponseSerializer {
|
||||||
fn set_structured_event(mut self, bytes: Vec<u8>) -> Result<HttpResponse, Error> {
|
fn set_structured_event(mut self, bytes: Vec<u8>) -> Result<HttpResponse> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.builder
|
.builder
|
||||||
.set_header(
|
.set_header(
|
||||||
|
@ -70,7 +70,7 @@ impl StructuredSerializer<HttpResponse> for HttpResponseSerializer {
|
||||||
pub async fn event_to_response(
|
pub async fn event_to_response(
|
||||||
event: Event,
|
event: Event,
|
||||||
response: HttpResponseBuilder,
|
response: HttpResponseBuilder,
|
||||||
) -> Result<HttpResponse, actix_web::error::Error> {
|
) -> std::result::Result<HttpResponse, actix_web::error::Error> {
|
||||||
BinaryDeserializer::deserialize_binary(event, HttpResponseSerializer::new(response))
|
BinaryDeserializer::deserialize_binary(event, HttpResponseSerializer::new(response))
|
||||||
.map_err(actix_web::error::ErrorBadRequest)
|
.map_err(actix_web::error::ErrorBadRequest)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use super::{BinarySerializer, Encoding, Error, StructuredSerializer, Result};
|
use super::{BinarySerializer, Encoding, StructuredSerializer, Result};
|
||||||
use crate::Event;
|
use crate::Event;
|
||||||
|
|
||||||
pub trait StructuredDeserializer
|
pub trait StructuredDeserializer
|
||||||
|
|
Loading…
Reference in New Issue