/
response.py
49 lines (43 loc) · 1.76 KB
/
response.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import json
import h11
from . import _request_local, log
# Helper function
async def respond(status_code, content_type, body):
log.info(f"Sending {status_code} response with {len(body)} bytes")
headers = _request_local.transport.basic_headers()
headers.append(("Content-Type", content_type))
headers.append(("Content-Length", str(len(body))))
res = h11.Response(status_code=status_code, headers=headers)
await _request_local.transport.send(res)
await _request_local.transport.send(h11.Data(data=body))
await _request_local.transport.send(h11.EndOfMessage())
class Json:
pass
JSON_CONTENT_TYPE = "application/json; charset=utf-8"
TEXT_CONTENT_TYPE = "text/plain; charset=utf-8"
BINARY_CONTENT_TYPE = "application/octet-stream"
def response_to_bytes(handler, response):
# TODO flesh out with other types such as html, css, javascript
# media files, etc
ann = handler.__annotations__
if "return" in ann:
if issubclass(ann["return"], Json):
content_type = JSON_CONTENT_TYPE
content = json.dumps(response).encode("utf-8")
elif issubclass(ann["return"], (bytes, bytearray)):
content_type = BINARY_CONTENT_TYPE
content = response
else:
content_type = TEXT_CONTENT_TYPE
content = response.encode("utf-8")
else:
if isinstance(response, (dict, tuple, list, int, float)):
content_type = JSON_CONTENT_TYPE
content = json.dumps(response).encode("utf-8")
elif isinstance(response, (bytes, bytearray)):
content_type = BINARY_CONTENT_TYPE
content = response
else:
content_type = TEXT_CONTENT_TYPE
content = response.encode("utf-8")
return content_type, content