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

Commit 59229eb

Browse files
feat: add context manager support in client (#122)
- [ ] 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 2fa295c commit 59229eb

24 files changed

Lines changed: 340 additions & 16 deletions

File tree

google/cloud/servicedirectory_v1/services/lookup_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ async def resolve_service(
216216
# Done; return the response.
217217
return response
218218

219+
async def __aenter__(self):
220+
return self
221+
222+
async def __aexit__(self, exc_type, exc, tb):
223+
await self.transport.close()
224+
219225

220226
try:
221227
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/servicedirectory_v1/services/lookup_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,7 @@ def __init__(
364364
client_cert_source_for_mtls=client_cert_source_func,
365365
quota_project_id=client_options.quota_project_id,
366366
client_info=client_info,
367-
always_use_jwt_access=(
368-
Transport == type(self).get_transport_class("grpc")
369-
or Transport == type(self).get_transport_class("grpc_asyncio")
370-
),
367+
always_use_jwt_access=True,
371368
)
372369

373370
def resolve_service(
@@ -424,6 +421,19 @@ def resolve_service(
424421
# Done; return the response.
425422
return response
426423

424+
def __enter__(self):
425+
return self
426+
427+
def __exit__(self, type, value, traceback):
428+
"""Releases underlying transport's resources.
429+
430+
.. warning::
431+
ONLY use as a context manager if the transport is NOT shared
432+
with other clients! Exiting the with block will CLOSE the transport
433+
and may cause errors in other clients!
434+
"""
435+
self.transport.close()
436+
427437

428438
try:
429439
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/servicedirectory_v1/services/lookup_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ def _prep_wrapped_messages(self, client_info):
159159
),
160160
}
161161

162+
def close(self):
163+
"""Closes resources associated with the transport.
164+
165+
.. warning::
166+
Only call this method if the transport is NOT shared
167+
with other clients - this may cause errors in other clients!
168+
"""
169+
raise NotImplementedError()
170+
162171
@property
163172
def resolve_service(
164173
self,

google/cloud/servicedirectory_v1/services/lookup_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,8 @@ def resolve_service(
255255
)
256256
return self._stubs["resolve_service"]
257257

258+
def close(self):
259+
self.grpc_channel.close()
260+
258261

259262
__all__ = ("LookupServiceGrpcTransport",)

google/cloud/servicedirectory_v1/services/lookup_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,8 @@ def resolve_service(
259259
)
260260
return self._stubs["resolve_service"]
261261

262+
def close(self):
263+
return self.grpc_channel.close()
264+
262265

263266
__all__ = ("LookupServiceGrpcAsyncIOTransport",)

google/cloud/servicedirectory_v1/services/registration_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,12 @@ async def test_iam_permissions(
16581658
# Done; return the response.
16591659
return response
16601660

1661+
async def __aenter__(self):
1662+
return self
1663+
1664+
async def __aexit__(self, exc_type, exc, tb):
1665+
await self.transport.close()
1666+
16611667

16621668
try:
16631669
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/servicedirectory_v1/services/registration_service/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_namespace(
@@ -1879,6 +1876,19 @@ def test_iam_permissions(
18791876
# Done; return the response.
18801877
return response
18811878

1879+
def __enter__(self):
1880+
return self
1881+
1882+
def __exit__(self, type, value, traceback):
1883+
"""Releases underlying transport's resources.
1884+
1885+
.. warning::
1886+
ONLY use as a context manager if the transport is NOT shared
1887+
with other clients! Exiting the with block will CLOSE the transport
1888+
and may cause errors in other clients!
1889+
"""
1890+
self.transport.close()
1891+
18821892

18831893
try:
18841894
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/servicedirectory_v1/services/registration_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ def _prep_wrapped_messages(self, client_info):
221221
),
222222
}
223223

224+
def close(self):
225+
"""Closes resources associated with the transport.
226+
227+
.. warning::
228+
Only call this method if the transport is NOT shared
229+
with other clients - this may cause errors in other clients!
230+
"""
231+
raise NotImplementedError()
232+
224233
@property
225234
def create_namespace(
226235
self,

google/cloud/servicedirectory_v1/services/registration_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,5 +737,8 @@ def test_iam_permissions(
737737
)
738738
return self._stubs["test_iam_permissions"]
739739

740+
def close(self):
741+
self.grpc_channel.close()
742+
740743

741744
__all__ = ("RegistrationServiceGrpcTransport",)

google/cloud/servicedirectory_v1/services/registration_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,5 +760,8 @@ def test_iam_permissions(
760760
)
761761
return self._stubs["test_iam_permissions"]
762762

763+
def close(self):
764+
return self.grpc_channel.close()
765+
763766

764767
__all__ = ("RegistrationServiceGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)