forked from aws/aws-lambda-rust-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.rs
More file actions
156 lines (138 loc) · 4.61 KB
/
Copy pathrequests.rs
File metadata and controls
156 lines (138 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::{types::Diagnostic, Error};
use http::{Method, Request, Response, Uri};
use hyper::Body;
use serde::Serialize;
use std::str::FromStr;
pub(crate) trait IntoRequest {
fn into_req(self) -> Result<Request<Body>, Error>;
}
pub(crate) trait IntoResponse {
fn into_rsp(self) -> Result<Response<Body>, Error>;
}
// /runtime/invocation/next
#[derive(Debug, PartialEq)]
pub(crate) struct NextEventRequest;
impl IntoRequest for NextEventRequest {
fn into_req(self) -> Result<Request<Body>, Error> {
let req = Request::builder()
.method(Method::GET)
.uri(Uri::from_static("/2018-06-01/runtime/invocation/next"))
.body(Body::empty())?;
Ok(req)
}
}
#[derive(Debug, PartialEq)]
pub struct NextEventResponse<'a> {
// lambda-runtime-aws-request-id
pub request_id: &'a str,
// lambda-runtime-deadline-ms
pub deadline: u64,
// lambda-runtime-invoked-function-arn
pub arn: &'a str,
// lambda-runtime-trace-id
pub trace_id: &'a str,
// the actual body,
pub body: Vec<u8>,
}
impl<'a> IntoResponse for NextEventResponse<'a> {
fn into_rsp(self) -> Result<Response<Body>, Error> {
let rsp = Response::builder()
.header("lambda-runtime-aws-request-id", self.request_id)
.header("lambda-runtime-deadline-ms", self.deadline)
.header("lambda-runtime-invoked-function-arn", self.arn)
.header("lambda-runtime-trace-id", self.trace_id)
.body(Body::from(self.body))?;
Ok(rsp)
}
}
#[test]
fn test_next_event_request() {
let req = NextEventRequest;
let req = req.into_req().unwrap();
assert_eq!(req.method(), Method::GET);
assert_eq!(req.uri(), &Uri::from_static("/2018-06-01/runtime/invocation/next"));
}
// /runtime/invocation/{AwsRequestId}/response
pub(crate) struct EventCompletionRequest<'a, T> {
pub(crate) request_id: &'a str,
pub(crate) body: T,
}
impl<'a, T> IntoRequest for EventCompletionRequest<'a, T>
where
T: for<'serialize> Serialize,
{
fn into_req(self) -> Result<Request<Body>, Error> {
let uri = format!("/2018-06-01/runtime/invocation/{}/response", self.request_id);
let uri = Uri::from_str(&uri)?;
let body = serde_json::to_vec(&self.body)?;
let body = Body::from(body);
let req = Request::builder().method(Method::POST).uri(uri).body(body)?;
Ok(req)
}
}
#[test]
fn test_event_completion_request() {
let req = EventCompletionRequest {
request_id: "id",
body: "hello, world!",
};
let req = req.into_req().unwrap();
let expected = Uri::from_static("/2018-06-01/runtime/invocation/id/response");
assert_eq!(req.method(), Method::POST);
assert_eq!(req.uri(), &expected);
}
// /runtime/invocation/{AwsRequestId}/error
pub(crate) struct EventErrorRequest<'a> {
pub(crate) request_id: &'a str,
pub(crate) diagnostic: Diagnostic,
}
impl<'a> IntoRequest for EventErrorRequest<'a> {
fn into_req(self) -> Result<Request<Body>, Error> {
let uri = format!("/2018-06-01/runtime/invocation/{}/error", self.request_id);
let uri = Uri::from_str(&uri)?;
let body = serde_json::to_vec(&self.diagnostic)?;
let body = Body::from(body);
let req = Request::builder()
.method(Method::POST)
.uri(uri)
.header("lambda-runtime-function-error-type", "unhandled")
.body(body)?;
Ok(req)
}
}
#[test]
fn test_event_error_request() {
let req = EventErrorRequest {
request_id: "id",
diagnostic: Diagnostic {
error_type: "InvalidEventDataError".to_string(),
error_message: "Error parsing event data".to_string(),
},
};
let req = req.into_req().unwrap();
let expected = Uri::from_static("/2018-06-01/runtime/invocation/id/error");
assert_eq!(req.method(), Method::POST);
assert_eq!(req.uri(), &expected);
}
// /runtime/init/error
struct InitErrorRequest;
impl IntoRequest for InitErrorRequest {
fn into_req(self) -> Result<Request<Body>, Error> {
let uri = "/2018-06-01/runtime/init/error".to_string();
let uri = Uri::from_str(&uri)?;
let req = Request::builder()
.method(Method::POST)
.uri(uri)
.header("lambda-runtime-function-error-type", "unhandled")
.body(Body::empty())?;
Ok(req)
}
}
#[test]
fn test_init_error_request() {
let req = InitErrorRequest;
let req = req.into_req().unwrap();
let expected = Uri::from_static("/2018-06-01/runtime/init/error");
assert_eq!(req.method(), Method::POST);
assert_eq!(req.uri(), &expected);
}