Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit a3d2cf1

Browse files
feat: add context manager support in client (#440)
- [ ] 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: googleapis/googleapis-gen@81decff Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 27e6a85 commit a3d2cf1

File tree

23 files changed

+306
-15
lines changed

23 files changed

+306
-15
lines changed

google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,12 @@ async def test_iam_permissions(
19311931
# Done; return the response.
19321932
return response
19331933

1934+
async def __aenter__(self):
1935+
return self
1936+
1937+
async def __aexit__(self, exc_type, exc, tb):
1938+
await self.transport.close()
1939+
19341940

19351941
try:
19361942
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,7 @@ def __init__(
408408
client_cert_source_for_mtls=client_cert_source_func,
409409
quota_project_id=client_options.quota_project_id,
410410
client_info=client_info,
411-
always_use_jwt_access=(
412-
Transport == type(self).get_transport_class("grpc")
413-
or Transport == type(self).get_transport_class("grpc_asyncio")
414-
),
411+
always_use_jwt_access=True,
415412
)
416413

417414
def create_instance(
@@ -2041,6 +2038,19 @@ def test_iam_permissions(
20412038
# Done; return the response.
20422039
return response
20432040

2041+
def __enter__(self):
2042+
return self
2043+
2044+
def __exit__(self, type, value, traceback):
2045+
"""Releases underlying transport's resources.
2046+
2047+
.. warning::
2048+
ONLY use as a context manager if the transport is NOT shared
2049+
with other clients! Exiting the with block will CLOSE the transport
2050+
and may cause errors in other clients!
2051+
"""
2052+
self.transport.close()
2053+
20442054

20452055
try:
20462056
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/base.py

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

374+
def close(self):
375+
"""Closes resources associated with the transport.
376+
377+
.. warning::
378+
Only call this method if the transport is NOT shared
379+
with other clients - this may cause errors in other clients!
380+
"""
381+
raise NotImplementedError()
382+
374383
@property
375384
def operations_client(self) -> operations_v1.OperationsClient:
376385
"""Return the client designed to process long-running operations."""

google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,5 +770,8 @@ def test_iam_permissions(
770770
)
771771
return self._stubs["test_iam_permissions"]
772772

773+
def close(self):
774+
self.grpc_channel.close()
775+
773776

774777
__all__ = ("BigtableInstanceAdminGrpcTransport",)

google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,5 +796,8 @@ def test_iam_permissions(
796796
)
797797
return self._stubs["test_iam_permissions"]
798798

799+
def close(self):
800+
return self.grpc_channel.close()
801+
799802

800803
__all__ = ("BigtableInstanceAdminGrpcAsyncIOTransport",)

google/cloud/bigtable_admin_v2/services/bigtable_table_admin/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,12 @@ async def test_iam_permissions(
22712271
# Done; return the response.
22722272
return response
22732273

2274+
async def __aenter__(self):
2275+
return self
2276+
2277+
async def __aexit__(self, exc_type, exc, tb):
2278+
await self.transport.close()
2279+
22742280

22752281
try:
22762282
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/bigtable_admin_v2/services/bigtable_table_admin/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,7 @@ def __init__(
446446
client_cert_source_for_mtls=client_cert_source_func,
447447
quota_project_id=client_options.quota_project_id,
448448
client_info=client_info,
449-
always_use_jwt_access=(
450-
Transport == type(self).get_transport_class("grpc")
451-
or Transport == type(self).get_transport_class("grpc_asyncio")
452-
),
449+
always_use_jwt_access=True,
453450
)
454451

455452
def create_table(
@@ -2443,6 +2440,19 @@ def test_iam_permissions(
24432440
# Done; return the response.
24442441
return response
24452442

2443+
def __enter__(self):
2444+
return self
2445+
2446+
def __exit__(self, type, value, traceback):
2447+
"""Releases underlying transport's resources.
2448+
2449+
.. warning::
2450+
ONLY use as a context manager if the transport is NOT shared
2451+
with other clients! Exiting the with block will CLOSE the transport
2452+
and may cause errors in other clients!
2453+
"""
2454+
self.transport.close()
2455+
24462456

24472457
try:
24482458
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,15 @@ def _prep_wrapped_messages(self, client_info):
360360
),
361361
}
362362

363+
def close(self):
364+
"""Closes resources associated with the transport.
365+
366+
.. warning::
367+
Only call this method if the transport is NOT shared
368+
with other clients - this may cause errors in other clients!
369+
"""
370+
raise NotImplementedError()
371+
363372
@property
364373
def operations_client(self) -> operations_v1.OperationsClient:
365374
"""Return the client designed to process long-running operations."""

google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,5 +920,8 @@ def test_iam_permissions(
920920
)
921921
return self._stubs["test_iam_permissions"]
922922

923+
def close(self):
924+
self.grpc_channel.close()
925+
923926

924927
__all__ = ("BigtableTableAdminGrpcTransport",)

google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,5 +942,8 @@ def test_iam_permissions(
942942
)
943943
return self._stubs["test_iam_permissions"]
944944

945+
def close(self):
946+
return self.grpc_channel.close()
947+
945948

946949
__all__ = ("BigtableTableAdminGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)