Created
August 16, 2017 15:14
-
-
Save dchaplinsky/31d0166f9db45c3a82f62b838aa23ba8 to your computer and use it in GitHub Desktop.
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
--- ../venv/lib/python3.6/site-packages/flask_lambda.py 2017-07-04 02:51:23.000000000 +0300 | |
+++ lambdas/venues_finder/venues_finder/lambdify.py 2017-07-10 16:49:41.000000000 +0300 | |
@@ -31,7 +31,7 @@ | |
except ImportError: | |
from io import StringIO | |
-from werkzeug.wrappers import BaseRequest | |
+from werkzeug.wrappers import BaseRequest, Response | |
__version__ = '0.0.4' | |
@@ -39,8 +39,9 @@ | |
def make_environ(event): | |
environ = {} | |
+ headers = event['headers'] or {} | |
- for hdr_name, hdr_value in event['headers'].items(): | |
+ for hdr_name, hdr_value in headers.items(): | |
hdr_name = hdr_name.replace('-', '_').upper() | |
if hdr_name in ['CONTENT_TYPE', 'CONTENT_LENGTH']: | |
environ[hdr_name] = hdr_value | |
@@ -55,17 +56,19 @@ | |
environ['PATH_INFO'] = event['path'] | |
environ['QUERY_STRING'] = urlencode(qs) if qs else '' | |
environ['REMOTE_ADDR'] = event['requestContext']['identity']['sourceIp'] | |
- environ['HOST'] = '%(HTTP_HOST)s:%(HTTP_X_FORWARDED_PORT)s' % environ | |
+ if "HTTP_HOST" in environ and "HTTP_X_FORWARDED_PORT" in environ: | |
+ environ['HOST'] = '%(HTTP_HOST)s:%(HTTP_X_FORWARDED_PORT)s' % environ | |
environ['SCRIPT_NAME'] = '' | |
- environ['SERVER_PORT'] = environ['HTTP_X_FORWARDED_PORT'] | |
+ environ['SERVER_PORT'] = environ.get('HTTP_X_FORWARDED_PORT', "80") | |
environ['SERVER_PROTOCOL'] = 'HTTP/1.1' | |
+ environ['SERVER_NAME'] = 'GKWF' | |
environ['CONTENT_LENGTH'] = str( | |
len(event['body']) if event['body'] else '' | |
) | |
- environ['wsgi.url_scheme'] = environ['HTTP_X_FORWARDED_PROTO'] | |
+ environ['wsgi.url_scheme'] = environ.get('HTTP_X_FORWARDED_PROTO', "http") | |
environ['wsgi.input'] = StringIO(event['body'] or '') | |
environ['wsgi.version'] = (1, 0) | |
environ['wsgi.errors'] = sys.stderr | |
@@ -78,16 +81,6 @@ | |
return environ | |
-class LambdaResponse(object): | |
- def __init__(self): | |
- self.status = None | |
- self.response_headers = None | |
- | |
- def start_response(self, status, response_headers, exc_info=None): | |
- self.status = int(status[:3]) | |
- self.response_headers = dict(response_headers) | |
- | |
- | |
class FlaskLambda(Flask): | |
def __call__(self, event, context): | |
if 'httpMethod' not in event: | |
@@ -96,15 +89,10 @@ | |
# occur via API Gateway and Lambda | |
return super(FlaskLambda, self).__call__(event, context) | |
- response = LambdaResponse() | |
- | |
- body = next(self.wsgi_app( | |
- make_environ(event), | |
- response.start_response | |
- )) | |
+ response = Response.from_app(self.wsgi_app, make_environ(event)) | |
return { | |
- 'statusCode': response.status, | |
- 'headers': response.response_headers, | |
- 'body': body | |
+ 'statusCode': response.status_code, | |
+ 'headers': dict(response.headers), | |
+ 'body': response.get_data(as_text=True) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment