-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
executable file
·77 lines (57 loc) · 2.33 KB
/
handler.py
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
from evaluation_function_utils.errors import EvaluationException
from .tools import commands, docs, validate
from .tools.parse import ParseError
from .tools.utils import ErrorResponse, HandlerResponse, JsonType, Response
from .tools.validate import ResBodyValidators, ValidationError
def handle_command(event: JsonType, command: str) -> HandlerResponse:
"""Switch case for handling different command options.
Args:
event (JsonType): The AWS Lambda event recieved by the handler.
command (str): The name of the function to invoke.
Returns:
HandlerResponse: The response object returned by the handler.
"""
# No validation of the doc commands.
if command in ("docs-dev", "docs"):
return docs.dev()
elif command == "docs-user":
return docs.user()
elif command in ("eval", "grade"):
response = commands.evaluate(event)
validator = ResBodyValidators.EVALUATION
elif command == "preview":
response = commands.preview(event)
validator = ResBodyValidators.PREVIEW
elif command == "healthcheck":
response = commands.healthcheck()
validator = ResBodyValidators.HEALTHCHECK
else:
response = Response(
error=ErrorResponse(message=f"Unknown command '{command}'.")
)
validator = ResBodyValidators.EVALUATION
validate.body(response, validator)
return response
def handler(event: JsonType, _: JsonType = {}) -> HandlerResponse:
"""Main function invoked by AWS Lambda to handle incoming requests.
Args:
event (JsonType): The AWS Lambda event received by the gateway.
Returns:
HandlerResponse: The response to return back to the requestor.
"""
headers = event.get("headers", dict())
command = headers.get("command", "eval")
try:
return handle_command(event, command)
except (ParseError, ValidationError) as e:
error = ErrorResponse(message=e.message, detail=e.error_thrown)
except EvaluationException as e:
error = e.error_dict
# Catch-all for any unexpected errors.
except Exception as e:
error = ErrorResponse(
message="An exception was raised while "
"executing the evaluation function.",
detail=(str(e) if str(e) != "" else repr(e)),
)
return Response(error=error)