def _dispatch(self, request, *args, **kwargs): # always check API Key permissions self.args = defaultdict(lambda: '') for k, v in request.GET.iteritems(): self.args[k] = v for k, v in request.POST.iteritems(): self.args[k] = v self.format = self.args.pop('format', 'json') self.jsonp = self.args.pop('callback', None) self.all_fields = self.args.pop('all_fields', False) self.depth = min(self.args.pop('depth', 1), 5) if self.format not in SUPPORTED_FORMATS: return APIFormatNotSupported(format=self.format) self.api_key = self.args.pop('k', ANONYMOUS_API_KEY) authorized = True try: apikey = APIKey.objects.get(key=self.api_key, active=True) except APIKey.DoesNotExist: authorized = False else: authorized = apikey.is_authorized(request) if authorized: lim = apikey.check_limits(request) if lim != True: return APIRateLimit("Limit exceeded: %s" % lim) else: return APIForbidden("Invalid API Key") # parse slicing arguments, if any self.slice_left = self.args.pop('slice_left', 0) self.slice_right = self.args.pop('slice_right', 100) try: self.slice_left = int(self.slice_left) self.slice_right = int(self.slice_right) except ValueError: return APIBadSlice() if (self.slice_left >= self.slice_right or self.slice_left < 0 or self.slice_right < 1 or (self.slice_right - self.slice_left) > wsettings.WAPITI_MAX_SLICE_SIZE): return APIBadSlice() # parse the arguments self._decoder = Decoder(self.format) for k, v in self.args.iteritems(): try: self.args[k] = self._decoder.decode(v) except: return APICantGrokParameter(k, v) try: resp = super(WapitiBaseView, self).dispatch(request, *args, **kwargs) except APIBaseException, e: return e
class TestJSONDecode(unittest.TestCase): def setUp(self): self.decoder = Decoder('json') def test_str(self): self.assertEqual(self.decoder.decode('"FOO"'), "FOO") self.assertRaises(TypeError, self.decoder.decode, ('FOO', )) def test_list(self): self.assertEqual(self.decoder.decode('["FOO"]'), ["FOO"]) def test_dict(self): self.assertEqual(self.decoder.decode('{"FOO":"BAR"}'), {"FOO": "BAR"}) def test_nested(self): self.assertEqual(self.decoder.decode('{"FOO":["BAR"]}'), {"FOO": ["BAR"]})
class TestJSONDecode(unittest.TestCase): def setUp(self): self.decoder = Decoder('json') def test_str(self): self.assertEqual(self.decoder.decode('"FOO"'), "FOO") self.assertRaises(TypeError, self.decoder.decode, ('FOO',)) def test_list(self): self.assertEqual(self.decoder.decode('["FOO"]'), ["FOO"]) def test_dict(self): self.assertEqual(self.decoder.decode('{"FOO":"BAR"}'), {"FOO": "BAR"}) def test_nested(self): self.assertEqual(self.decoder.decode('{"FOO":["BAR"]}'), {"FOO": ["BAR"]})
def setUp(self): self.decoder = Decoder('json')