Beispiel #1
0
 def _lookup_cache_or_invoke_API(self, cache, API_full_url, timeout):
     cached = cache.get(API_full_url)
     if cached:
         return cached
     else:
         try:
             try:
                 from urllib.request import urlopen
             except ImportError:
                 from urllib2 import urlopen
             response = urlopen(API_full_url, None, timeout)
         except HTTPError as e:
             if '401' in str(e):
                 raise unauthorized_error.UnauthorizedError(
                     'Invalid API key')
             if '404' in str(e):
                 raise not_found_error.NotFoundError(
                     'The resource was not found')
             raise api_call_error.APICallError(str(e), e)
         except URLError as e:
             raise api_call_error.APICallError(str(e), e)
         else:
             data = response.read().decode('utf-8')
             cache.set(API_full_url, data)
             return data
Beispiel #2
0
 def check_status_code(cls, status_code, payload):
     if status_code < 400:
         return
     if status_code == 400:
         raise api_call_error.APICallError(payload)
     elif status_code == 401:
         raise unauthorized_error.UnauthorizedError(
             'Invalid API Key provided')
     elif status_code == 404:
         raise not_found_error.NotFoundError('Unable to find the resource')
     elif status_code == 502:
         raise api_call_error.BadGatewayError(
             'Unable to contact the upstream server')
     else:
         raise api_call_error.APICallError(payload)