38
38
if self.method == 'GET':
39
39
return self.do_GET()
41
def send_response(self, code, headers={}, reason=None):
41
def send_response(self, code, response='', headers={}, reason=None):
43
43
reason = httplib.responses[code]
44
44
response_code = '%s %s' % (code, reason)
45
45
response_headers = {'content-type': 'application/json'}
46
46
response_headers.update(headers.items())
47
47
self.start_response(response_code, response_headers.items())
50
51
"""Create an archive or message on PUT."""
52
53
# This expected path is /archive/archive_id.
54
55
self.store.put_archive(self.path[0])
55
self.send_response(httplib.CREATED)
56
return self.send_response(httplib.CREATED)
56
57
except Exception, error:
58
return self.send_response(
58
59
httplib.BAD_REQUEST, reason=error.__doc__)
59
60
elif len(self.path) == 2:
60
61
# This expected path is /archive/archive_id/message_id.
62
63
put_input = self.environ['wsgi.input']
63
64
message = put_input.read(int(self.environ['CONTENT_LENGTH']))
64
65
self.store.put_message(self.path[0], self.path[1], message)
65
self.send_response(httplib.CREATED)
66
return self.send_response(httplib.CREATED)
66
67
except Exception, error:
68
return self.send_response(
68
69
httplib.BAD_REQUEST, reason=error.__doc__)
72
72
"""Change a message on POST."""
76
76
# This expected path is /archive/archive_id/message_id.
77
77
response = self.store.hide_message(
78
78
self.path[0], self.path[1], self.query_string)
79
self.send_response(httplib.OK)
80
return [simplejson.dumps(response)]
79
response = simplejson.dumps(response)
80
return self.send_response(httplib.OK, response=response)
81
81
except Exception, error:
82
self.send_response(httplib.BAD_REQUEST, reason=error.__doc__)
82
return self.send_response(
83
httplib.BAD_REQUEST, reason=error.__doc__)
86
86
"""Retrieve a list of messages on GET."""
88
88
response = self.store.get_messages(
89
89
self.path[0], self.query_string)
90
self.send_response(httplib.OK)
91
return [simplejson.dumps(response)]
90
response = simplejson.dumps(response)
91
return self.send_response(httplib.OK, response=response)
92
92
except Exception, error:
93
self.send_response(httplib.BAD_REQUEST, reason=error.__doc__)
93
return self.send_response(
94
httplib.BAD_REQUEST, reason=error.__doc__)
96
96
def log_message(self, format, *args):
97
97
"""Override log_message to use standard Python logging."""