コード例 #1
0
ファイル: client.py プロジェクト: devopszone/sensu_st2_backup
    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
コード例 #2
0
 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)
コード例 #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)
コード例 #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)
コード例 #5
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)
コード例 #6
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)
コード例 #7
0
 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)
コード例 #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)
コード例 #9
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)
コード例 #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
コード例 #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)