-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(type-safe-api): faster code generation for python lambda handlers (
#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
Showing
20 changed files
with
242 additions
and
476 deletions.
There are no files selected for viewing
7 changes: 0 additions & 7 deletions
7
...s/type-safe-api/scripts/type-safe-api/generators/python-async-lambda-handlers/config.yaml
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
...-api/scripts/type-safe-api/generators/python-async-lambda-handlers/templates/handlers.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %>) | ||
<%_ } _%> | ||
<%_ }); _%> | ||
<%_ }); _%> |
47 changes: 0 additions & 47 deletions
47
...ripts/type-safe-api/generators/python-async-lambda-handlers/templates/handlers.handlebars
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
...afe-api/scripts/type-safe-api/generators/python-async-lambda-handlers/templates/tests.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
<%_ } _%> | ||
<%_ }); _%> | ||
<%_ }); _%> |
72 changes: 0 additions & 72 deletions
72
.../scripts/type-safe-api/generators/python-async-lambda-handlers/templates/tests.handlebars
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
packages/type-safe-api/scripts/type-safe-api/generators/python-lambda-handlers/config.yaml
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
...e-safe-api/scripts/type-safe-api/generators/python-lambda-handlers/templates/handlers.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %>) | ||
<%_ } _%> | ||
<%_ }); _%> | ||
<%_ }); _%> |
47 changes: 0 additions & 47 deletions
47
...api/scripts/type-safe-api/generators/python-lambda-handlers/templates/handlers.handlebars
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.