Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ nosetests.xml
coverage.xml
*,cover
.hypothesis/
.pytest_cache

# Translations
*.mo
Expand Down Expand Up @@ -108,6 +109,10 @@ ENV/
# IntelliJ
/out/

# vscode
.vscode
.history

# mpeltonen/sbt-idea plugin
.idea_modules/

Expand Down
28 changes: 18 additions & 10 deletions flask_consulate/consul.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm cool with this, and here's an alternative just in case you weren't aware: https://docs.python.org/3/library/warnings.html#warning-categories

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. I did give warnings.warn a try - but the output is captured by the flask framework in the same way as print() etc. So I figured people won't actually see the warning output. Unless I'm missing something.

return self._session

def init_app(self, app):
self.app = app

Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)
10 changes: 8 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from flask_consulate import Consul
from flask_consulate.exceptions import ConsulConnectionError
from consulate.exceptions import RequestError


class TestFlaskConsulate(unittest.TestCase):
Expand Down Expand Up @@ -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',
Expand All @@ -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)
Expand Down