예제 #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
예제 #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 api_response_error.UnauthorizedError('Invalid API Key provided')
     elif status_code == 404:
         raise api_response_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)
예제 #3
0
    def call_API(self,
                 API_endpoint_URL,
                 params_dict,
                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
        """
        Invokes a specific OWM web API endpoint URL, returning raw JSON data.

        :param API_endpoint_URL: the API endpoint to be invoked
        :type API_endpoint_URL: str
        :param params_dict: a dictionary containing the query parameters to be
            used in the HTTP request (given as key-value couples in the dict)
        :type params_dict: dict
        :param timeout: how many seconds to wait for connection establishment
            (defaults to ``socket._GLOBAL_DEFAULT_TIMEOUT``)
        :type timeout: int
        :returns: a string containing raw JSON data
        :raises: *APICallError*

        """
        url = self._build_full_URL(API_endpoint_URL, params_dict)
        cached = self._cache.get(url)
        if cached:
            return cached
        else:
            try:
                if self._identity and self._identity.token:
                    bearer_token_header = "Bearer %s:%s" % (
                        self._identity.device_id, self._identity.token)
                else:
                    bearer_token_header = None
                try:
                    from urllib.request import build_opener
                    opener = build_opener()
                    if bearer_token_header:
                        opener.addheaders = [('Authorization',
                                              bearer_token_header)]
                except ImportError:
                    from urllib2 import build_opener
                    opener = build_opener()
                    if bearer_token_header:
                        opener.addheaders = [('Authorization',
                                              bearer_token_header)]
                response = opener.open(url, None, timeout)
            except HTTPError as e:
                raise api_call_error.APICallError(str(e.reason), e)
            except URLError as e:
                raise api_call_error.APICallError(str(e.reason), e)
            else:
                data = response.read().decode('utf-8')
                self._cache.set(url, data)
                return data
예제 #4
0
    def call_API(self,
                 API_endpoint_URL,
                 params_dict,
                 timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
        """
        Invokes a specific OWM web API endpoint URL, returning raw JSON data.

        :param API_endpoint_URL: the API endpoint to be invoked
        :type API_endpoint_URL: str
        :param params_dict: a dictionary containing the query parameters to be
            used in the HTTP request (given as key-value couples in the dict)
        :type params_dict: dict
        :param timeout: how many seconds to wait for connection establishment
            (defaults to ``socket._GLOBAL_DEFAULT_TIMEOUT``)
        :type timeout: int
        :returns: a string containing raw JSON data
        :raises: *APICallError*

        """
        url = self._build_full_URL(API_endpoint_URL, params_dict)
        cached = self._cache.get(url)
        if cached:
            return cached
        else:
            try:
                try:
                    from urllib.request import urlopen
                except ImportError:
                    from urllib2 import urlopen
                response = urlopen(url, None, timeout)
            except HTTPError as e:
                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')
                self._cache.set(url, data)
                return data