Skip to content

Commit

Permalink
feat(type-safe-api): faster code generation for python lambda handlers (
Browse files Browse the repository at this point in the history
#850)

Move to new codegen for python lambda handlers.

Again, copy the same templates as a starting point for async python handlers for when support is
added.
  • Loading branch information
cogwirrel authored Oct 9, 2024
1 parent 96946c2 commit df2b69d
Show file tree
Hide file tree
Showing 20 changed files with 242 additions and 476 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<%_ services.forEach((service) => { _%>
<%_ service.operations.forEach((operation) => { _%>
<%_ if (operation.vendorExtensions && operation.vendorExtensions['x-handler'] && operation.vendorExtensions['x-handler'].language === 'python') { _%>
###TSAPI_WRITE_FILE###
{
"id": "<%- operation.name %>",
"dir": "<%- metadata.srcDir || 'src' %>",
"name": "<%- operation.operationIdSnakeCase %>",
"ext": ".py",
"overwrite": false
}
###/TSAPI_WRITE_FILE###from <%- metadata.runtimeModuleName %>.models import *
from <%- metadata.runtimeModuleName %>.response import Response
from <%- metadata.runtimeModuleName %>.interceptors import INTERCEPTORS
from <%- metadata.runtimeModuleName %>.interceptors.powertools.logger import LoggingInterceptor
from <%- metadata.runtimeModuleName %>.api.operation_config import (
<%- operation.operationIdSnakeCase %>_handler, <%- operation.operationIdPascalCase %>Request, <%- operation.operationIdPascalCase %>OperationResponses
)
def <%- operation.operationIdSnakeCase %>(input: <%- operation.operationIdPascalCase %>Request, **kwargs) -> <%- operation.operationIdPascalCase %>OperationResponses:
"""
Type-safe handler for the <%- operation.operationIdPascalCase %> operation
"""
LoggingInterceptor.get_logger(input).info("Start <%- operation.operationIdPascalCase %> Operation")
# TODO: Implement <%- operation.operationIdPascalCase %> Operation. `input` contains the request input
return Response.internal_failure(InternalFailureErrorResponseContent(
message="Not Implemented!"
))
# Entry point for the AWS Lambda handler for the <%- operation.operationIdPascalCase %> operation.
# The <%- operation.operationIdSnakeCase %>_handler method wraps the type-safe handler and manages marshalling inputs and outputs
handler = <%- operation.operationIdSnakeCase %>_handler(interceptors=INTERCEPTORS)(<%- operation.operationIdSnakeCase %>)
<%_ } _%>
<%_ }); _%>
<%_ }); _%>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
###TSAPI_WRITE_FILE###
{
"dir": "<%- metadata.tstDir || 'tst' %>",
"name": "__init__",
"ext": ".py",
"overwrite": false
}
###/TSAPI_WRITE_FILE####
<%_ services.forEach((service) => { _%>
<%_ service.operations.forEach((operation) => { _%>
<%_ if (operation.vendorExtensions && operation.vendorExtensions['x-handler'] && operation.vendorExtensions['x-handler'].language === 'python') { _%>
###TSAPI_WRITE_FILE###
{
"id": "test_<%- operation.operationIdSnakeCase %>",
"dir": "<%- metadata.tstDir || 'tst' %>",
"name": "test_<%- operation.operationIdSnakeCase %>",
"ext": ".py",
"overwrite": false,
"generateConditionallyId": "<%- operation.name %>"
}
###/TSAPI_WRITE_FILE###import pytest
from aws_lambda_powertools import Logger
from <%- metadata.moduleName %>.<%- operation.operationIdSnakeCase %> import <%- operation.operationIdSnakeCase %>
from <%- metadata.runtimeModuleName %>.api.operation_config import (
<%- operation.operationIdPascalCase %>Request, <%- operation.operationIdPascalCase %>RequestParameters, <%- operation.operationIdPascalCase %>RequestBody
)
@pytest.fixture
def request_arguments():
"""
Fixture for constructing common request arguments
"""
return {
"event": {},
"context": None,
"interceptor_context": {
"logger": Logger(),
},
}
def test_<%- operation.operationIdSnakeCase %>_should_return_not_implemented_error(request_arguments):
# TODO: Update the test as appropriate when you implement your handler
response = <%- operation.operationIdSnakeCase %>(<%- operation.operationIdPascalCase %>Request(
**request_arguments,
# request_parameters=<%- operation.operationIdPascalCase %>RequestParameters(
# # Add request parameters here...
# ),
request_parameters=None,
<%_ if (operation.parametersBody) { %>
# body=<%- operation.operationIdPascalCase %>RequestBody(
# # Add body fields here...
# )
<%_ } _%>
body=None,
))
assert response.status_code == 500
assert response.body.message == "Not Implemented!"
<%_ } _%>
<%_ }); _%>
<%_ }); _%>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<%_ services.forEach((service) => { _%>
<%_ service.operations.forEach((operation) => { _%>
<%_ if (operation.vendorExtensions && operation.vendorExtensions['x-handler'] && operation.vendorExtensions['x-handler'].language === 'python') { _%>
###TSAPI_WRITE_FILE###
{
"id": "<%- operation.name %>",
"dir": "<%- metadata.srcDir || 'src' %>",
"name": "<%- operation.operationIdSnakeCase %>",
"ext": ".py",
"overwrite": false
}
###/TSAPI_WRITE_FILE###from <%- metadata.runtimeModuleName %>.models import *
from <%- metadata.runtimeModuleName %>.response import Response
from <%- metadata.runtimeModuleName %>.interceptors import INTERCEPTORS
from <%- metadata.runtimeModuleName %>.interceptors.powertools.logger import LoggingInterceptor
from <%- metadata.runtimeModuleName %>.api.operation_config import (
<%- operation.operationIdSnakeCase %>_handler, <%- operation.operationIdPascalCase %>Request, <%- operation.operationIdPascalCase %>OperationResponses
)
def <%- operation.operationIdSnakeCase %>(input: <%- operation.operationIdPascalCase %>Request, **kwargs) -> <%- operation.operationIdPascalCase %>OperationResponses:
"""
Type-safe handler for the <%- operation.operationIdPascalCase %> operation
"""
LoggingInterceptor.get_logger(input).info("Start <%- operation.operationIdPascalCase %> Operation")
# TODO: Implement <%- operation.operationIdPascalCase %> Operation. `input` contains the request input
return Response.internal_failure(InternalFailureErrorResponseContent(
message="Not Implemented!"
))
# Entry point for the AWS Lambda handler for the <%- operation.operationIdPascalCase %> operation.
# The <%- operation.operationIdSnakeCase %>_handler method wraps the type-safe handler and manages marshalling inputs and outputs
handler = <%- operation.operationIdSnakeCase %>_handler(interceptors=INTERCEPTORS)(<%- operation.operationIdSnakeCase %>)
<%_ } _%>
<%_ }); _%>
<%_ }); _%>

This file was deleted.

Loading

0 comments on commit df2b69d

Please sign in to comment.