예제 #1
0
    def _request(self, resource, timeout=None):
        if timeout is None:
            timeout = self.timeout

        headers = {
            'Accept': 'application/json',
        }
        if self.auth_token:
            headers['Authorization'] = 'Token ' + self.auth_token

        try:
            response = requests.get('{0}/{1}'.format(self.base_url, resource),
                                    headers=headers,
                                    timeout=timeout)

            if response.status_code != requests.codes.ok:  # pylint: disable=no-member
                message = 'Resource "{0}" returned status code {1}'.format(
                    resource, response.status_code)
                log.error(message)
                raise ClientError(message)

            return response

        except requests.exceptions.RequestException:
            message = 'Unable to retrieve resource'
            log.exception(message)
            raise ClientError('{0} "{1}"'.format(message, resource))
예제 #2
0
    def request(self, method, resource, data=None, timeout=None, data_format=data_formats.JSON):
        """
        Retrieve the from an HTTP request.

        Arguments:

            method (http_method): HTTP method. Only GET and POST are supported currenly.
            resource (str): Path in the form of slash separated strings.
            data (dict): Dictionary containing POST data.
            timeout (float): Continue to attempt to retrieve a resource for this many seconds before giving up and
                raising an error.
            data_format (str): Format in which data should be returned

        Returns: API response data in specified data_format

        Raises: ClientError if the resource cannot be retrieved for any reason.

        """
        response = self._request(
            method=method,
            resource=resource,
            data=data,
            timeout=timeout,
            data_format=data_format
        )

        if data_format == data_formats.CSV:
            return response.text

        try:
            return response.json()
        except ValueError as exception:
            message = 'Unable to decode JSON response'
            log.exception(message)
            raise ClientError(message) from exception
예제 #3
0
    def get(self, resource, timeout=None, data_format=DF.JSON):
        """
        Retrieve the data for a resource.

        Arguments:

            resource (str): Path in the form of slash separated strings.
            timeout (float): Continue to attempt to retrieve a resource for this many seconds before giving up and
                raising an error.
            data_format (str): Format in which data should be returned

        Returns: API response data in specified data_format

        Raises: ClientError if the resource cannot be retrieved for any reason.

        """
        response = self._request(resource, timeout=timeout, data_format=data_format)

        if data_format == DF.CSV:
            return response.text

        try:
            return response.json()
        except ValueError:
            message = 'Unable to decode JSON response'
            log.exception(message)
            raise ClientError(message)
예제 #4
0
    def _request(self, method, resource, data=None, timeout=None, data_format=data_formats.JSON):
        if timeout is None:
            timeout = self.timeout

        accept_format = 'application/json'
        if data_format == data_formats.CSV:
            accept_format = 'text/csv'

        headers = {
            'Accept': accept_format,
        }

        if self.auth_token:
            headers['Authorization'] = 'Token ' + self.auth_token

        try:
            uri = f'{self.base_url}/{resource}'

            if method == http_methods.GET:
                params = self._data_to_get_params(data or {})
                response = requests.get(uri, params=params, headers=headers, timeout=timeout)
            elif method == http_methods.POST:
                response = requests.post(uri, data=(data or {}), headers=headers, timeout=timeout)
            else:
                raise ValueError(
                    'Invalid \'method\' argument: expected {} or {}, got {}'.format(
                        http_methods.GET,
                        http_methods.POST,
                        method,
                    )
                )

            status = response.status_code
            if status != requests.codes.ok:
                message = f'Resource "{resource}" returned status code {status}'
                error_class = ClientError

                if status == requests.codes.bad_request:
                    message = f'The request to {uri} was invalid.'
                    error_class = InvalidRequestError
                elif status == requests.codes.not_found:
                    message = f'Resource {uri} was not found on the API server.'
                    error_class = NotFoundError

                log.error(message)
                raise error_class(message)

            return response

        except requests.exceptions.Timeout as exception:
            message = f"Response from {resource} exceeded timeout of {timeout}s."
            log.exception(message)
            raise TimeoutError(message) from exception

        except requests.exceptions.RequestException as exception:
            message = 'Unable to retrieve resource'
            log.exception(message)
            raise ClientError(f'{message} "{resource}"') from exception
예제 #5
0
    def _request(self, resource, timeout=None, data_format=DF.JSON):
        if timeout is None:
            timeout = self.timeout

        accept_format = 'application/json'
        if data_format == DF.CSV:
            accept_format = 'text/csv'

        headers = {
            'Accept': accept_format,
        }

        if self.auth_token:
            headers['Authorization'] = 'Token ' + self.auth_token

        try:
            uri = '{0}/{1}'.format(self.base_url, resource)
            response = requests.get(uri, headers=headers, timeout=timeout)

            status = response.status_code
            if status != requests.codes.ok:
                message = 'Resource "{0}" returned status code {1}'.format(resource, status)
                error_class = ClientError

                if status == requests.codes.bad_request:
                    message = 'The request to {0} was invalid.'.format(uri)
                    error_class = InvalidRequestError
                elif status == requests.codes.not_found:
                    message = 'Resource {0} was not found on the API server.'.format(uri)
                    error_class = NotFoundError

                log.error(message)
                raise error_class(message)

            return response

        except requests.exceptions.Timeout:
            message = "Response from {0} exceeded timeout of {1}s.".format(resource, timeout)
            log.exception(message)
            raise TimeoutError(message)

        except requests.exceptions.RequestException:
            message = 'Unable to retrieve resource'
            log.exception(message)
            raise ClientError('{0} "{1}"'.format(message, resource))
예제 #6
0
    def get(self, resource, timeout=None):
        """
        Retrieve the data for a resource.

        Arguments:

            resource (str): Path in the form of slash separated strings.
            timeout (float): Continue to attempt to retrieve a resource for this many seconds before giving up and
                raising an error.

        Returns: A structure consisting of simple python types (dict, list, int, str etc).

        Raises: ClientError if the resource cannot be retrieved for any reason.

        """
        response = self._request(resource, timeout=timeout)

        try:
            return response.json()
        except ValueError:
            message = 'Unable to decode JSON response'
            log.exception(message)
            raise ClientError(message)