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

Commit 7ac092c

Browse files
feat: add context manager support in client (#184)
- [ ] 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 638ef07 commit 7ac092c

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

google/cloud/devtools/cloudbuild_v1/services/cloud_build/async_client.py

+6
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,12 @@ async def list_worker_pools(
19041904
# Done; return the response.
19051905
return response
19061906

1907+
async def __aenter__(self):
1908+
return self
1909+
1910+
async def __aexit__(self, exc_type, exc, tb):
1911+
await self.transport.close()
1912+
19071913

19081914
try:
19091915
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/devtools/cloudbuild_v1/services/cloud_build/client.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,7 @@ def __init__(
469469
client_cert_source_for_mtls=client_cert_source_func,
470470
quota_project_id=client_options.quota_project_id,
471471
client_info=client_info,
472-
always_use_jwt_access=(
473-
Transport == type(self).get_transport_class("grpc")
474-
or Transport == type(self).get_transport_class("grpc_asyncio")
475-
),
472+
always_use_jwt_access=True,
476473
)
477474

478475
def create_build(
@@ -2123,6 +2120,19 @@ def list_worker_pools(
21232120
# Done; return the response.
21242121
return response
21252122

2123+
def __enter__(self):
2124+
return self
2125+
2126+
def __exit__(self, type, value, traceback):
2127+
"""Releases underlying transport's resources.
2128+
2129+
.. warning::
2130+
ONLY use as a context manager if the transport is NOT shared
2131+
with other clients! Exiting the with block will CLOSE the transport
2132+
and may cause errors in other clients!
2133+
"""
2134+
self.transport.close()
2135+
21262136

21272137
try:
21282138
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/devtools/cloudbuild_v1/services/cloud_build/transports/base.py

+9
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ def _prep_wrapped_messages(self, client_info):
303303
),
304304
}
305305

306+
def close(self):
307+
"""Closes resources associated with the transport.
308+
309+
.. warning::
310+
Only call this method if the transport is NOT shared
311+
with other clients - this may cause errors in other clients!
312+
"""
313+
raise NotImplementedError()
314+
306315
@property
307316
def operations_client(self) -> operations_v1.OperationsClient:
308317
"""Return the client designed to process long-running operations."""

google/cloud/devtools/cloudbuild_v1/services/cloud_build/transports/grpc.py

+3
Original file line numberDiff line numberDiff line change
@@ -777,5 +777,8 @@ def list_worker_pools(
777777
)
778778
return self._stubs["list_worker_pools"]
779779

780+
def close(self):
781+
self.grpc_channel.close()
782+
780783

781784
__all__ = ("CloudBuildGrpcTransport",)

google/cloud/devtools/cloudbuild_v1/services/cloud_build/transports/grpc_asyncio.py

+3
Original file line numberDiff line numberDiff line change
@@ -804,5 +804,8 @@ def list_worker_pools(
804804
)
805805
return self._stubs["list_worker_pools"]
806806

807+
def close(self):
808+
return self.grpc_channel.close()
809+
807810

808811
__all__ = ("CloudBuildGrpcAsyncIOTransport",)

google/cloud/devtools/cloudbuild_v1/types/cloudbuild.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,8 @@ class ReceiveTriggerWebhookRequest(proto.Message):
17391739
class ReceiveTriggerWebhookResponse(proto.Message):
17401740
r"""ReceiveTriggerWebhookResponse [Experimental] is the response object
17411741
for the ReceiveTriggerWebhook method.
1742-
"""
1742+
1743+
"""
17431744

17441745

17451746
class WorkerPool(proto.Message):
@@ -1852,6 +1853,7 @@ class WorkerConfig(proto.Message):
18521853

18531854
class NetworkConfig(proto.Message):
18541855
r"""Defines the network configuration for the pool.
1856+
18551857
Attributes:
18561858
peered_network (str):
18571859
Required. Immutable. The network definition that the workers

tests/unit/gapic/cloudbuild_v1/test_cloud_build.py

+50
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from google.api_core import grpc_helpers_async
3434
from google.api_core import operation_async # type: ignore
3535
from google.api_core import operations_v1
36+
from google.api_core import path_template
3637
from google.auth import credentials as ga_credentials
3738
from google.auth.exceptions import MutualTLSChannelError
3839
from google.cloud.devtools.cloudbuild_v1.services.cloud_build import (
@@ -4386,6 +4387,9 @@ def test_cloud_build_base_transport():
43864387
with pytest.raises(NotImplementedError):
43874388
getattr(transport, method)(request=object())
43884389

4390+
with pytest.raises(NotImplementedError):
4391+
transport.close()
4392+
43894393
# Additionally, the LRO client (a property) should
43904394
# also raise NotImplementedError
43914395
with pytest.raises(NotImplementedError):
@@ -5060,3 +5064,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
50605064
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
50615065
)
50625066
prep.assert_called_once_with(client_info)
5067+
5068+
5069+
@pytest.mark.asyncio
5070+
async def test_transport_close_async():
5071+
client = CloudBuildAsyncClient(
5072+
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
5073+
)
5074+
with mock.patch.object(
5075+
type(getattr(client.transport, "grpc_channel")), "close"
5076+
) as close:
5077+
async with client:
5078+
close.assert_not_called()
5079+
close.assert_called_once()
5080+
5081+
5082+
def test_transport_close():
5083+
transports = {
5084+
"grpc": "_grpc_channel",
5085+
}
5086+
5087+
for transport, close_name in transports.items():
5088+
client = CloudBuildClient(
5089+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
5090+
)
5091+
with mock.patch.object(
5092+
type(getattr(client.transport, close_name)), "close"
5093+
) as close:
5094+
with client:
5095+
close.assert_not_called()
5096+
close.assert_called_once()
5097+
5098+
5099+
def test_client_ctx():
5100+
transports = [
5101+
"grpc",
5102+
]
5103+
for transport in transports:
5104+
client = CloudBuildClient(
5105+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
5106+
)
5107+
# Test client calls underlying transport.
5108+
with mock.patch.object(type(client.transport), "close") as close:
5109+
close.assert_not_called()
5110+
with client:
5111+
pass
5112+
close.assert_called()

0 commit comments

Comments
 (0)