def request(self, url, method, **kwargs):
        kwargs.setdefault('headers', kwargs.get('headers', {}))
        # NOTE(sileht): The standard call raises errors from
        # keystoneauth, where we need to raise the gnocchiclient errors.
        raise_exc = kwargs.pop('raise_exc', True)

        try:
            resp = super(SessionClient, self).request(url,
                                                      method,
                                                      raise_exc=False,
                                                      **kwargs)
        except k_exc.connection.ConnectFailure as e:
            raise exceptions.ConnectionFailure(message=str(e),
                                               url=url,
                                               method=method)
        except k_exc.connection.UnknownConnectionError as e:
            raise exceptions.UnknownConnectionError(message=str(e),
                                                    url=url,
                                                    method=method)
        except k_exc.connection.ConnectTimeout as e:
            raise exceptions.ConnectionTimeout(message=str(e),
                                               url=url,
                                               method=method)
        except k_exc.SSLError as e:
            raise exceptions.SSLError(message=str(e), url=url, method=method)

        if raise_exc and resp.status_code >= 400:
            raise exceptions.from_response(resp, method)
        return resp
 def test_from_response_unknown_middleware(self):
     r = models.Response()
     r.status_code = 400
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps({"unknown": "random message"}).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.ClientException)
     self.assertEqual('{"unknown": "random message"}', exc.message)
Exemple #3
0
 def test_resource_type_before_resource(self):
     r = models.Response()
     r.status_code = 404
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps(
         {"description": "Resource type foobar does not exist"}
     ).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.ResourceTypeNotFound)
Exemple #4
0
 def test_from_response_404(self):
     r = models.Response()
     r.status_code = 404
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps(
         {"description": "Archive policy rule foobar does not exist"}
     ).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.ArchivePolicyRuleNotFound)
 def test_from_response_404(self):
     r = models.Response()
     r.status_code = 404
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps(
         {"description": "Archive policy rule foobar does not exist"}
     ).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.ArchivePolicyRuleNotFound)
 def test_resource_type_before_resource(self):
     r = models.Response()
     r.status_code = 404
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps(
         {"description": "Resource type foobar does not exist"}
     ).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.ResourceTypeNotFound)
 def test_from_response_unknown_middleware(self):
     r = models.Response()
     r.status_code = 400
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps(
         {"unknown": "random message"}
     ).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.ClientException)
     self.assertEqual('{"unknown": "random message"}', exc.message)
Exemple #8
0
 def test_from_response_keystone_401(self):
     r = models.Response()
     r.status_code = 401
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps({"error": {
         "message": "The request you have made requires authentication.",
         "code": 401, "title": "Unauthorized"}}
     ).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.Unauthorized)
     self.assertEqual("The request you have made requires authentication.",
                      exc.message)
 def test_from_response_keystone_401(self):
     r = models.Response()
     r.status_code = 401
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps({"error": {
         "message": "The request you have made requires authentication.",
         "code": 401, "title": "Unauthorized"}}
     ).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.Unauthorized)
     self.assertEqual("The request you have made requires authentication.",
                      exc.message)
Exemple #10
0
    def request(self, url, method, **kwargs):
        kwargs.setdefault('headers', kwargs.get('headers', {}))
        # NOTE(sileht): The standard call raises errors from
        # keystoneauth, where we need to raise the gnocchiclient errors.
        raise_exc = kwargs.pop('raise_exc', True)
        resp = super(SessionClient, self).request(url,
                                                  method,
                                                  raise_exc=False,
                                                  **kwargs)

        if raise_exc and resp.status_code >= 400:
            raise exceptions.from_response(resp, method)
        return resp
Exemple #11
0
 def test_from_response_404_with_detail(self):
     r = models.Response()
     r.status_code = 404
     r.headers['Content-Type'] = "application/json"
     r._content = json.dumps({
         "code": 404,
         "description": {
             "cause": "Aggregation method does not exist for this metric",
             "detail": {
                 "aggregation_method": "rate:mean",
                 "metric": "a914dad6-b8f6-42f6-b090-6daa29725caf",
             }},
         "title": "Not Found"
     }).encode('utf-8')
     exc = exceptions.from_response(r)
     self.assertIsInstance(exc, exceptions.ClientException)