Skip to content

Commit 4de3956

Browse files
committed
create disconnect() method for BaseAPI
This new method can help ppl tearing things down without using the "with" statement. Similarly to the motivation behind implmenting the "with" statement, this can be useful for connections that were made through a tunnel (for example ssh). This method makes it possible for example to have a BaseAPI object wrapped up in another class, and tear down connections only when that wrapping class is all done with calls to the object.
1 parent 491adb1 commit 4de3956

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Diff for: pypuppetdb/api.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,22 @@ def __init__(self, host='localhost', port=8080, ssl_verify=True,
194194
else:
195195
self.protocol = 'http'
196196

197+
def disconnect(self):
198+
"""Close all connections that this class opened up."""
199+
# If we don't explicitly close connections, we might cause other
200+
# functions or libraries to hang on the open connections. This happens
201+
# for example with using paramiko to tunnel PuppetDB connections
202+
# through ssh.
203+
self._session.close()
204+
197205
def __enter__(self):
198206
"""Set up environment for 'with' statement."""
199207
# Once this class has been instantiated, there's nothing more required
200208
return self
201209

202210
def __exit__(self, type, value, trace):
203211
"""Tear down connections."""
204-
# If we don't explicitly close connections, we might cause other
205-
# functions or libraries to hang on the open connections. This happens
206-
# for example with using paramiko to tunnel PuppetDB connections
207-
# through ssh.
208-
self._session.close()
212+
self.disconnect()
209213

210214
@property
211215
def version(self):

0 commit comments

Comments
 (0)