Пример #1
0
 def _raise_client_error(self, response, url=None):
     try:
         result = response.json()
     except Exception:
         if response.status_code == 304:
             error_msg = 'Nothing to modify'
             self._prepare_and_raise_exception(
                 message=error_msg,
                 error_code='not_modified',
                 status_code=response.status_code,
                 server_traceback='')
         else:
             message = response.content
             if url:
                 message = '{0} [{1}]'.format(message, url)
             error_msg = '{0}: {1}'.format(response.status_code, message)
         raise exceptions.CloudifyClientError(
             error_msg, status_code=response.status_code)
     message = result['message']
     code = result.get('error_code')
     server_traceback = result.get('server_traceback')
     self._prepare_and_raise_exception(message=message,
                                       error_code=code,
                                       status_code=response.status_code,
                                       server_traceback=server_traceback,
                                       response=response)
Пример #2
0
    def _do_request(self, requests_method, request_url, body, params, headers,
                    expected_status_code, stream, verify, timeout):
        """Run a requests method.

        :param request_method: string choosing the method, eg "get" or "post"
        :param request_url: the URL to run the request against
        :param body: request body, as a string
        :param params: querystring parameters, as a dict
        :param headers: request headers, as a dict
        :param expected_status_code: check that the response is this
            status code, can also be an iterable of allowed status codes.
        :param stream: whether or not to stream the response
        :param verify: the CA cert path
        :param timeout: request timeout or a (connect, read) timeouts pair
        """
        auth = None
        if self.has_kerberos() and not self.has_auth_header():
            if HTTPKerberosAuth is None:
                raise exceptions.CloudifyClientError(
                    'Trying to create a client with kerberos, '
                    'but kerberos_env does not exist')
            auth = HTTPKerberosAuth()
        response = requests_method(request_url,
                                   data=body,
                                   params=params,
                                   headers=headers,
                                   stream=stream,
                                   verify=verify,
                                   timeout=timeout or self.default_timeout_sec,
                                   auth=auth)
        if self.logger.isEnabledFor(logging.DEBUG):
            for hdr, hdr_content in response.request.headers.items():
                self.logger.debug('request header:  %s: %s'
                                  % (hdr, hdr_content))
            self.logger.debug('reply:  "%s %s" %s'
                              % (response.status_code,
                                 response.reason, response.content))
            for hdr, hdr_content in response.headers.items():
                self.logger.debug('response header:  %s: %s'
                                  % (hdr, hdr_content))

        if isinstance(expected_status_code, numbers.Number):
            expected_status_code = [expected_status_code]
        if response.status_code not in expected_status_code:
            self._raise_client_error(response, request_url)

        if response.status_code == 204:
            return None

        if stream:
            return StreamedResponse(response)

        response_json = response.json()

        if response.history:
            response_json['history'] = response.history

        return response_json
Пример #3
0
    def _do_request(self, requests_method, request_url, body, params, headers,
                    expected_status_code, stream, verify, timeout):
        auth = None
        if self.has_kerberos() and not self.has_auth_header():
            if HTTPKerberosAuth is None:
                raise exceptions.CloudifyClientError(
                    'Trying to create a client with kerberos, '
                    'but kerberos_env does not exist')
            auth = HTTPKerberosAuth()
        response = requests_method(request_url,
                                   data=body,
                                   params=params,
                                   headers=headers,
                                   stream=stream,
                                   verify=verify,
                                   timeout=timeout or self.default_timeout_sec,
                                   auth=auth)
        if self.logger.isEnabledFor(logging.DEBUG):
            for hdr, hdr_content in response.request.headers.iteritems():
                self.logger.debug('request header:  %s: %s' %
                                  (hdr, hdr_content))
            self.logger.debug(
                'reply:  "%s %s" %s' %
                (response.status_code, response.reason, response.content))
            for hdr, hdr_content in response.headers.iteritems():
                self.logger.debug('response header:  %s: %s' %
                                  (hdr, hdr_content))

        if response.status_code != expected_status_code:
            self._raise_client_error(response, request_url)

        if stream:
            return StreamedResponse(response)

        response_json = response.json()

        if response.history:
            response_json['history'] = response.history

        return response_json