Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_blocks_rate_limited(self):
self.mox.StubOutWithMock(FakeSource, 'get_blocklist')
FakeSource.get_blocklist().AndRaise(source.RateLimited('foo', partial=[]))
self.mox.ReplayAll()
resp = app.application.get_response('/fake/123/@blocks/')
self.assertEqual(429, resp.status_int)
def test_load_blocklist_rate_limited(self):
source = FakeSource(id='x')
self.mox.StubOutWithMock(source.gr_source, 'get_blocklist_ids')
source.gr_source.get_blocklist_ids().AndRaise(
gr_source.RateLimited(partial=[4, 5]))
self.mox.ReplayAll()
source.load_blocklist()
self.assertEqual([4, 5], source.blocked_ids)
def _get_blocklist_fn(self, api_endpoint, response_fn):
values = []
cursor = '-1'
while cursor and cursor != '0':
try:
resp = self.urlopen(api_endpoint % cursor)
except urllib_error.HTTPError as e:
if e.code in HTTP_RATE_LIMIT_CODES:
raise source.RateLimited(str(e), partial=values)
raise
values.extend(response_fn(resp))
cursor = resp.get('next_cursor_str')
return values
def _get_blocklist_fn(self, api_endpoint, response_fn):
values = []
cursor = '-1'
while cursor and cursor != '0':
try:
resp = self.urlopen(api_endpoint % cursor)
except urllib.error.HTTPError as e:
if e.code in HTTP_RATE_LIMIT_CODES:
raise source.RateLimited(str(e), partial=values)
raise
values.extend(response_fn(resp))
cursor = resp.get('next_cursor_str')
return values
def load_blocklist(self):
"""Fetches this user's blocklist, if supported, and stores it in the entity."""
if not self.HAS_BLOCKS:
return
try:
ids = self.gr_source.get_blocklist_ids()
except gr_source.RateLimited as e:
ids = e.partial or []
self.blocked_ids = ids[:BLOCKLIST_MAX_IDS]
self.put()
if domain != src.DOMAIN:
raise exc.HTTPBadRequest('Expected domain %s in tag URI %s, found %s' %
(src.DOMAIN, arg, domain))
args[i] = id
# handle default path elements
args = [None if a in defaults else a
for a, defaults in zip(args, PATH_DEFAULTS)]
user_id = args[0] if args else None
# get activities (etc)
try:
if len(args) >= 2 and args[1] == '@blocks':
try:
response = {'items': src.get_blocklist()}
except source.RateLimited as e:
if not e.partial:
self.abort(429, str(e))
response = {'items': e.partial}
else:
response = src.get_activities_response(*args, **self.get_kwargs())
except (NotImplementedError, ValueError) as e:
self.abort(400, str(e))
# other exceptions are handled by webutil.handlers.handle_exception(),
# which uses interpret_http_exception(), etc.
# fetch actor if necessary
actor = response.get('actor')
if not actor and self.request.get('format') == 'atom':
# atom needs actor
actor = src.get_actor(user_id) if src else {}