def _make_request(self, method, path, data=None, **kwargs):
        """Make a request.

        Use the `requests` module to actually perform the request.

        Args:
            `method`: The method to use.
            `path`: The path to the resource.
            `data`: Any data to send (for POST and PUT requests).
            `kwargs`: Other parameters for `requests`.
        Returns:
            The content of the response.
        Raises:
            An exception depending on the HTTP status code of the response.
        """
        _logger.debug("Method for request is %s" % method)
        url = self._construct_full_url(path)
        _logger.debug("URL for request is %s" % url)
        self._auth_info.populate_request_data(kwargs)
        _logger.debug("The arguments are %s" % kwargs)
        res = requests.request(method, url, data=data, **kwargs)

        if res.ok:
            _logger.debug("Request was successful.")
            return res.content

        if hasattr(res, 'content'):
            _logger.debug("Response was %s:%s" % (res.status_code, res.content))
            raise self._exception_for(res.status_code)(
                res.content, http_code=res.status_code
            )
        else:
            msg = "No response from the  URL %s" % res.request.url
            _logger.error(msg)
            raise NoResponseError(msg)
    def _handle_wrong_field(cls, field_name, field_type):
        """Raise an exception whenever an invalid attribute with
        the given name was attempted to be set to or retrieved from
        this model class.

        Assumes that the given field is invalid, without making any checks.

        Also adds an entry to the logs.
        """
        if field_type == ATTR_TYPE_READ:
            field_type = 'readable'
        elif field_type == ATTR_TYPE_WRITE:
            field_type = 'writable'
        elif field_type == ATTR_TYPE_URL:
            field_type = 'URL'
        else:
            raise AttributeError('Invalid attribute type: {}'.format(
                field_type
            ))

        msg = '{} has no {} attribute "{}"'.format(
            cls.__name__,
            field_type,
            field_name
        )
        _logger.error(msg)
        raise AttributeError(msg)
    def _make_request(self, method, path, data=None, **kwargs):
        """Make a request.

        Use the `requests` module to actually perform the request.

        Args:
            `method`: The method to use.
            `path`: The path to the resource.
            `data`: Any data to send (for POST and PUT requests).
            `kwargs`: Other parameters for `requests`.
        Returns:
            The content of the response.
        Raises:
            An exception depending on the HTTP status code of the response.
        """
        _logger.debug("Method for request is %s" % method)
        url = self._construct_full_url(path)
        _logger.debug("URL for request is %s" % url)
        self._auth_info.populate_request_data(kwargs)
        _logger.debug("The arguments are %s" % kwargs)
        res = requests.request(method, url, data=data, **kwargs)

        if res.ok:
            _logger.debug("Request was successful.")
            return res.content

        if hasattr(res, 'content'):
            _logger.debug("Response was %s:%s" % (res.status_code, res.content))
            raise self._exception_for(res.status_code)(
                res.content, http_code=res.status_code
            )
        else:
            msg = "No response from the  URL %s" % res.request.url
            _logger.error(msg)
            raise NoResponseError(msg)
Example #4
0
    def _handle_wrong_field(cls, field_name, field_type):
        """Raise an exception whenever an invalid attribute with
        the given name was attempted to be set to or retrieved from
        this model class.

        Assumes that the given field is invalid, without making any checks.

        Also adds an entry to the logs.
        """
        if field_type == ATTR_TYPE_READ:
            field_type = 'readable'
        elif field_type == ATTR_TYPE_WRITE:
            field_type = 'writable'
        elif field_type == ATTR_TYPE_URL:
            field_type = 'URL'
        else:
            raise AttributeError('Invalid attribute type: {}'.format(
                field_type
            ))

        msg = '{} has no {} attribute "{}"'.format(
            cls.__name__,
            field_type,
            field_name
        )
        _logger.error(msg)
        raise AttributeError(msg)
 def __setattr__(self, name, value):
     """Set the value of a field."""
     if not '_is_initialized' in self.__dict__ or name in self.__dict__:
         return super(BaseModel, self).__setattr__(name, value)
     elif name in self.write_also_fields:
         self._modified_fields[name] = value
     else:
         msg = "%s has no attribute %s" % (self.__class__.__name__, name)
         _logger.error(msg)
         raise AttributeError(msg)
Example #6
0
 def __setattr__(self, name, value):
     """Set the value of a field."""
     if not '_is_initialized' in self.__dict__ or name in self.__dict__:
         return super(BaseModel, self).__setattr__(name, value)
     elif name in self.write_also_fields:
         self._modified_fields[name] = value
     else:
         msg = "%s has no attribute %s" % (self.__class__.__name__, name)
         _logger.error(msg)
         raise AttributeError(msg)
    def __getattr__(self, name):
        """Return the value of the field.

        Look in the modified and the populated fields.
        """
        if name in self._modified_fields:
            return self._modified_fields[name]
        elif name in self._populated_fields:
            return self._populated_fields[name]
        else:
            msg = "%s has no attribute %s" % (self.__class__.__name__, name)
            _logger.error(msg)
            raise AttributeError(msg)
Example #8
0
    def __getattr__(self, name):
        """Return the value of the field.

        Look in the modified and the populated fields.
        """
        if name in self._modified_fields:
            return self._modified_fields[name]
        elif name in self._populated_fields:
            return self._populated_fields[name]
        else:
            msg = "%s has no attribute %s" % (self.__class__.__name__, name)
            _logger.error(msg)
            raise AttributeError(msg)