def api_worker(self): """generic flow for API methods...""" data = None status = None content = None try: data = self._data() except MalformedRequest: status = 403 content = { 'error': 'malformed request' } except interpretation.BunkInterpretation, error: status = 400 content = { 'error': str(error) } except comment.BunkComment, error: status = 400 content = { 'error': str(error) } except interpretation.BadOwnerBaton: status = 403 content = { 'error': 'Bad owner baton given' } else: (status, content) = self._logic(data) self.response.set_status(status) if 'Content-Type' in self.response.headers: del self.response.headers['Content-Type'] self.response.headers.add_header('Content-Type', 'text/plain') self.response.out.write(json.ify(content))
def test(self): j = json.ify([]) self.assertEquals(j, '[]') j = json.ify({}) self.assertEquals(j, '{}') j = json.ify([{'a':'b'}]) self.assertEquals(j, '[{"a":"b"}]') j = json.ify([{'a':'b'}, {'c':'d'}]) self.assertEquals(j, '[{"a":"b"},{"c":"d"}]') j = json.ify({'a':'b'}) self.assertEquals(j, '{"a":"b"}') j = json.ify({'a':'b"'}) self.assertEquals(j, '{"a":"b\\""}') j = json.ify({'a':1}) self.assertEquals(j, '{"a":1}') j = json.ify({'a':1L}) self.assertEquals(j, '{"a":1}') j = json.ify({'a':None}) self.assertEquals(j, '{"a":null}') j = json.ify({'a':True}) self.assertEquals(j, '{"a":true}') j = json.ify({'a':False}) self.assertEquals(j, '{"a":false}')