-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
27 lines (24 loc) · 892 Bytes
/
handler.py
File metadata and controls
27 lines (24 loc) · 892 Bytes
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
# file: handler.py (~30 lines)
import json, os, boto3
DDB = boto3.resource("dynamodb")
RECS = DDB.Table(os.environ["RECOMMEND_TABLE"])
META = DDB.Table(os.environ["META_TABLE"])
def lambda_handler(event, _ctx):
q = event.get("queryStringParameters") or {}
cid = q.get("course")
k = int(q.get("k", "5"))
if not cid:
return {"statusCode": 400,
"body": "missing ?course="}
entry = RECS.get_item(Key={"course_id": cid}).get("Item")
if not entry:
return {"statusCode": 404, "body": "course not found"}
out = []
for c in entry["neighbors"][:k]:
meta = META.get_item(Key={"course_id": c}).get("Item")
if meta:
meta.pop("course_id", None)
out.append(meta)
return {"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(out)}