def setup_riot_api(conf): cassioepia = conf['cassiopeia'] baseriotapi.set_api_key(cassioepia['api_key']) baseriotapi.set_region(cassioepia['region']) limits = cassioepia.get('rate_limits', None) if limits is not None: if isinstance(limits[0], (list, tuple)): baseriotapi.set_rate_limits(*limits) else: baseriotapi.set_rate_limit(limits[0], limits[1]) baseriotapi.print_calls(cassioepia.get('print_calls', False))
import json import string from boto.sqs.connection import SQSConnection from boto.sqs.message import RawMessage from cassiopeia import baseriotapi from django.http import HttpResponse from django.views.decorators.http import require_POST from portal.errors import INVALID_REQUEST_FORMAT from portal.keys import AWS_ACCESS_KEY_ID from portal.keys import AWS_SECRET_ACCESS_KEY from portal.keys import RIOT_API_KEY QUEUE = "TEAM_BUILDER_DRAFT_RANKED_5x5" SEASON = "SEASON2016" baseriotapi.set_api_key(RIOT_API_KEY) baseriotapi.set_rate_limits((270, 10), (16200, 600)) def format_key(key): # filter to remove punctuation translator = str.maketrans({key: None for key in string.punctuation}) # also remove white spaces and make lowercase return key.translate(translator).replace(" ", "").lower() def riot_request(region, args): # set region baseriotapi.set_region(region) # extract arguments
# Set API key # Lets cassi handle riot api rate limits. But does this work per instance of this app (if many ppl are using website at same time) or globally? is this the best place # for this, or should this be under the relevant url routes? riotapi.set_region('na') riotapi.print_calls(True) riotapi.set_api_key(API_KEY) riotapi.set_rate_limits((9, 10), (480, 600)) riotapi.set_load_policy(LoadPolicy.lazy) baseriotapi.set_region('na') baseriotapi.print_calls(True) baseriotapi.set_api_key(API_KEY) baseriotapi.set_rate_limits((9, 10), (480, 600)) def auto_retry(api_call_method): """ A decorator to automatically retry 500s (Service Unavailable) and skip 400s (Bad Request) or 404 (Not Found). """ def call_wrapper(*args, **kwargs): try: return api_call_method(*args, **kwargs) except APIError as error: # Try Again Once if error.error_code in [500]: try: print("Got a 500, trying again...") return api_call_method(*args, **kwargs) except APIError as another_error: if another_error.error_code in [500, 400, 404]: