コード例 #1
0
 def _run(self, *args, **kwargs):
     url = self._url_template.format(
         protocol=self._runner.protocol,
         target_host=self._target_host,
         **self._url_params,
     )
     log.debug('Requesting', url=url, method=self._method)
     try:
         resp = self._runner.session.request(self._method,
                                             url,
                                             json=self._request_params)
     except RequestException as ex:
         raise RESTAPIError(
             f'Error performing REST-API call: {self._name}') from ex
     if not self._http_status_re.match(str(resp.status_code)):
         raise RESTAPIStatusMismatchError(
             f'HTTP status code "{resp.status_code}" while fetching {url}. '
             f'Expected {self._expected_http_status}: {resp.text}', )
     try:
         if resp.content == b'':
             # Allow empty responses
             response_dict = {}
         else:
             response_dict = resp.json()
         return self._process_response(response_dict)
     except (ValueError, UnicodeDecodeError) as ex:
         raise RESTAPIError(
             f'Error decoding response for url {url}: {resp.status_code} {resp.text}',
         ) from ex
コード例 #2
0
ファイル: api_base.py プロジェクト: binaryflesh/raiden
    def _run(self, *args, **kwargs):  # pylint: disable=unused-argument
        url = self._expand_url()
        log.debug('Requesting',
                  url=url,
                  method=self._method,
                  json=self._request_params)
        try:
            resp = self._runner.session.request(
                method=self._method,
                url=url,
                json=self._request_params,
                timeout=self._timeout,
            )
        except (ReadTimeout, ConnectTimeout) as ex:
            self._handle_timeout(ex)
        except RequestException as ex:
            raise RESTAPIError(
                f'Error performing REST-API call: {self._name}') from ex
        if not self._http_status_re.match(str(resp.status_code)):
            raise RESTAPIStatusMismatchError(
                f'HTTP status code "{resp.status_code}" while fetching {url}. '
                f'Expected {self._expected_http_status}: {resp.text}', )
        try:
            if resp.content == b'':
                # Allow empty responses
                response_dict = {}
            else:
                response_dict = resp.json()

            log.debug('Received response', json=response_dict)
            return self._process_response(response_dict)
        except (ValueError, UnicodeDecodeError) as ex:
            raise RESTAPIError(
                f'Error decoding response for url {url}: {resp.status_code} {resp.text}',
            ) from ex