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

Commit 03953f8

Browse files
feat: add context manager support in client (#157)
- [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: googleapis/googleapis@787f8c9 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 37c4f44 commit 03953f8

File tree

14 files changed

+285
-10
lines changed

14 files changed

+285
-10
lines changed

google/cloud/container_v1/services/cluster_manager/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,6 +3491,12 @@ async def list_usable_subnetworks(
34913491
# Done; return the response.
34923492
return response
34933493

3494+
async def __aenter__(self):
3495+
return self
3496+
3497+
async def __aexit__(self, exc_type, exc, tb):
3498+
await self.transport.close()
3499+
34943500

34953501
try:
34963502
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/container_v1/services/cluster_manager/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,7 @@ def __init__(
329329
client_cert_source_for_mtls=client_cert_source_func,
330330
quota_project_id=client_options.quota_project_id,
331331
client_info=client_info,
332-
always_use_jwt_access=(
333-
Transport == type(self).get_transport_class("grpc")
334-
or Transport == type(self).get_transport_class("grpc_asyncio")
335-
),
332+
always_use_jwt_access=True,
336333
)
337334

338335
def list_clusters(
@@ -3588,6 +3585,19 @@ def list_usable_subnetworks(
35883585
# Done; return the response.
35893586
return response
35903587

3588+
def __enter__(self):
3589+
return self
3590+
3591+
def __exit__(self, type, value, traceback):
3592+
"""Releases underlying transport's resources.
3593+
3594+
.. warning::
3595+
ONLY use as a context manager if the transport is NOT shared
3596+
with other clients! Exiting the with block will CLOSE the transport
3597+
and may cause errors in other clients!
3598+
"""
3599+
self.transport.close()
3600+
35913601

35923602
try:
35933603
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/container_v1/services/cluster_manager/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,15 @@ def _prep_wrapped_messages(self, client_info):
373373
),
374374
}
375375

376+
def close(self):
377+
"""Closes resources associated with the transport.
378+
379+
.. warning::
380+
Only call this method if the transport is NOT shared
381+
with other clients - this may cause errors in other clients!
382+
"""
383+
raise NotImplementedError()
384+
376385
@property
377386
def list_clusters(
378387
self,

google/cloud/container_v1/services/cluster_manager/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,5 +1123,8 @@ def list_usable_subnetworks(
11231123
)
11241124
return self._stubs["list_usable_subnetworks"]
11251125

1126+
def close(self):
1127+
self.grpc_channel.close()
1128+
11261129

11271130
__all__ = ("ClusterManagerGrpcTransport",)

google/cloud/container_v1/services/cluster_manager/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,5 +1173,8 @@ def list_usable_subnetworks(
11731173
)
11741174
return self._stubs["list_usable_subnetworks"]
11751175

1176+
def close(self):
1177+
return self.grpc_channel.close()
1178+
11761179

11771180
__all__ = ("ClusterManagerGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)