Skip to content

Commit

Permalink
Dynamically select sanic response instantiation
Browse files Browse the repository at this point in the history
Switch between sanic HTTPResponse instantiation keyword arguments based
on the results of `inspect`

Resolves: miguelgrinberg#213
  • Loading branch information
jdraymon committed Jan 30, 2021
1 parent 7536563 commit f026649
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion engineio/async_drivers/sanic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import inspect
from urllib.parse import urlsplit

try: # pragma: no cover
Expand Down Expand Up @@ -94,6 +95,23 @@ def make_response(status, headers, payload, environ): # pragma: no cover
"""
headers_dict = {}
content_type = None
for h in headers:
if h[0].lower() == 'content-type':
content_type = h[1]
else:
headers_dict[h[0]] = h[1]
return HTTPResponse(body=payload, content_type=content_type,
status=int(status.split()[0]), headers=headers_dict)


def make_response_pre_20_12(status, headers, payload, environ): # pragma: no cover
"""This function generates an appropriate response object for this async
mode.
`body_bytes` keyword was removed in sanic version 20.12
"""
headers_dict = {}
content_type = None
for h in headers:
if h[0].lower() == 'content-type':
content_type = h[1]
Expand Down Expand Up @@ -134,10 +152,16 @@ async def wait(self):
return data


_make_response = make_response

if HTTPResponse and 'body_bytes' in inspect.signature(HTTPResponse):
_make_response = make_response_pre_20_12


_async = {
'asyncio': True,
'create_route': create_route,
'translate_request': translate_request,
'make_response': make_response,
'make_response': _make_response,
'websocket': WebSocket if WebSocketProtocol else None,
}

0 comments on commit f026649

Please sign in to comment.