def _do_request(self, method, action, body=None, headers=None): """ Perform the HTTP request. The response is in either JSON format or plain text. A GET method will invoke a JSON response while a PUT/POST/DELETE returns message from the VSM in plain text format. Exception is raised when VSM replies with an INTERNAL SERVER ERROR HTTP status code (500) i.e. an error has occurred on the VSM or SERVICE UNAVAILABLE (503) i.e. VSM is not reachable. :param method: type of the HTTP request. POST, GET, PUT or DELETE :param action: path to which the client makes request :param body: dict for arguments which are sent as part of the request :param headers: header for the HTTP request :returns: JSON or plain text in HTTP response """ action = self.action_prefix + action if not headers and self.hosts: headers = self._get_auth_header(self.hosts[0]) headers['Content-Type'] = self._set_content_type('json') headers['Accept'] = self._set_content_type('json') if body: body = jsonutils.dumps(body, indent=2) LOG.debug("req: %s", body) try: resp = self.pool.spawn(requests.request, method, url=action, data=body, headers=headers, timeout=self.timeout).wait() except Exception as e: raise c_exc.VSMConnectionFailed(reason=e) LOG.debug("status_code %s", resp.status_code) if resp.status_code == requests.codes.OK: if 'application/json' in resp.headers['content-type']: try: return resp.json() except ValueError: return {} elif 'text/plain' in resp.headers['content-type']: LOG.debug("VSM: %s", resp.text) else: raise c_exc.VSMError(reason=resp.text)
def _do_request(self, method, action, body=None, headers=None): """ Perform the HTTP request. The response is in either XML format or plain text. A GET method will invoke a XML response while a PUT/POST/DELETE returns message from the VSM in plain text format. Exception is raised when VSM replies with an INTERNAL SERVER ERROR HTTP status code (500) i.e. an error has occurred on the VSM or SERVICE UNAVAILABLE (503) i.e. VSM is not reachable. :param method: type of the HTTP request. POST, GET, PUT or DELETE :param action: path to which the client makes request :param body: dict for arguments which are sent as part of the request :param headers: header for the HTTP request :returns: XML or plain text in HTTP response """ action = self.action_prefix + action if not headers and self.hosts: headers = self._get_auth_header(self.hosts[0]) headers['Content-Type'] = self._set_content_type('json') if body: body = self._serialize(body) LOG.debug(_("req: %s"), body) try: resp, replybody = (httplib2.Http(timeout=self.timeout). request(action, method, body=body, headers=headers)) except Exception as e: raise c_exc.VSMConnectionFailed(reason=e) LOG.debug(_("status_code %s"), resp.status) if resp.status == 200: if 'application/xml' in resp['content-type']: return self._deserialize(replybody, resp.status) elif 'text/plain' in resp['content-type']: LOG.debug(_("VSM: %s"), replybody) else: raise c_exc.VSMError(reason=replybody)