Skip to content

Commit

Permalink
avoid keyerrors by using the get function on an object
Browse files Browse the repository at this point in the history
  • Loading branch information
jente-peeraer-cloudway authored and Jochen Van de Velde committed Jul 8, 2019
1 parent 1608d00 commit beff8a0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ ENV/

# Rope project settings
.ropeproject

.idea
19 changes: 10 additions & 9 deletions flask_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from io import StringIO

from flask import Request
from util import get_nested


__version__ = '0.1.2'
Expand All @@ -50,28 +51,28 @@ def make_environ(event, context):
http_hdr_name = 'HTTP_{}'.format(hdr_name)
environ[http_hdr_name] = hdr_value

qs = event['queryStringParameters']
qs = event.get('queryStringParameters', '')

environ['REQUEST_METHOD'] = event['httpMethod']
environ['PATH_INFO'] = event['path']
environ['REQUEST_METHOD'] = event.get('httpMethod', '')
environ['PATH_INFO'] = event.get('path', '')
environ['QUERY_STRING'] = urlencode(qs) if qs else ''
environ['REMOTE_ADDR'] = event['requestContext']['identity']['sourceIp']
environ['REMOTE_ADDR'] = get_nested(event, '', 'requestContext', 'identity', 'sourceIp')
environ['HOST'] = '{}:{}'.format(
environ.get('HTTP_HOST', ''),
environ.get('HTTP_X_FORWARDED_PORT', ''),
)
environ['SCRIPT_NAME'] = ''
environ['SERVER_NAME'] = 'SERVER_NAME'

environ['SERVER_PORT'] = environ['HTTP_X_FORWARDED_PORT']
environ['SERVER_PORT'] = environ.get('HTTP_X_FORWARDED_PORT', '')
environ['SERVER_PROTOCOL'] = 'HTTP/1.1'

environ['CONTENT_LENGTH'] = str(
len(event['body']) if event['body'] else ''
len(event.get('body', ''))
)

environ['wsgi.url_scheme'] = environ['HTTP_X_FORWARDED_PROTO']
environ['wsgi.input'] = StringIO(event['body'] or '')
environ['wsgi.url_scheme'] = environ.get('HTTP_X_FORWARDED_PROTO', '')
environ['wsgi.input'] = StringIO(event.get('body', ''))
environ['wsgi.version'] = (1, 0)
environ['wsgi.errors'] = sys.stderr
environ['wsgi.multithread'] = False
Expand Down Expand Up @@ -140,5 +141,5 @@ def __call__(self, event, context):
return {
'statusCode': 500,
'headers': {},
'body': 'internal server error'
'body': 'Internal Server Error'
}
1 change: 1 addition & 0 deletions util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from object_utils import get_nested
9 changes: 9 additions & 0 deletions util/object_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def get_nested(o, default, *args):
if o is None:
return default
current = o
for arg in args:
if current is None or arg is None or current.get(arg, None) is None:
return default
current = current.get(arg, default)
return current

0 comments on commit beff8a0

Please sign in to comment.