Module actix

Module actix 

Source
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§

HttpRequestExt
Extension Trait for HttpRequest which acts as a wrapper for the function request_to_event().
HttpResponseBuilderExt
Extension Trait for HttpResponseBuilder which acts as a wrapper for the function event_to_response().

Functions§

event_to_response
Method to fill an HttpResponseBuilder with an Event.
request_to_event
Method to transform an incoming HttpRequest to Event.