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

Commit 91201e0

Browse files
feat: add context manager support in client (#49)
- [ ] 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 024620f commit 91201e0

File tree

7 files changed

+91
-4
lines changed

7 files changed

+91
-4
lines changed

google/cloud/resourcesettings_v1/services/resource_settings_service/async_client.py

+6
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,12 @@ async def update_setting(
428428
# Done; return the response.
429429
return response
430430

431+
async def __aenter__(self):
432+
return self
433+
434+
async def __aexit__(self, exc_type, exc, tb):
435+
await self.transport.close()
436+
431437

432438
try:
433439
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/resourcesettings_v1/services/resource_settings_service/client.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,7 @@ def __init__(
358358
client_cert_source_for_mtls=client_cert_source_func,
359359
quota_project_id=client_options.quota_project_id,
360360
client_info=client_info,
361-
always_use_jwt_access=(
362-
Transport == type(self).get_transport_class("grpc")
363-
or Transport == type(self).get_transport_class("grpc_asyncio")
364-
),
361+
always_use_jwt_access=True,
365362
)
366363

367364
def list_settings(
@@ -584,6 +581,19 @@ def update_setting(
584581
# Done; return the response.
585582
return response
586583

584+
def __enter__(self):
585+
return self
586+
587+
def __exit__(self, type, value, traceback):
588+
"""Releases underlying transport's resources.
589+
590+
.. warning::
591+
ONLY use as a context manager if the transport is NOT shared
592+
with other clients! Exiting the with block will CLOSE the transport
593+
and may cause errors in other clients!
594+
"""
595+
self.transport.close()
596+
587597

588598
try:
589599
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/resourcesettings_v1/services/resource_settings_service/transports/base.py

+9
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ def _prep_wrapped_messages(self, client_info):
201201
),
202202
}
203203

204+
def close(self):
205+
"""Closes resources associated with the transport.
206+
207+
.. warning::
208+
Only call this method if the transport is NOT shared
209+
with other clients - this may cause errors in other clients!
210+
"""
211+
raise NotImplementedError()
212+
204213
@property
205214
def list_settings(
206215
self,

google/cloud/resourcesettings_v1/services/resource_settings_service/transports/grpc.py

+3
Original file line numberDiff line numberDiff line change
@@ -336,5 +336,8 @@ def update_setting(
336336
)
337337
return self._stubs["update_setting"]
338338

339+
def close(self):
340+
self.grpc_channel.close()
341+
339342

340343
__all__ = ("ResourceSettingsServiceGrpcTransport",)

google/cloud/resourcesettings_v1/services/resource_settings_service/transports/grpc_asyncio.py

+3
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,8 @@ def update_setting(
344344
)
345345
return self._stubs["update_setting"]
346346

347+
def close(self):
348+
return self.grpc_channel.close()
349+
347350

348351
__all__ = ("ResourceSettingsServiceGrpcAsyncIOTransport",)

google/cloud/resourcesettings_v1/types/resource_settings.py

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class SettingView(proto.Enum):
4141

4242
class Setting(proto.Message):
4343
r"""The schema for settings.
44+
4445
Attributes:
4546
name (str):
4647
The resource name of the setting. Must be in one of the
@@ -139,6 +140,7 @@ class DataType(proto.Enum):
139140

140141
class Value(proto.Message):
141142
r"""The data in a setting value.
143+
142144
Attributes:
143145
boolean_value (bool):
144146
Defines this value as being a boolean value.
@@ -184,6 +186,7 @@ class EnumValue(proto.Message):
184186

185187
class ListSettingsRequest(proto.Message):
186188
r"""The request for ListSettings.
189+
187190
Attributes:
188191
parent (str):
189192
Required. The Cloud resource that parents the setting. Must
@@ -210,6 +213,7 @@ class ListSettingsRequest(proto.Message):
210213

211214
class ListSettingsResponse(proto.Message):
212215
r"""The response from ListSettings.
216+
213217
Attributes:
214218
settings (Sequence[google.cloud.resourcesettings_v1.types.Setting]):
215219
A list of settings that are available at the
@@ -229,6 +233,7 @@ def raw_page(self):
229233

230234
class GetSettingRequest(proto.Message):
231235
r"""The request for GetSetting.
236+
232237
Attributes:
233238
name (str):
234239
Required. The name of the setting to get. See
@@ -244,6 +249,7 @@ class GetSettingRequest(proto.Message):
244249

245250
class UpdateSettingRequest(proto.Message):
246251
r"""The request for UpdateSetting.
252+
247253
Attributes:
248254
setting (google.cloud.resourcesettings_v1.types.Setting):
249255
Required. The setting to update. See

tests/unit/gapic/resourcesettings_v1/test_resource_settings_service.py

+50
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from google.api_core import gapic_v1
3030
from google.api_core import grpc_helpers
3131
from google.api_core import grpc_helpers_async
32+
from google.api_core import path_template
3233
from google.auth import credentials as ga_credentials
3334
from google.auth.exceptions import MutualTLSChannelError
3435
from google.cloud.resourcesettings_v1.services.resource_settings_service import (
@@ -1346,6 +1347,9 @@ def test_resource_settings_service_base_transport():
13461347
with pytest.raises(NotImplementedError):
13471348
getattr(transport, method)(request=object())
13481349

1350+
with pytest.raises(NotImplementedError):
1351+
transport.close()
1352+
13491353

13501354
@requires_google_auth_gte_1_25_0
13511355
def test_resource_settings_service_base_transport_with_credentials_file():
@@ -1830,3 +1834,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
18301834
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
18311835
)
18321836
prep.assert_called_once_with(client_info)
1837+
1838+
1839+
@pytest.mark.asyncio
1840+
async def test_transport_close_async():
1841+
client = ResourceSettingsServiceAsyncClient(
1842+
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
1843+
)
1844+
with mock.patch.object(
1845+
type(getattr(client.transport, "grpc_channel")), "close"
1846+
) as close:
1847+
async with client:
1848+
close.assert_not_called()
1849+
close.assert_called_once()
1850+
1851+
1852+
def test_transport_close():
1853+
transports = {
1854+
"grpc": "_grpc_channel",
1855+
}
1856+
1857+
for transport, close_name in transports.items():
1858+
client = ResourceSettingsServiceClient(
1859+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
1860+
)
1861+
with mock.patch.object(
1862+
type(getattr(client.transport, close_name)), "close"
1863+
) as close:
1864+
with client:
1865+
close.assert_not_called()
1866+
close.assert_called_once()
1867+
1868+
1869+
def test_client_ctx():
1870+
transports = [
1871+
"grpc",
1872+
]
1873+
for transport in transports:
1874+
client = ResourceSettingsServiceClient(
1875+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
1876+
)
1877+
# Test client calls underlying transport.
1878+
with mock.patch.object(type(client.transport), "close") as close:
1879+
close.assert_not_called()
1880+
with client:
1881+
pass
1882+
close.assert_called()

0 commit comments

Comments
 (0)