Skip to content

Commit f5af164

Browse files
feat: add context manager support in client (#415)
* feat: add context manager support in client 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 * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 82e5dbf commit f5af164

File tree

22 files changed

+303
-13
lines changed

22 files changed

+303
-13
lines changed

google/cloud/logging_v2/services/config_service_v2/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,12 @@ async def update_cmek_settings(
19291929
# Done; return the response.
19301930
return response
19311931

1932+
async def __aenter__(self):
1933+
return self
1934+
1935+
async def __aexit__(self, exc_type, exc, tb):
1936+
await self.transport.close()
1937+
19321938

19331939
try:
19341940
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/logging_v2/services/config_service_v2/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,7 @@ def __init__(
397397
client_cert_source_for_mtls=client_cert_source_func,
398398
quota_project_id=client_options.quota_project_id,
399399
client_info=client_info,
400-
always_use_jwt_access=(
401-
Transport == type(self).get_transport_class("grpc")
402-
or Transport == type(self).get_transport_class("grpc_asyncio")
403-
),
400+
always_use_jwt_access=True,
404401
)
405402

406403
def list_buckets(
@@ -2091,6 +2088,19 @@ def update_cmek_settings(
20912088
# Done; return the response.
20922089
return response
20932090

2091+
def __enter__(self):
2092+
return self
2093+
2094+
def __exit__(self, type, value, traceback):
2095+
"""Releases underlying transport's resources.
2096+
2097+
.. warning::
2098+
ONLY use as a context manager if the transport is NOT shared
2099+
with other clients! Exiting the with block will CLOSE the transport
2100+
and may cause errors in other clients!
2101+
"""
2102+
self.transport.close()
2103+
20942104

20952105
try:
20962106
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/logging_v2/services/config_service_v2/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ def _prep_wrapped_messages(self, client_info):
322322
),
323323
}
324324

325+
def close(self):
326+
"""Closes resources associated with the transport.
327+
328+
.. warning::
329+
Only call this method if the transport is NOT shared
330+
with other clients - this may cause errors in other clients!
331+
"""
332+
raise NotImplementedError()
333+
325334
@property
326335
def list_buckets(
327336
self,

google/cloud/logging_v2/services/config_service_v2/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,5 +883,8 @@ def update_cmek_settings(
883883
)
884884
return self._stubs["update_cmek_settings"]
885885

886+
def close(self):
887+
self.grpc_channel.close()
888+
886889

887890
__all__ = ("ConfigServiceV2GrpcTransport",)

google/cloud/logging_v2/services/config_service_v2/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,5 +915,8 @@ def update_cmek_settings(
915915
)
916916
return self._stubs["update_cmek_settings"]
917917

918+
def close(self):
919+
return self.grpc_channel.close()
920+
918921

919922
__all__ = ("ConfigServiceV2GrpcAsyncIOTransport",)

google/cloud/logging_v2/services/logging_service_v2/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,12 @@ def tail_log_entries(
779779
# Done; return the response.
780780
return response
781781

782+
async def __aenter__(self):
783+
return self
784+
785+
async def __aexit__(self, exc_type, exc, tb):
786+
await self.transport.close()
787+
782788

783789
try:
784790
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/logging_v2/services/logging_service_v2/client.py

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

350347
def delete_log(
@@ -886,6 +883,19 @@ def tail_log_entries(
886883
# Done; return the response.
887884
return response
888885

886+
def __enter__(self):
887+
return self
888+
889+
def __exit__(self, type, value, traceback):
890+
"""Releases underlying transport's resources.
891+
892+
.. warning::
893+
ONLY use as a context manager if the transport is NOT shared
894+
with other clients! Exiting the with block will CLOSE the transport
895+
and may cause errors in other clients!
896+
"""
897+
self.transport.close()
898+
889899

890900
try:
891901
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/logging_v2/services/logging_service_v2/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ def _prep_wrapped_messages(self, client_info):
257257
),
258258
}
259259

260+
def close(self):
261+
"""Closes resources associated with the transport.
262+
263+
.. warning::
264+
Only call this method if the transport is NOT shared
265+
with other clients - this may cause errors in other clients!
266+
"""
267+
raise NotImplementedError()
268+
260269
@property
261270
def delete_log(
262271
self,

google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,5 +404,8 @@ def tail_log_entries(
404404
)
405405
return self._stubs["tail_log_entries"]
406406

407+
def close(self):
408+
self.grpc_channel.close()
409+
407410

408411
__all__ = ("LoggingServiceV2GrpcTransport",)

google/cloud/logging_v2/services/logging_service_v2/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,5 +415,8 @@ def tail_log_entries(
415415
)
416416
return self._stubs["tail_log_entries"]
417417

418+
def close(self):
419+
return self.grpc_channel.close()
420+
418421

419422
__all__ = ("LoggingServiceV2GrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)