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

Commit 3e68d78

Browse files
feat: add context manager support in client (#101)
- [ ] 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 78cb94d commit 3e68d78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+569
-32
lines changed

google/cloud/retail_v2/services/catalog_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,12 @@ async def get_default_branch(
529529
# Done; return the response.
530530
return response
531531

532+
async def __aenter__(self):
533+
return self
534+
535+
async def __aexit__(self, exc_type, exc, tb):
536+
await self.transport.close()
537+
532538

533539
try:
534540
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/retail_v2/services/catalog_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 list_catalogs(
@@ -732,6 +729,19 @@ def get_default_branch(
732729
# Done; return the response.
733730
return response
734731

732+
def __enter__(self):
733+
return self
734+
735+
def __exit__(self, type, value, traceback):
736+
"""Releases underlying transport's resources.
737+
738+
.. warning::
739+
ONLY use as a context manager if the transport is NOT shared
740+
with other clients! Exiting the with block will CLOSE the transport
741+
and may cause errors in other clients!
742+
"""
743+
self.transport.close()
744+
735745

736746
try:
737747
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/retail_v2/services/catalog_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ def _prep_wrapped_messages(self, client_info):
168168
),
169169
}
170170

171+
def close(self):
172+
"""Closes resources associated with the transport.
173+
174+
.. warning::
175+
Only call this method if the transport is NOT shared
176+
with other clients - this may cause errors in other clients!
177+
"""
178+
raise NotImplementedError()
179+
171180
@property
172181
def list_catalogs(
173182
self,

google/cloud/retail_v2/services/catalog_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,5 +383,8 @@ def get_default_branch(
383383
)
384384
return self._stubs["get_default_branch"]
385385

386+
def close(self):
387+
self.grpc_channel.close()
388+
386389

387390
__all__ = ("CatalogServiceGrpcTransport",)

google/cloud/retail_v2/services/catalog_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,5 +391,8 @@ def get_default_branch(
391391
)
392392
return self._stubs["get_default_branch"]
393393

394+
def close(self):
395+
return self.grpc_channel.close()
396+
394397

395398
__all__ = ("CatalogServiceGrpcAsyncIOTransport",)

google/cloud/retail_v2/services/completion_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ async def import_completion_data(
291291
# Done; return the response.
292292
return response
293293

294+
async def __aenter__(self):
295+
return self
296+
297+
async def __aexit__(self, exc_type, exc, tb):
298+
await self.transport.close()
299+
294300

295301
try:
296302
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/retail_v2/services/completion_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,7 @@ def __init__(
354354
client_cert_source_for_mtls=client_cert_source_func,
355355
quota_project_id=client_options.quota_project_id,
356356
client_info=client_info,
357-
always_use_jwt_access=(
358-
Transport == type(self).get_transport_class("grpc")
359-
or Transport == type(self).get_transport_class("grpc_asyncio")
360-
),
357+
always_use_jwt_access=True,
361358
)
362359

363360
def complete_query(
@@ -484,6 +481,19 @@ def import_completion_data(
484481
# Done; return the response.
485482
return response
486483

484+
def __enter__(self):
485+
return self
486+
487+
def __exit__(self, type, value, traceback):
488+
"""Releases underlying transport's resources.
489+
490+
.. warning::
491+
ONLY use as a context manager if the transport is NOT shared
492+
with other clients! Exiting the with block will CLOSE the transport
493+
and may cause errors in other clients!
494+
"""
495+
self.transport.close()
496+
487497

488498
try:
489499
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/retail_v2/services/completion_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ def _prep_wrapped_messages(self, client_info):
165165
),
166166
}
167167

168+
def close(self):
169+
"""Closes resources associated with the transport.
170+
171+
.. warning::
172+
Only call this method if the transport is NOT shared
173+
with other clients - this may cause errors in other clients!
174+
"""
175+
raise NotImplementedError()
176+
168177
@property
169178
def operations_client(self) -> operations_v1.OperationsClient:
170179
"""Return the client designed to process long-running operations."""

google/cloud/retail_v2/services/completion_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,8 @@ def import_completion_data(
318318
)
319319
return self._stubs["import_completion_data"]
320320

321+
def close(self):
322+
self.grpc_channel.close()
323+
321324

322325
__all__ = ("CompletionServiceGrpcTransport",)

google/cloud/retail_v2/services/completion_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,8 @@ def import_completion_data(
323323
)
324324
return self._stubs["import_completion_data"]
325325

326+
def close(self):
327+
return self.grpc_channel.close()
328+
326329

327330
__all__ = ("CompletionServiceGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)