Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

feat: add context manager support in client #157

Merged
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
Expand Up @@ -3491,6 +3491,12 @@ async def list_usable_subnetworks(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
18 changes: 14 additions & 4 deletions google/cloud/container_v1/services/cluster_manager/client.py
Expand Up @@ -329,10 +329,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def list_clusters(
Expand Down Expand Up @@ -3588,6 +3585,19 @@ def list_usable_subnetworks(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.

.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Expand Up @@ -373,6 +373,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.

.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def list_clusters(
self,
Expand Down
Expand Up @@ -1123,5 +1123,8 @@ def list_usable_subnetworks(
)
return self._stubs["list_usable_subnetworks"]

def close(self):
self.grpc_channel.close()


__all__ = ("ClusterManagerGrpcTransport",)
Expand Up @@ -1173,5 +1173,8 @@ def list_usable_subnetworks(
)
return self._stubs["list_usable_subnetworks"]

def close(self):
return self.grpc_channel.close()


__all__ = ("ClusterManagerGrpcAsyncIOTransport",)