-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.py
45 lines (33 loc) · 1.17 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
import json
from twisted.web.resource import Resource
def _with_json_headers(request, response):
request.setHeader('Content-Type', 'application/json')
request.setHeader('Content-Length', len(response))
return response
class Ok(Resource):
def render(self, request):
response = json.dumps(
{'status': 'ok',
'code': 0}) + '\n'
return _with_json_headers(request, response)
class Data(Resource):
def __init__(self, data):
self._data = data
def render(self, request):
request.setHeader('Content-Type', 'application/octet-stream')
request.setHeader('Content-Length', len(self._data))
return self._data
class Closed(Resource):
def render(self, request):
response = json.dumps(
{'status': 'ok',
'code': -1,
'message': 'channel closed'}) + '\n'
return _with_json_headers(request, response)
class Timeout(Resource):
def render(self, request):
response = json.dumps(
{'status': 'error',
'code': 2,
'message': 'request timed out'}) + '\n'
return _with_json_headers(request, response)