[fix] handle cases with b64encoded body, add test payload
Browse files- events/payloads_body_base64.json +52 -0
- src/utilities/utilities.py +10 -7
events/payloads_body_base64.json
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"version": "2.0",
|
3 |
+
"routeKey": "POST /prediction",
|
4 |
+
"rawPath": "/localhost/prediction",
|
5 |
+
"rawQueryString": "",
|
6 |
+
"headers": {
|
7 |
+
"accept": "application/json, text/plain, */*",
|
8 |
+
"accept-encoding": "gzip, deflate, br",
|
9 |
+
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
|
10 |
+
"authorization": "Bearer ey...",
|
11 |
+
"content-length": "126",
|
12 |
+
"content-type": "application/json",
|
13 |
+
"host": "xxx.execute-api.eu-west-1.amazonaws.com",
|
14 |
+
"origin": "http://localhost:3000",
|
15 |
+
"referer": "http://localhost:3000/",
|
16 |
+
"sec-fetch-dest": "empty",
|
17 |
+
"sec-fetch-mode": "cors",
|
18 |
+
"sec-fetch-site": "cross-site",
|
19 |
+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
|
20 |
+
"x-amzn-trace-id": "Root=1-xxx-aaaa",
|
21 |
+
"x-forwarded-for": "1.1.1.1",
|
22 |
+
"x-forwarded-port": "443",
|
23 |
+
"x-forwarded-proto": "https"
|
24 |
+
},
|
25 |
+
"requestContext": {
|
26 |
+
"accountId": "123",
|
27 |
+
"apiId": "api-id-aws",
|
28 |
+
"authorizer": {
|
29 |
+
"jwt": {
|
30 |
+
"claims": {
|
31 |
+
"scope": "openid profile email"
|
32 |
+
},
|
33 |
+
"scopes": null
|
34 |
+
}
|
35 |
+
},
|
36 |
+
"domainName": "xxx.execute-api.eu-west-1.amazonaws.com",
|
37 |
+
"domainPrefix": "xxx",
|
38 |
+
"http": {
|
39 |
+
"method": "POST",
|
40 |
+
"path": "/localhost/prediction",
|
41 |
+
"protocol": "HTTP/1.1",
|
42 |
+
"sourceIp": "52.143.157.36",
|
43 |
+
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
|
44 |
+
},
|
45 |
+
"requestId": "aaa-bbbb=",
|
46 |
+
"routeKey": "POST /prediction",
|
47 |
+
"stage": "localhost",
|
48 |
+
"time": "21/May/2023:19:32:35 +0000",
|
49 |
+
"timeEpoch": 1684697555723
|
50 |
+
},
|
51 |
+
"body": "eyJiYm94IjogWzEsIDIsIDMsIDRdLCAicG9pbnRzIjogW1s1LCA2XV19"
|
52 |
+
}
|
src/utilities/utilities.py
CHANGED
@@ -1,16 +1,19 @@
|
|
1 |
"""Various utilities (logger, time benchmark, args dump, numerical and stats info)"""
|
2 |
-
import loguru
|
3 |
|
4 |
-
from src.utilities.constants import ROOT
|
5 |
|
6 |
-
|
7 |
-
def is_base64(s):
|
8 |
import base64
|
9 |
|
10 |
try:
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
return False
|
15 |
|
16 |
|
|
|
1 |
"""Various utilities (logger, time benchmark, args dump, numerical and stats info)"""
|
|
|
2 |
|
|
|
3 |
|
4 |
+
def is_base64(sb):
|
|
|
5 |
import base64
|
6 |
|
7 |
try:
|
8 |
+
if isinstance(sb, str):
|
9 |
+
# If there's any unicode here, an exception will be thrown and the function will return false
|
10 |
+
sb_bytes = bytes(sb, 'ascii')
|
11 |
+
elif isinstance(sb, bytes):
|
12 |
+
sb_bytes = sb
|
13 |
+
else:
|
14 |
+
raise ValueError("Argument must be string or bytes")
|
15 |
+
return base64.b64encode(base64.b64decode(sb_bytes, validate=True)) == sb_bytes
|
16 |
+
except ValueError:
|
17 |
return False
|
18 |
|
19 |
|