Skip to content

Commit f3fc371

Browse files
authored
Merge pull request #18 from lambda-feedback/feature/muEd-versioning
Feature/mu ed versioning
2 parents eb63c76 + 1a027b5 commit f3fc371

5 files changed

Lines changed: 323 additions & 107 deletions

File tree

handler.py

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import json
12
from evaluation_function_utils.errors import EvaluationException
23

34
from .tools import commands, docs, parse, validate
45
from .tools.parse import ParseError
5-
from .tools.utils import ErrorResponse, HandlerResponse, JsonType, Response
6+
from typing import Any, Optional
7+
8+
from .tools.utils import DocsResponse, ErrorResponse, HandlerResponse, JsonType, Response
69
from .tools.validate import (
710
LegacyReqBodyValidators,
811
LegacyResBodyValidators,
@@ -58,6 +61,57 @@ def handle_legacy_command(event: JsonType, command: str) -> HandlerResponse:
5861
return response
5962

6063

64+
def wrap_muEd_response(body: Any, event: JsonType, status_code: int = 200) -> DocsResponse:
65+
"""Wrap a muEd response body in Lambda proxy format with X-Api-Version header.
66+
67+
Args:
68+
body: The response body to serialise.
69+
event (JsonType): The incoming event (used to resolve the served version).
70+
status_code (int): The HTTP status code. Defaults to 200.
71+
72+
Returns:
73+
DocsResponse: Proxy-format response with X-Api-Version header set.
74+
"""
75+
requested = (event.get("headers") or {}).get("X-Api-Version")
76+
if requested and requested in commands.SUPPORTED_MUED_VERSIONS:
77+
version = requested
78+
else:
79+
version = commands.SUPPORTED_MUED_VERSIONS[-1]
80+
return DocsResponse(
81+
statusCode=status_code,
82+
headers={"X-Api-Version": version},
83+
body=json.dumps(body),
84+
isBase64Encoded=False,
85+
)
86+
87+
88+
def check_muEd_version(event: JsonType) -> Optional[HandlerResponse]:
89+
"""Check the X-Api-Version header against supported muEd versions.
90+
91+
Args:
92+
event (JsonType): The AWS Lambda event received by the handler.
93+
94+
Returns:
95+
Optional[HandlerResponse]: A version-not-supported error response if
96+
the requested version is unsupported, otherwise None.
97+
"""
98+
version = (event.get("headers") or {}).get("X-Api-Version")
99+
if version and version not in commands.SUPPORTED_MUED_VERSIONS:
100+
return {
101+
"title": "API version not supported",
102+
"message": (
103+
f"The requested API version '{version}' is not supported. "
104+
f"Supported versions are: {commands.SUPPORTED_MUED_VERSIONS}."
105+
),
106+
"code": "VERSION_NOT_SUPPORTED",
107+
"details": {
108+
"requestedVersion": version,
109+
"supportedVersions": commands.SUPPORTED_MUED_VERSIONS,
110+
},
111+
}
112+
return None
113+
114+
61115
def handle_muEd_command(event: JsonType, command: str) -> HandlerResponse:
62116
"""Switch case for handling different command options using muEd schemas.
63117
@@ -68,21 +122,26 @@ def handle_muEd_command(event: JsonType, command: str) -> HandlerResponse:
68122
Returns:
69123
HandlerResponse: The response object returned by the handler.
70124
"""
125+
version_error = check_muEd_version(event)
126+
if version_error:
127+
return wrap_muEd_response(version_error, event, 406)
128+
71129
if command == "eval":
72130
body = parse.body(event)
73131
validate.body(body, MuEdReqBodyValidators.EVALUATION)
74132
response = commands.evaluate_muEd(body)
75133
validate.body(response, MuEdResBodyValidators.EVALUATION)
76134

77135
elif command == "healthcheck":
78-
response = commands.healthcheck()
136+
response = commands.healthcheck_muEd()
137+
validate.body(response, MuEdResBodyValidators.HEALTHCHECK)
79138

80139
else:
81140
response = Response(
82141
error=ErrorResponse(message=f"Unknown command '{command}'.")
83142
)
84143

85-
return response
144+
return wrap_muEd_response(response, event)
86145

87146

88147
def handler(event: JsonType, _=None) -> HandlerResponse:

0 commit comments

Comments
 (0)