Wrappers for using a single lambda function for AWS ApiGateway
Project description
sfunc
Single lambda function for AWS Api Gateway
This is a very simple package which adds a few decorators to simplify the use of a single lambda function for multiple
endpoints for AWS Api Gateway. It uses the operationId
attribute in the OAS3 specification to identify the correct
handler and then passes the event and context to the correct method.
Basic usage
import json
from sfunc import SFunc
sfunc = SFunc()
@sfunc.operation('getPetsByName')
def get_pets_by_name(event, context):
"""The methods need to have the same signature as a regular lambda handler entrypoint"""
return {
'statusCode': 200,
'body': json.dumps({'names': ['fido', 'catty']})
}
@sfunc.operation('getPetsByAge')
def get_pets_by_age(event, context):
"""This will be invoked when the endpoint with the operationId 'getPetsByAge' is invoked"""
return {
'statusCode': 204
}
def lambda_handler(event, context):
"""We only need to specify one entry point and can ignore any other """
rsp = sfunc.execute(event, context) # rsp will contain the return value from the invoked method
return rsp
Using sfunc classes
For request/response which doesn't require any additional and are json encoded, sfunc provides two convenience classes for request and response which can simplify the code further when it comes to parsing and getting headers and path params. The following code will result in the same outcome as the example above, except for the extra headers.
from sfunc import ApiGatewayRequest, ApiGatewayResponse, SFunc
sfunc = SFunc()
@sfunc.operation('getPetsByName')
def pets_by_name(req: ApiGatewayRequest, context) -> ApiGatewayResponse:
_ = req.headers().get('x-request-id')
return ApiGatewayResponse(200, {'names': ['fido', 'catty']})
@sfunc.operation('getPetsByAge')
def pets_by_age(req: ApiGatewayRequest, context) -> ApiGatewayResponse:
rsp = ApiGatewayResponse(204)
rsp.add_header('content-type', 'application/json')
return rsp
def lambda_handler(event, context):
return sfunc.sfunc_execute(ApiGatewayRequest(event), context).response()
Specifying default handlers
It might be useful during development or if some endpoints are missing the operationId attribute in the specification.
from sfunc import SFunc
sfunc = SFunc()
@sfunc.default()
def default_handler(event, context):
return {
'statusCode': 405
}
def lambda_handler(event, context):
return sfunc.execute(event, context) # or sfunc.sfunc_execute(ApiGatewayRequest) -> ApiGatewayResponse as above
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sfunc-0.3.2.tar.gz
.
File metadata
- Download URL: sfunc-0.3.2.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d8c342f06ef51ccdf67ef260d8f5210bc45a93d493ca92c4273af351425ac61 |
|
MD5 | 2c5c79c90ddca0819921f3e631fac997 |
|
BLAKE2b-256 | 4039903c114d362c0fa0c329848785a708f73cbf493c15e0e64467d279f4db89 |
File details
Details for the file sfunc-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: sfunc-0.3.2-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a3542555bf045110b09159dc002e57836235f926167c0269e2c7fc39111ae9b |
|
MD5 | 9521754e301eb9c83658d2a088312e26 |
|
BLAKE2b-256 | 3d18a76fd19444aafc56e42a0756a92fc37f6422d6dd958e22a6c3ec15a26665 |