Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setCacheTime(seconds):
""" This function sets how long the request cache should last, in seconds.
Parameters
-----------
seconds : float
How long you would like Hypixel-API requests to be cached for.
"""
try:
global cacheTime
cacheTime = float(seconds)
return "Cache time has been successfully set to {} seconds.".format(cacheTime)
except ValueError as chainedException:
raise HypixelAPIError("Invalid cache time \"{}\"".format(seconds)) from chainedException
requestEnd += '&{}={}'.format(name, value)
cacheURL = HYPIXEL_API_URL + '{}?key={}{}'.format(typeOfRequest, "None", requestEnd) # TODO: Lowercase
allURLS = [HYPIXEL_API_URL + '{}?key={}{}'.format(typeOfRequest, api_key, requestEnd)] # Create request URL.
# If url exists in request cache, and time hasn't expired...
if cacheURL in requestCache and requestCache[cacheURL]['cacheTime'] > time():
response = requestCache[cacheURL]['data'] # TODO: Extend cache time
else:
requests = (grequests.get(u) for u in allURLS)
responses = grequests.imap(requests)
for r in responses:
response = r.json()
if response['success'] is False:
raise HypixelAPIError(response)
if typeOfRequest == 'player':
if response['player'] is None:
raise PlayerNotFoundException(uuid)
if typeOfRequest != 'key': # Don't cache key requests.
requestCache[cacheURL] = {}
requestCache[cacheURL]['data'] = response
requestCache[cacheURL]['cacheTime'] = time() + cacheTime # Cache request and clean current cache.
cleanCache()
try:
return response[typeOfRequest]
except KeyError:
return response
def getSession(self):
""" This function is used to get a player's session information. """
UUID = self.UUID
try:
session = getJSON('session', uuid=UUID)
except HypixelAPIError:
session = None
return session
Parameters
-----------
api_keys : list
A list of the API keys that you would like to use.
Example: ``['740b8cf8-8aba-f2ed-f7b10119d28']``.
"""
for api_key in api_keys:
if len(api_key) == HYPIXEL_API_KEY_LENGTH:
response = getJSON('key', key=api_key)
if response['success'] is True:
verified_api_keys.append(api_key)
else:
raise HypixelAPIError("hypixel/setKeys: Error with key XXXXXXXX-XXXX-XXXX-XXXX{} | {}".format(api_key[23:], response))
else:
raise HypixelAPIError("hypixel/setKeys: The key '{}' is not 36 characters.".format(api_key))