Available on crate feature
actix only.Expand description
This module integrates the cloudevents-sdk with Actix web to easily send and receive CloudEvents.
To deserialize an HTTP request as CloudEvent:
use cloudevents::Event;
use actix_web::post;
#[post("/")]
async fn post_event(event: Event) -> Result<String, actix_web::Error> {
println!("Received Event: {:?}", event);
Ok(format!("{:?}", event))
}For more complex applications, access the Payload directly:
use cloudevents::binding::actix::HttpRequestExt;
use actix_web::{HttpRequest, web, post};
#[post("/")]
async fn post_event(req: HttpRequest, payload: web::Payload) -> Result<String, actix_web::Error> {
let event = req.to_event(payload).await?;
println!("Received Event: {:?}", event);
Ok(format!("{:?}", event))
}To serialize a CloudEvent to an HTTP response:
use actix_web::get;
use cloudevents::{Event, EventBuilderV10, EventBuilder};
use serde_json::json;
#[get("/")]
async fn get_event() -> Event {
let payload = json!({"hello": "world"});
EventBuilderV10::new()
.id("0001")
.ty("example.test")
.source("http://localhost/")
.data("application/json", payload)
.extension("someint", "10")
.build()
.unwrap()
}For more complex applications, use the HTTP response builder extension:
use cloudevents::binding::actix::HttpResponseBuilderExt;
use actix_web::{get, HttpResponse};
use cloudevents::{EventBuilderV10, EventBuilder};
use serde_json::json;
#[get("/")]
async fn get_event() -> Result<HttpResponse, actix_web::Error> {
HttpResponse::Ok()
.event(
EventBuilderV10::new()
.id("0001")
.ty("example.test")
.source("http://localhost/")
.data("application/json", json!({"hello": "world"}))
.build()
.expect("No error while building the event"),
)
}Traits§
- Http
Request Ext - Extension Trait for
HttpRequestwhich acts as a wrapper for the functionrequest_to_event(). - Http
Response Builder Ext - Extension Trait for
HttpResponseBuilderwhich acts as a wrapper for the functionevent_to_response().
Functions§
- event_
to_ response - Method to fill an
HttpResponseBuilderwith anEvent. - request_
to_ event - Method to transform an incoming
HttpRequesttoEvent.