Skip to content

Commit b832b6b

Browse files
Redesigned EventBuilder (cloudevents#53)
* Progress Signed-off-by: Francesco Guardiani <[email protected]> * Builder finished Signed-off-by: Francesco Guardiani <[email protected]> * Fixed all integrations Signed-off-by: Francesco Guardiani <[email protected]> * Fmt'ed Signed-off-by: Francesco Guardiani <[email protected]> * Fmt'ed part 2 Signed-off-by: Francesco Guardiani <[email protected]> * Fixed tests in reqwest integration Signed-off-by: Francesco Guardiani <[email protected]> * fmt'ed again Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 2b91ee8 commit b832b6b

File tree

25 files changed

+662
-268
lines changed

25 files changed

+662
-268
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ cloudevents-sdk = "0.1.0"
3535
Now you can start creating events:
3636

3737
```rust
38-
use cloudevents::EventBuilder;
38+
use cloudevents::{EventBuilder, EventBuilderV10};
3939
use url::Url;
4040

41-
let event = EventBuilder::v03()
41+
let event = EventBuilderV10::new()
4242
.id("aaa")
4343
.source(Url::parse("http://localhost").unwrap())
4444
.ty("example.demo")
45-
.build();
45+
.build()
46+
.unwrap();
4647
```
4748

4849
Checkout the examples using our integrations with `actix-web` and `reqwest` to learn how to send and receive events:

cloudevents-sdk-actix-web/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ actix-rt = "1"
1717
lazy_static = "1.4.0"
1818
bytes = "^0.5"
1919
futures = "^0.3"
20-
serde_json = "^1.0"
2120

2221
[dev-dependencies]
23-
url = { version = "^2.1", features = ["serde"] }
22+
url = { version = "^2.1", features = ["serde"] }
23+
serde_json = "^1.0"
24+
chrono = { version = "^0.4", features = ["serde"] }

cloudevents-sdk-actix-web/src/server_request.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,32 @@ mod tests {
117117
use actix_web::test;
118118
use url::Url;
119119

120-
use cloudevents::EventBuilder;
120+
use chrono::Utc;
121+
use cloudevents::{EventBuilder, EventBuilderV10};
121122
use serde_json::json;
122123
use std::str::FromStr;
123124

124125
#[actix_rt::test]
125126
async fn test_request() {
126-
let expected = EventBuilder::new()
127+
let time = Utc::now();
128+
let expected = EventBuilderV10::new()
127129
.id("0001")
128130
.ty("example.test")
129-
.source(Url::from_str("http://localhost").unwrap())
131+
.source("http://localhost/")
132+
//TODO this is required now because the message deserializer implictly set default values
133+
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
134+
.time(time)
130135
.extension("someint", "10")
131-
.build();
136+
.build()
137+
.unwrap();
132138

133139
let (req, payload) = test::TestRequest::post()
134140
.header("ce-specversion", "1.0")
135141
.header("ce-id", "0001")
136142
.header("ce-type", "example.test")
137-
.header("ce-source", "http://localhost")
143+
.header("ce-source", "http://localhost/")
138144
.header("ce-someint", "10")
145+
.header("ce-time", time.to_rfc3339())
139146
.to_http_parts();
140147

141148
let resp = request_to_event(&req, web::Payload(payload)).await.unwrap();
@@ -144,22 +151,28 @@ mod tests {
144151

145152
#[actix_rt::test]
146153
async fn test_request_with_full_data() {
154+
let time = Utc::now();
147155
let j = json!({"hello": "world"});
148156

149-
let expected = EventBuilder::new()
157+
let expected = EventBuilderV10::new()
150158
.id("0001")
151159
.ty("example.test")
152160
.source(Url::from_str("http://localhost").unwrap())
161+
//TODO this is required now because the message deserializer implictly set default values
162+
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
163+
.time(time)
153164
.data("application/json", j.clone())
154165
.extension("someint", "10")
155-
.build();
166+
.build()
167+
.unwrap();
156168

157169
let (req, payload) = test::TestRequest::post()
158170
.header("ce-specversion", "1.0")
159171
.header("ce-id", "0001")
160172
.header("ce-type", "example.test")
161173
.header("ce-source", "http://localhost")
162174
.header("ce-someint", "10")
175+
.header("ce-time", time.to_rfc3339())
163176
.header("content-type", "application/json")
164177
.set_json(&j)
165178
.to_http_parts();

cloudevents-sdk-actix-web/src/server_response.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,20 @@ mod tests {
8282

8383
use actix_web::http::StatusCode;
8484
use actix_web::test;
85-
use cloudevents::EventBuilder;
85+
use cloudevents::{EventBuilder, EventBuilderV10};
8686
use futures::TryStreamExt;
8787
use serde_json::json;
8888
use std::str::FromStr;
8989

9090
#[actix_rt::test]
9191
async fn test_response() {
92-
let input = EventBuilder::new()
92+
let input = EventBuilderV10::new()
9393
.id("0001")
9494
.ty("example.test")
9595
.source(Url::from_str("http://localhost/").unwrap())
9696
.extension("someint", "10")
97-
.build();
97+
.build()
98+
.unwrap();
9899

99100
let resp = event_to_response(input, HttpResponseBuilder::new(StatusCode::OK))
100101
.await
@@ -130,13 +131,14 @@ mod tests {
130131
async fn test_response_with_full_data() {
131132
let j = json!({"hello": "world"});
132133

133-
let input = EventBuilder::new()
134+
let input = EventBuilderV10::new()
134135
.id("0001")
135136
.ty("example.test")
136137
.source(Url::from_str("http://localhost").unwrap())
137138
.data("application/json", j.clone())
138139
.extension("someint", "10")
139-
.build();
140+
.build()
141+
.unwrap();
140142

141143
let mut resp = event_to_response(input, HttpResponseBuilder::new(StatusCode::OK))
142144
.await

cloudevents-sdk-reqwest/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ repository = "https://github.com/cloudevents/sdk-rust"
1414
cloudevents-sdk = { version = "0.1.0", path = ".." }
1515
lazy_static = "1.4.0"
1616
bytes = "^0.5"
17-
serde_json = "^1.0"
1817

1918
[dependencies.reqwest]
2019
version = "0.10.4"
@@ -24,4 +23,6 @@ features = ["rustls-tls"]
2423
[dev-dependencies]
2524
mockito = "0.25.1"
2625
tokio = { version = "^0.2", features = ["full"] }
27-
url = { version = "^2.1" }
26+
url = { version = "^2.1" }
27+
serde_json = "^1.0"
28+
chrono = { version = "^0.4", features = ["serde"] }

cloudevents-sdk-reqwest/src/client_request.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mod tests {
7373
use mockito::{mock, Matcher};
7474

7575
use cloudevents::message::StructuredDeserializer;
76-
use cloudevents::EventBuilder;
76+
use cloudevents::{EventBuilder, EventBuilderV10};
7777
use serde_json::json;
7878
use url::Url;
7979

@@ -89,12 +89,13 @@ mod tests {
8989
.match_body(Matcher::Missing)
9090
.create();
9191

92-
let input = EventBuilder::new()
92+
let input = EventBuilderV10::new()
9393
.id("0001")
9494
.ty("example.test")
9595
.source(Url::from_str("http://localhost/").unwrap())
9696
.extension("someint", "10")
97-
.build();
97+
.build()
98+
.unwrap();
9899

99100
let client = reqwest::Client::new();
100101
event_to_request(input, client.post(&url))
@@ -121,13 +122,14 @@ mod tests {
121122
.match_body(Matcher::Exact(j.to_string()))
122123
.create();
123124

124-
let input = EventBuilder::new()
125+
let input = EventBuilderV10::new()
125126
.id("0001")
126127
.ty("example.test")
127128
.source(Url::from_str("http://localhost").unwrap())
128129
.data("application/json", j.clone())
129130
.extension("someint", "10")
130-
.build();
131+
.build()
132+
.unwrap();
131133

132134
let client = reqwest::Client::new();
133135
event_to_request(input, client.post(&url))
@@ -143,13 +145,14 @@ mod tests {
143145
async fn test_structured_request_with_full_data() {
144146
let j = json!({"hello": "world"});
145147

146-
let input = EventBuilder::new()
148+
let input = EventBuilderV10::new()
147149
.id("0001")
148150
.ty("example.test")
149151
.source(Url::from_str("http://localhost").unwrap())
150152
.data("application/json", j.clone())
151153
.extension("someint", "10")
152-
.build();
154+
.build()
155+
.unwrap();
153156

154157
let url = mockito::server_url();
155158
let m = mock("POST", "/")

cloudevents-sdk-reqwest/src/client_response.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ mod tests {
112112
use super::*;
113113
use mockito::mock;
114114

115-
use cloudevents::EventBuilder;
115+
use chrono::Utc;
116+
use cloudevents::{EventBuilder, EventBuilderV10};
116117
use serde_json::json;
117118
use std::str::FromStr;
118119
use url::Url;
119120

120121
#[tokio::test]
121122
async fn test_response() {
123+
let time = Utc::now();
122124
let url = mockito::server_url();
123125
let _m = mock("GET", "/")
124126
.with_status(200)
@@ -127,14 +129,19 @@ mod tests {
127129
.with_header("ce-type", "example.test")
128130
.with_header("ce-source", "http://localhost")
129131
.with_header("ce-someint", "10")
132+
.with_header("ce-time", &time.to_rfc3339())
130133
.create();
131134

132-
let expected = EventBuilder::new()
135+
let expected = EventBuilderV10::new()
133136
.id("0001")
134137
.ty("example.test")
138+
//TODO this is required now because the message deserializer implictly set default values
139+
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
140+
.time(time)
135141
.source(Url::from_str("http://localhost").unwrap())
136142
.extension("someint", "10")
137-
.build();
143+
.build()
144+
.unwrap();
138145

139146
let client = reqwest::Client::new();
140147
let res = client.get(&url).send().await.unwrap();
@@ -145,6 +152,7 @@ mod tests {
145152

146153
#[tokio::test]
147154
async fn test_response_with_full_data() {
155+
let time = Utc::now();
148156
let j = json!({"hello": "world"});
149157

150158
let url = mockito::server_url();
@@ -156,16 +164,21 @@ mod tests {
156164
.with_header("ce-source", "http://localhost/")
157165
.with_header("content-type", "application/json")
158166
.with_header("ce-someint", "10")
167+
.with_header("ce-time", &time.to_rfc3339())
159168
.with_body(j.to_string())
160169
.create();
161170

162-
let expected = EventBuilder::new()
171+
let expected = EventBuilderV10::new()
163172
.id("0001")
164173
.ty("example.test")
174+
//TODO this is required now because the message deserializer implictly set default values
175+
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
176+
.time(time)
165177
.source(Url::from_str("http://localhost").unwrap())
166178
.data("application/json", j.clone())
167179
.extension("someint", "10")
168-
.build();
180+
.build()
181+
.unwrap();
169182

170183
let client = reqwest::Client::new();
171184
let res = client.get(&url).send().await.unwrap();
@@ -176,14 +189,20 @@ mod tests {
176189

177190
#[tokio::test]
178191
async fn test_structured_response_with_full_data() {
192+
let time = Utc::now();
193+
179194
let j = json!({"hello": "world"});
180-
let expected = EventBuilder::new()
195+
let expected = EventBuilderV10::new()
181196
.id("0001")
182197
.ty("example.test")
198+
//TODO this is required now because the message deserializer implictly set default values
199+
// As soon as this defaulting doesn't happen anymore, we can remove it (Issues #40/#41)
200+
.time(time)
183201
.source(Url::from_str("http://localhost").unwrap())
184202
.data("application/json", j.clone())
185203
.extension("someint", "10")
186-
.build();
204+
.build()
205+
.unwrap();
187206

188207
let url = mockito::server_url();
189208
let _m = mock("GET", "/")

example-projects/actix-web-example/src/main.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer};
2-
use cloudevents::EventBuilder;
3-
use url::Url;
4-
use std::str::FromStr;
2+
use cloudevents::{EventBuilder, EventBuilderV10};
53
use serde_json::json;
4+
use std::str::FromStr;
5+
use url::Url;
66

77
#[post("/")]
88
async fn post_event(req: HttpRequest, payload: web::Payload) -> Result<String, actix_web::Error> {
@@ -16,15 +16,17 @@ async fn get_event() -> Result<HttpResponse, actix_web::Error> {
1616
let payload = json!({"hello": "world"});
1717

1818
Ok(cloudevents_sdk_actix_web::event_to_response(
19-
EventBuilder::new()
19+
EventBuilderV10::new()
2020
.id("0001")
2121
.ty("example.test")
2222
.source(Url::from_str("http://localhost/").unwrap())
2323
.data("application/json", payload)
2424
.extension("someint", "10")
25-
.build(),
26-
HttpResponse::Ok()
27-
).await?)
25+
.build()
26+
.unwrap(),
27+
HttpResponse::Ok(),
28+
)
29+
.await?)
2830
}
2931

3032
#[actix_rt::main]
@@ -39,8 +41,8 @@ async fn main() -> std::io::Result<()> {
3941
.service(post_event)
4042
.service(get_event)
4143
})
42-
.bind("127.0.0.1:9000")?
43-
.workers(1)
44-
.run()
45-
.await
44+
.bind("127.0.0.1:9000")?
45+
.workers(1)
46+
.run()
47+
.await
4648
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
use cloudevents::{EventBuilder, EventBuilderV10};
12
use wasm_bindgen::prelude::*;
23

34
#[wasm_bindgen]
4-
pub async fn run(target: String, ty: String, datacontenttype: String, data: String) -> Result<(), String> {
5-
let event = cloudevents::EventBuilder::new()
5+
pub async fn run(
6+
target: String,
7+
ty: String,
8+
datacontenttype: String,
9+
data: String,
10+
) -> Result<(), String> {
11+
let event = EventBuilderV10::new()
612
.ty(ty)
713
.data(datacontenttype, data)
8-
.build();
14+
.build()
15+
.unwrap();
916

1017
println!("Going to send event: {:?}", event);
1118

@@ -17,4 +24,4 @@ pub async fn run(target: String, ty: String, datacontenttype: String, data: Stri
1724
.map_err(|e| e.to_string())?;
1825

1926
Ok(())
20-
}
27+
}

0 commit comments

Comments
 (0)