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 __init__(self, hostname, auth=AnonymousAuth()): """Initializer for the base class. Save the hostname to use for all requests as well as any authentication info needed. Args: hostname: The host for the requests. auth: The authentication info needed for any requests. """ self._hostname = self._construct_full_hostname(hostname) _logger.debug("Hostname is %s" % self._hostname) self._auth_info = auth