Serialize request
This commit is contained in:
parent
44bba45c7b
commit
c0309e47b5
3 changed files with 24 additions and 2 deletions
20
lantern/request.py
Normal file
20
lantern/request.py
Normal 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
|
|
@ -1,12 +1,13 @@
|
||||||
from sanic.response import html
|
from sanic.response import html
|
||||||
from lantern.handle import execute_handle
|
from lantern.handle import execute_handle
|
||||||
from lantern.error import error_response
|
from lantern.error import error_response
|
||||||
|
from lantern.request import serialize_request
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
async def main_route(request, path=None):
|
async def main_route(request, path=None):
|
||||||
handle = os.path.join(os.path.dirname(__file__), 'test_handle.py')
|
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:
|
if result.exit_code != 0:
|
||||||
return error_response(result.error)
|
return error_response(result.error)
|
||||||
|
|
||||||
|
|
|
@ -2,4 +2,5 @@
|
||||||
|
|
||||||
# Test handler
|
# Test handler
|
||||||
|
|
||||||
print("<h1>Test</h1>")
|
import sys
|
||||||
|
print(sys.stdin.read())
|
||||||
|
|
Reference in a new issue