Skip to content

Commit a72f09a

Browse files
Add new helpers, change builder interface (#23)
1 parent 2ed5396 commit a72f09a

File tree

2 files changed

+106
-21
lines changed

2 files changed

+106
-21
lines changed

edge-function/src/helpers.rs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl ResponseBuilder {
4545
self
4646
}
4747

48-
pub fn build(self, resp: ResponseOutparam) {
48+
pub fn send(self, resp: ResponseOutparam) {
4949
let resp_tx = OutgoingResponse::new(self.headers);
5050
let _ = resp_tx.set_status_code(self.status_code);
5151

@@ -78,14 +78,14 @@ pub fn parse_body(req: IncomingRequest) -> Result<Vec<u8>, String> {
7878
let mut request_body = Vec::new();
7979
let stream = match req.consume() {
8080
Ok(stream) => stream,
81-
Err(_) => {
81+
Err(_e) => {
8282
return Err("Failed to consume request stream".to_string());
8383
}
8484
};
8585
let stream = match stream.stream() {
8686
Ok(stream) => stream,
87-
Err(_) => {
88-
return Err("Failed to get request stream".to_string());
87+
Err(_e) => {
88+
return Err("Failed to get request stream: ".to_string());
8989
}
9090
};
9191

@@ -108,3 +108,76 @@ pub fn parse_body(req: IncomingRequest) -> Result<Vec<u8>, String> {
108108
}
109109
Ok(request_body)
110110
}
111+
112+
pub fn build_response(body: &str, status_code: u16, content_type: &str) -> ResponseBuilder {
113+
let mut builder = ResponseBuilder::new();
114+
builder
115+
.set_header("content-type", content_type)
116+
.set_status_code(status_code)
117+
.set_body(body);
118+
builder
119+
}
120+
pub fn build_response_html(body: &str, status_code: u16) -> ResponseBuilder {
121+
build_response(body, status_code, "text/html; charset=utf-8")
122+
}
123+
124+
#[allow(dead_code)]
125+
pub fn build_response_json(body: &str, status_code: u16) -> ResponseBuilder {
126+
build_response(body, status_code, "application/json")
127+
}
128+
129+
#[allow(dead_code)]
130+
pub fn build_response_json_error(message: &str, status_code: u16) -> ResponseBuilder {
131+
let body = format!("{{\"error\": \"{message}\"}}");
132+
build_response_json(&body, status_code)
133+
}
134+
135+
//#[cfg(test)]
136+
//mod tests {
137+
// use super::*;
138+
//
139+
// // Test ResponseBuilder header and status
140+
// #[test]
141+
// fn test_response_builder_setters() {
142+
// let mut builder = ResponseBuilder::new();
143+
// builder //.set_header("foo", "bar")
144+
// .set_status_code(404)
145+
// .set_body("hello world");
146+
// assert_eq!(builder.status_code, 404);
147+
// assert_eq!(builder.body_content.as_deref(), Some("hello world"));
148+
// let headers_map = parse_headers(&builder.headers);
149+
// assert!(headers_map.contains_key("foo"));
150+
// assert_eq!(headers_map.get("foo").unwrap(), &vec!["bar".to_string()]);
151+
// }
152+
//
153+
// #[test]
154+
// fn test_build_response_plain_text() {
155+
// let response = build_response("abc", 200, "text/plain");
156+
// assert_eq!(response.status_code, 200);
157+
// }
158+
//
159+
// #[test]
160+
// fn test_build_response_html() {
161+
// let response = build_response_html("abc", 201);
162+
// assert_eq!(response.status_code, 201);
163+
// }
164+
//
165+
// #[test]
166+
// fn test_build_response_json() {
167+
// let response = build_response_json("{\"a\":1}", 202);
168+
// assert_eq!(response.status_code, 202);
169+
// }
170+
//
171+
// #[test]
172+
// fn test_send_error_json() {
173+
// let response = build_response_json_error("fail", 500);
174+
// assert_eq!(response.status_code, 500);
175+
// }
176+
//
177+
// #[test]
178+
// fn test_response_builder_default() {
179+
// let builder = ResponseBuilder::default();
180+
// assert_eq!(builder.status_code, 200);
181+
// assert!(builder.body_content.is_none());
182+
// }
183+
//}

edge-function/src/lib.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,49 @@ use world::bindings::Component;
1010

1111
impl Guest for Component {
1212
fn handle(req: IncomingRequest, resp: ResponseOutparam) {
13+
let body = include_str!("index.html");
14+
1315
let _ = match Settings::from_req(&req) {
1416
Ok(settings) => settings,
1517
Err(_) => {
16-
let mut builder = helpers::ResponseBuilder::new();
17-
builder
18-
.set_header("content-type", "text/html")
19-
.set_status_code(200)
20-
.set_body(include_str!("index.html"));
21-
builder.build(resp);
18+
let response = helpers::build_response_html(body, 200);
19+
response.send(resp);
2220
return;
2321
}
2422
};
2523

2624
let _ = helpers::parse_body(req);
2725

28-
//let example = waki::Client::new()
26+
// Uncomment the following lines to see how to use the waki client
27+
// let example = waki::Client::new()
2928
// .get("https://example.com")
3029
// .send()
3130
// .unwrap()
3231
// .body()
3332
// .unwrap();
34-
35-
//let body = String::from_utf8_lossy(&example).to_string();
36-
let body = include_str!("index.html");
37-
38-
let mut builder = helpers::ResponseBuilder::new();
39-
builder
40-
.set_header("content-type", "text/html")
41-
.set_status_code(200)
42-
.set_body(body);
43-
builder.build(resp);
33+
// let body = String::from_utf8_lossy(&example).to_string();
34+
35+
// Uncomment the following lines to see how to parse the request body and parse it to JSON
36+
// let request_body = match helpers::parse_body(req) {
37+
// Ok(body) => body,
38+
// Err(e) => {
39+
// let response = helpers::build_response_json_error(&e, 400);
40+
// response.send(resp);
41+
// return;
42+
// }
43+
// };
44+
// // parse body to JSON
45+
// let body_json: serde_json::Value = match serde_json::from_slice(&request_body) {
46+
// Ok(json) => json,
47+
// Err(_) => {
48+
// let response = helpers::build_response_json_error("Invalid JSON in request body", 400);
49+
// response.send(resp);
50+
// return;
51+
// }
52+
// };
53+
54+
let response = helpers::build_response_html(body, 200);
55+
response.send(resp);
4456
}
4557
}
4658

0 commit comments

Comments
 (0)