Package: lamper
Lambda Helper to Api Gateway Lambda IntegrationThis lib provide a way to organize aws lambda integration with aws api gateway
using Models, Services and Controllers.Get It Now$pipinstalllamperimportrequestsfrommarshmallowimportSchema,fieldsfromlamperimportcorefromlamperimportdecoratorsfromlamper.exceptionsimportHttpBaseExceptionfromlamper.httpsimportHttpResponsefromlamper.configimportget_loggerlogger=get_logger(__name__,level='DEBUG')#MODELSclassExampleQueryString(Schema):idFromApiGateway=fields.Str(required=True,attribute="id")classExampleResponse(Schema):one_changed=fields.Str(attribute="one")key_changed=fields.Str(attribute="key")#SERVICEdefexample_service(event):returncore.callapi(querystring_schema=ExampleQueryString,response_schema=ExampleResponse,http_method=core.HttpMethod.GET,api_url='http://echo.jsontest.com/key/value/one/two',event=event)# ROUTESroutes=decorators.Mapping()@routes.get('/example')defmy_example(event,context):try:response=example_service(event)exceptHttpBaseExceptionase:returne.get_response()returnHttpResponse.success(response)deflambda_handler(event,context):returncore.registry_components(event=event,context=context,routes=routes,)#TEST LAMBDA HANDLERif__name__=='__main__':event={'resource':'/example','httpMethod':'GET','queryStringParameters':{'idFromApiGateway':'1'}}result=lambda_handler(event,None)#SUCESS RESPONSE# 2020-09-26:11:09:48,483 INFO     [core.py:72] calling url:http://echo.jsontest.com/key/value/one/two# 2020-09-26:11:09:48,483 INFO     [core.py:73] with headers:# 2020-09-26:11:09:48,483 INFO     [core.py:74] None# 2020-09-26:11:09:48,483 INFO     [core.py:75] with payload:# 2020-09-26:11:09:48,483 INFO     [core.py:77] {'id': '1'}# 2020-09-26:11:09:48,486 DEBUG    [connectionpool.py:226] Starting new HTTP connection (1): echo.jsontest.com:80# send: b'GET /key/value/one/two?id=1 HTTP/1.1\r\nHost: echo.jsontest.com\r\nUser-Agent: python-requests/2.24.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\n\r\n'# reply: 'HTTP/1.1 200 OK\r\n'# header: Access-Control-Allow-Origin: *# header: Content-Type: application/json# header: X-Cloud-Trace-Context: 6943045aaeac18beff6462204546f853# header: Date: Sat, 26 Sep 2020 14:09:48 GMT# header: Server: Google Frontend# header: Content-Length: 39# {'statusCode': 200, 'body': '{"key_changed": "value", "one_changed": "two"}'}# 2020-09-26:11:09:48,702 DEBUG    [connectionpool.py:433] http://echo.jsontest.com:80 "GET /key/value/one/two?id=1 HTTP/1.1" 200 39# 2020-09-26:11:09:48,703 INFO     [core.py:88] request : <PreparedRequest [GET]># 2020-09-26:11:09:48,703 INFO     [core.py:89] response text: {#    "one": "two",#    "key": "value"# }## 2020-09-26:11:09:48,703 INFO     [core.py:97] response:# 2020-09-26:11:09:48,704 INFO     [core.py:98] {'key_changed': 'value', 'one_changed': 'two'}# 2020-09-26:11:09:48,704 INFO     [https.py:22] response to api gateway:# 2020-09-26:11:09:48,704 INFO     [https.py:23] {'body': '{"key_changed": "value", "one_changed": "two"}', 'statusCode': 200}##TEST VALIDATESevent={'resource':'/example','httpMethod':'GET','queryStringParameters':{'idFromApiGateway':None}}result=lambda_handler(event,None)#RESPONSE# 2020-09-26:11:16:19,832 INFO     [exceptions.py:18] response to api gateway:# 2020-09-26:11:16:19,833 INFO     [exceptions.py:19] {'body': '{"idFromApiGateway": ["Field may not be null."]}', 'statusCode': 422}