Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'Client.close' #100

Merged
merged 3 commits into from Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions google/cloud/client.py
Expand Up @@ -214,6 +214,21 @@ def _http(self):
self._http_internal.configure_mtls_channel(self._client_cert_source)
return self._http_internal

def close(self):
"""Clean up transport, if set.

Suggested use:

.. code-block:: python

import contextlib

with contextlib.closing(client): # closes on exit
do_something_with(client)
"""
if self._http_internal is not None:
self._http_internal.close()


class _ClientProjectMixin(object):
"""Mixin to allow setting the project on the client.
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_client.py
Expand Up @@ -205,6 +205,21 @@ def test_from_service_account_json(self):
file_open.assert_called_once_with(mock.sentinel.filename, "r", encoding="utf-8")
constructor.assert_called_once_with(info)

def test_close_w__http_internal_none(self):
credentials = _make_credentials()
client_obj = self._make_one(credentials=credentials, _http=None)

client_obj.close() # noraise

def test_close_w__http_internal_set(self):
credentials = _make_credentials()
http = mock.Mock(spec=["close"])
client_obj = self._make_one(credentials=credentials, _http=http)

client_obj.close()

http.close.assert_called_once_with()


class Test_ClientProjectMixin(unittest.TestCase):
@staticmethod
Expand Down