Archived
1

Serialize request

This commit is contained in:
Jake Howard 2017-04-11 14:19:36 +01:00
parent 44bba45c7b
commit c0309e47b5
3 changed files with 24 additions and 2 deletions

20
lantern/request.py Normal file
View File

@ -0,0 +1,20 @@
from sanic.exceptions import InvalidUsage
SERIALIZE_ATTRS = [
'args',
'form',
'url',
'ip',
'query_string'
]
def serialize_request(request):
serialized = {key: getattr(request, key) for key in SERIALIZE_ATTRS}
try:
serialized['json'] = request.json
except InvalidUsage: # if there's no body, we cant add it
pass
serialized['user_agent'] = request['user_agent'].to_dict()
return serialized

View File

@ -1,12 +1,13 @@
from sanic.response import html
from lantern.handle import execute_handle
from lantern.error import error_response
from lantern.request import serialize_request
import os
async def main_route(request, path=None):
handle = os.path.join(os.path.dirname(__file__), 'test_handle.py')
result = execute_handle(handle, {})
result = execute_handle(handle, serialize_request(request))
if result.exit_code != 0:
return error_response(result.error)

View File

@ -2,4 +2,5 @@
# Test handler
print("<h1>Test</h1>")
import sys
print(sys.stdin.read())