diff --git a/.gitignore b/.gitignore index 7a3eb2a..79060d0 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,7 @@ nosetests.xml coverage.xml *,cover .hypothesis/ +.pytest_cache # Translations *.mo @@ -108,6 +109,10 @@ ENV/ # IntelliJ /out/ +# vscode +.vscode +.history + # mpeltonen/sbt-idea plugin .idea_modules/ diff --git a/flask_consulate/consul.py b/flask_consulate/consul.py index 29cb668..86aa508 100644 --- a/flask_consulate/consul.py +++ b/flask_consulate/consul.py @@ -40,11 +40,17 @@ def __init__(self, app=None, **kwargs): os.environ.get('CONSUL_PORT', 8500) self.max_tries = self.kwargs.get('max_tries', 3) - self.session = None + self.consul = None + self._session = None # for backward compatibility if app is not None: self.init_app(app) + @property + def session(self): + self.app.logger.warning("session field deprecated - please use .consul") + return self._session + def init_app(self, app): self.app = app @@ -54,25 +60,27 @@ def init_app(self, app): raise RuntimeError('Flask application already initialized') app.extensions['consul'] = self - self.session = self._create_session( + self.consul = self._create_consul( test_connection=self.kwargs.get('test_connection', False), ) + # for backward compatibility + self._session = self.consul @with_retry_connections() - def _create_session(self, test_connection=False): + def _create_consul(self, test_connection=False): """ - Create a consulate.session object, and query for its leader to ensure + Create a consulate.consul object, and query for its leader to ensure that the connection is made. :param test_connection: call .leader() to ensure that the connection is valid :type test_connection: bool - :return consulate.Session instance + :return consulate.Consul instance """ - session = consulate.Session(host=self.host, port=self.port) + consul = consulate.Consul(host=self.host, port=self.port) if test_connection: - session.status.leader() - return session + consul.status.leader() + return consul @with_retry_connections() def apply_remote_config(self, namespace=None): @@ -93,7 +101,7 @@ def apply_remote_config(self, namespace=None): environment=os.environ.get('ENVIRONMENT', 'generic_environment') ) - for k, v in iteritems(self.session.kv.find(namespace)): + for k, v in iteritems(self.consul.kv.find(namespace)): k = k.replace(namespace, '') try: self.app.config[k] = json.loads(v) @@ -115,4 +123,4 @@ def register_service(self, **kwargs): kwargs passed to Consul.agent.service.register """ kwargs.setdefault('name', self.app.name) - self.session.agent.service.register(**kwargs) + self.consul.agent.service.register(**kwargs) diff --git a/tests/test_core.py b/tests/test_core.py index 0e6c249..cf0b92c 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -8,6 +8,7 @@ from flask_consulate import Consul from flask_consulate.exceptions import ConsulConnectionError +from consulate.exceptions import RequestError class TestFlaskConsulate(unittest.TestCase): @@ -62,12 +63,17 @@ def test_session(self): ) self.assertIsNotNone(consul) + self.assertIsNotNone(consul.consul) + + # consul.session should equal consul.consul. Included for backwards compatability + self.assertEqual(consul.consul, consul.session) + httpretty.disable() httpretty.reset() app = self.create_app() self.assertRaises( - ConsulConnectionError, + RequestError, lambda: Consul( app, consul_host='consul.internal', @@ -88,7 +94,7 @@ def test_register_service(self): body="localhost:8300", ) app = self.create_app() - with mock.patch('consulate.Session') as mocked: + with mock.patch('consulate.Consul') as mocked: consul = Consul(app) consul.register_service() self.assertEqual(mocked.return_value.agent.service.register.call_count, 1)