예제 #1
0
    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))
예제 #2
0
파일: test_json.py 프로젝트: dlowe/pootpoot
 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}')