API¶
ApiGateway¶
-
class
AwAws.Api.api.
ApiGateway
¶
Typically, when processing requests from ApiGateway, both the Event and Response calsses are required. This class initiallizes and exposes both of those classes for easier use. The Event class is used to prarse data from the incoming request from Api Gateway while the Response class offers a bunch of pre-canned responses that are formatted correctly for ApiGateway.
Typical usage in a Lambda:
def handle(self, event, context):
api = ApiGateway()
api.event.parse_event(event)
# now api.event contains all of the event's properties
if api.event.method != 'PUT'
return api.response.error_4XX('PUT method required')
params = event.params # a dictionary of incoming parameters
path = event.path # the incoming path for this request
body = event.body # incoming data
# -- do your thing here ---
# create a response
response_body = {'all_good': 'some data to return'}
response = api.response.ok_200(body=response_body)
return response
Note
Should also include a Context
class
Event¶
Used from ApiGateway class
-
class
AwAws.Api.event.
Event
¶ - Attributes:
event: (dict), None - the raw incoming event
method: (string), ‘ANY’ - http method
params: (dict), {} - qstring params
path: (string), None - incoming path
body: (string), {} - incoming data
- Raises
AwAwsInvalidHttpMethod, AwAwsMissingRequirement
-
HTTP_METHODS
= ['ANY', 'DELETE', 'GET', 'PATCH', 'POST', 'PUT']¶ Allowed HTTP Methods
-
parse_event
(event)¶ grab the api gateway event and parse out the good bits
-
set_body
()¶ grab the body section from the incoming event (if it exists)
-
set_event
(event)¶ load the incoming event
-
set_http_method
()¶ get the http method on the incoming event
-
set_path_params
()¶ get the path parameters from the event
-
set_qstring_params
()¶ get the query string params from the event
Response¶
Used from ApiGateway class
-
class
AwAws.Api.response.
Response
¶ -
error_4XX
(message=None)¶ send and error code (in the 400 range) - default to 422
-
error_5XX
(message=None)¶ send a system error code (in the 500 range)
-
ok_200
(body=None)¶ Return a 200 status, set body if exists - can be type dict or json
-
set_body
(body)¶ Set the body of the response message
-
set_header
(key, value=None)¶ Set the a header on this response
-
set_status_code
(status_code)¶ Only used if you need to override the status code
-