-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.py
36 lines (28 loc) · 1.21 KB
/
http.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
import webapp2
import json
import hashlib
# encode floats to 4 places
json.encoder.FLOAT_REPR = lambda f: ("%.4f" % f)
json.encoder.c_make_encoder = None
class RestHandler(webapp2.RequestHandler):
def html_response(self, payload):
self.response.headers['Content-Type'] = 'text/html'
self.response.write(payload)
def png_response(self, payload):
self.response.headers['Content-Type'] = 'image/png'
self.response.write(payload)
def text_response(self, payload):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(payload)
def js_response(self, payload):
self.response.headers['Content-Type'] = 'application/javascript'
self.response.write(payload)
def json_response(self, obj):
payload = json.dumps(obj,separators=(',', ':'))
self.response.headers['Content-Type'] = 'application/json'
self.response.write(payload)
def response_error(self):
self.response.headers['Cache-Control'] = 'public,max-age=0'
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json.dumps({"error":"not_found"}))
self.response.set_status(404)