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

Commit 51eae67

Browse files
feat: add context manager support in client (#516)
- [ ] 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 5224c46 commit 51eae67

20 files changed

Lines changed: 263 additions & 16 deletions

File tree

google/pubsub_v1/services/publisher/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,12 @@ async def test_iam_permissions(
11831183
# Done; return the response.
11841184
return response
11851185

1186+
async def __aenter__(self):
1187+
return self
1188+
1189+
async def __aexit__(self, exc_type, exc, tb):
1190+
await self.transport.close()
1191+
11861192

11871193
try:
11881194
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/pubsub_v1/services/publisher/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,7 @@ def __init__(
399399
client_cert_source_for_mtls=client_cert_source_func,
400400
quota_project_id=client_options.quota_project_id,
401401
client_info=client_info,
402-
always_use_jwt_access=(
403-
Transport == type(self).get_transport_class("grpc")
404-
or Transport == type(self).get_transport_class("grpc_asyncio")
405-
),
402+
always_use_jwt_access=True,
406403
)
407404

408405
def create_topic(
@@ -1064,6 +1061,19 @@ def detach_subscription(
10641061
# Done; return the response.
10651062
return response
10661063

1064+
def __enter__(self):
1065+
return self
1066+
1067+
def __exit__(self, type, value, traceback):
1068+
"""Releases underlying transport's resources.
1069+
1070+
.. warning::
1071+
ONLY use as a context manager if the transport is NOT shared
1072+
with other clients! Exiting the with block will CLOSE the transport
1073+
and may cause errors in other clients!
1074+
"""
1075+
self.transport.close()
1076+
10671077
def set_iam_policy(
10681078
self,
10691079
request: iam_policy_pb2.SetIamPolicyRequest = None,

google/pubsub_v1/services/publisher/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,15 @@ def _prep_wrapped_messages(self, client_info):
302302
),
303303
}
304304

305+
def close(self):
306+
"""Closes resources associated with the transport.
307+
308+
.. warning::
309+
Only call this method if the transport is NOT shared
310+
with other clients - this may cause errors in other clients!
311+
"""
312+
raise NotImplementedError()
313+
305314
@property
306315
def create_topic(
307316
self,

google/pubsub_v1/services/publisher/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,5 +559,8 @@ def test_iam_permissions(
559559
)
560560
return self._stubs["test_iam_permissions"]
561561

562+
def close(self):
563+
self.grpc_channel.close()
564+
562565

563566
__all__ = ("PublisherGrpcTransport",)

google/pubsub_v1/services/publisher/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,5 +569,8 @@ def test_iam_permissions(
569569
)
570570
return self._stubs["test_iam_permissions"]
571571

572+
def close(self):
573+
return self.grpc_channel.close()
574+
572575

573576
__all__ = ("PublisherGrpcAsyncIOTransport",)

google/pubsub_v1/services/schema_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,12 @@ async def test_iam_permissions(
871871
# Done; return the response.
872872
return response
873873

874+
async def __aenter__(self):
875+
return self
876+
877+
async def __aexit__(self, exc_type, exc, tb):
878+
await self.transport.close()
879+
874880

875881
try:
876882
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/pubsub_v1/services/schema_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,7 @@ def __init__(
342342
client_cert_source_for_mtls=client_cert_source_func,
343343
quota_project_id=client_options.quota_project_id,
344344
client_info=client_info,
345-
always_use_jwt_access=(
346-
Transport == type(self).get_transport_class("grpc")
347-
or Transport == type(self).get_transport_class("grpc_asyncio")
348-
),
345+
always_use_jwt_access=True,
349346
)
350347

351348
def create_schema(
@@ -785,6 +782,19 @@ def validate_message(
785782
# Done; return the response.
786783
return response
787784

785+
def __enter__(self):
786+
return self
787+
788+
def __exit__(self, type, value, traceback):
789+
"""Releases underlying transport's resources.
790+
791+
.. warning::
792+
ONLY use as a context manager if the transport is NOT shared
793+
with other clients! Exiting the with block will CLOSE the transport
794+
and may cause errors in other clients!
795+
"""
796+
self.transport.close()
797+
788798
def set_iam_policy(
789799
self,
790800
request: iam_policy_pb2.SetIamPolicyRequest = None,

google/pubsub_v1/services/schema_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ def _prep_wrapped_messages(self, client_info):
181181
),
182182
}
183183

184+
def close(self):
185+
"""Closes resources associated with the transport.
186+
187+
.. warning::
188+
Only call this method if the transport is NOT shared
189+
with other clients - this may cause errors in other clients!
190+
"""
191+
raise NotImplementedError()
192+
184193
@property
185194
def create_schema(
186195
self,

google/pubsub_v1/services/schema_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,5 +462,8 @@ def test_iam_permissions(
462462
)
463463
return self._stubs["test_iam_permissions"]
464464

465+
def close(self):
466+
self.grpc_channel.close()
467+
465468

466469
__all__ = ("SchemaServiceGrpcTransport",)

google/pubsub_v1/services/schema_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,5 +473,8 @@ def test_iam_permissions(
473473
)
474474
return self._stubs["test_iam_permissions"]
475475

476+
def close(self):
477+
return self.grpc_channel.close()
478+
476479

477480
__all__ = ("SchemaServiceGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)