def _cs_request(self, *args, **kwargs): kargs = {} kargs.setdefault('headers', kwargs.get('headers', {})) kargs['headers']['User-Agent'] = self.USER_AGENT if 'content_type' in kwargs: kargs['headers']['Content-Type'] = kwargs['content_type'] kargs['headers']['Accept'] = kwargs['content_type'] else: kargs['headers']['Content-Type'] = self.content_type kargs['headers']['Accept'] = self.content_type if 'body' in kwargs: kargs['body'] = kwargs['body'] resp, body = self.request(*args, **kargs) utils.http_log(_logger, args, kargs, resp, body) status_code = self.get_status_code(resp) if status_code == 401: raise exceptions.Unauthorized(message=body) elif status_code == 403: raise exceptions.Forbidden(message=body) return resp, body
def do_request(self, method, action, body=None, headers=None, params=None): """ Connects to the server and issues a request. Returns the result data, or raises an appropriate exception if HTTP status code is not 2xx :param method: HTTP method ("GET", "POST", "PUT", etc...) :param body: string of data to send, or None (default) :param headers: mapping of key/value pairs to add as headers :param params: dictionary of key/value pairs to add to append to action :raises: ConnectionFailed """ LOG.debug("Client issuing request: %s", action) # Ensure we have a tenant id if not self.tenant: raise Exception("Tenant ID not set") # Add format and tenant_id action += ".%s" % self.format action = self.action_prefix + action action = action.replace('{tenant_id}', self.tenant) if type(params) is dict: action += '?' + urllib.urlencode(params) if body: body = self.serialize(body) try: connection_type = self.get_connection_type() headers = headers or {"Content-Type": "application/%s" % self.format} # if available, add authentication token if self.auth_token: headers[AUTH_TOKEN_HEADER] = self.auth_token # Open connection and send request, handling SSL certs certs = {'key_file': self.key_file, 'cert_file': self.cert_file} certs = dict((x, certs[x]) for x in certs if certs[x] is not None) if self.use_ssl and len(certs): conn = connection_type(self.host, self.port, **certs) else: conn = connection_type(self.host, self.port) res = self._send_request(conn, method, action, body, headers) status_code = self.get_status_code(res) data = res.read() utils.http_log(LOG, [method, action], {'body': body, 'headers': headers, }, res, data) if self.logger: self.logger.debug("Quantum Client Reply (code = %s) :\n %s" % (str(status_code), data)) if status_code in (httplib.OK, httplib.CREATED, httplib.ACCEPTED, httplib.NO_CONTENT): return self.deserialize(data, status_code) else: self._handle_fault_response(status_code, data) except (socket.error, IOError), e: exc = exceptions.ConnectionFailed(reason=str(e)) LOG.exception(exc.message) raise exc