コード例 #1
0
    def _list(self, url, response_key):
        resp, body = self.api.client.get(url)

        if resp is None or resp.status_code != 200:
            raise exceptions.from_response(resp, body, url)

        if not body:
            raise Exception("Call to " + url + " did not return a body.")

        return [self.resource_class(self, res) for res in body[response_key]]
コード例 #2
0
    def _list(self, url, response_key):
        resp, body = self.api.client.get(url)

        if resp is None or resp.status_code != 200:
            raise exceptions.from_response(resp, body, url)

        if not body:
            raise Exception("Call to " + url + " did not return a body.")

        return [self.resource_class(self, res) for res in body[response_key]]
コード例 #3
0
 def _test_from_response(self, body):
     data = {
         'status_code': 503,
         'headers': {
             'Content-Type': 'application/json',
             'x-compute-request-id': (
                 'req-65d6443c-5910-4eb4-b48a-e69849c26836'),
         },
         'text': json.dumps(body)
     }
     response = test_utils.TestResponse(data)
     fake_url = 'http://localhost:8779/v1.0/fake/instances'
     error = exceptions.from_response(response, 'GET', fake_url)
     self.assertIsInstance(error, exceptions.ServiceUnavailable)
コード例 #4
0
 def _test_from_response(self, body):
     data = {
         'status_code': 503,
         'headers': {
             'Content-Type':
             'application/json',
             'x-compute-request-id':
             ('req-65d6443c-5910-4eb4-b48a-e69849c26836'),
         },
         'text': json.dumps(body)
     }
     response = test_utils.TestResponse(data)
     fake_url = 'http://localhost:8779/v1.0/fake/instances'
     error = exceptions.from_response(response, 'GET', fake_url)
     self.assertIsInstance(error, exceptions.ServiceUnavailable)
コード例 #5
0
    def request(self, method, url, **kwargs):
        """Send an http request with the specified characteristics.

        Wrapper around `requests.Session.request` to handle tasks such as
        setting headers, JSON encoding/decoding, and error handling.

        :param method: method of HTTP request
        :param url: URL of HTTP request
        :param kwargs: any other parameter that can be passed to
'            requests.Session.request (such as `headers`) or `json`
             that will be encoded as JSON and used as `data` argument
        """
        kwargs.setdefault("headers", kwargs.get("headers", {}))
        kwargs["headers"]["User-Agent"] = self.user_agent
        if self.original_ip:
            kwargs["headers"]["Forwarded"] = "for=%s;by=%s" % (
                self.original_ip, self.user_agent)
        if self.timeout is not None:
            kwargs.setdefault("timeout", self.timeout)
        kwargs.setdefault("verify", self.verify)
        if self.cert is not None:
            kwargs.setdefault("cert", self.cert)
        self.serialize(kwargs)

        self._http_log_req(method, url, kwargs)
        if self.timings:
            start_time = time.time()
        resp = self.http.request(method, url, **kwargs)
        if self.timings:
            self.times.append(("%s %s" % (method, url),
                               start_time, time.time()))
        self._http_log_resp(resp)

        if resp.status_code >= 400:
            _logger.debug(
                "Request returned failure status: %s",
                resp.status_code)
            raise exceptions.from_response(resp, method, url)

        return resp
コード例 #6
0
    def request(self, method, url, **kwargs):
        """Send an http request with the specified characteristics.

        Wrapper around `requests.Session.request` to handle tasks such as
        setting headers, JSON encoding/decoding, and error handling.

        :param method: method of HTTP request
        :param url: URL of HTTP request
        :param kwargs: any other parameter that can be passed to
'            requests.Session.request (such as `headers`) or `json`
             that will be encoded as JSON and used as `data` argument
        """
        kwargs.setdefault("headers", kwargs.get("headers", {}))
        kwargs["headers"]["User-Agent"] = self.user_agent
        if self.original_ip:
            kwargs["headers"]["Forwarded"] = "for=%s;by=%s" % (
                self.original_ip, self.user_agent)
        if self.timeout is not None:
            kwargs.setdefault("timeout", self.timeout)
        kwargs.setdefault("verify", self.verify)
        if self.cert is not None:
            kwargs.setdefault("cert", self.cert)
        self.serialize(kwargs)

        self._http_log_req(method, url, kwargs)
        if self.timings:
            start_time = time.time()
        resp = self.http.request(method, url, **kwargs)
        if self.timings:
            self.times.append(
                ("%s %s" % (method, url), start_time, time.time()))
        self._http_log_resp(resp)

        if resp.status_code >= 400:
            _logger.debug("Request returned failure status: %s",
                          resp.status_code)
            raise exceptions.from_response(resp, method, url)

        return resp
コード例 #7
0
def check_for_exceptions(resp, body, url):
    if resp.status_code in (400, 422, 500):
        raise exceptions.from_response(resp, body, url)
コード例 #8
0
def check_for_exceptions(resp, body, url):
    if resp.status_code in (400, 422, 500):
        raise exceptions.from_response(resp, body, url)