Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit 629365f

Browse files
feat: add context manager support in client (#247)
- [ ] 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 ca56a71 commit 629365f

File tree

75 files changed

+937
-32
lines changed

Some content is hidden

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

75 files changed

+937
-32
lines changed

google/cloud/vision_v1/services/image_annotator/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ async def async_batch_annotate_files(
525525
# Done; return the response.
526526
return response
527527

528+
async def __aenter__(self):
529+
return self
530+
531+
async def __aexit__(self, exc_type, exc, tb):
532+
await self.transport.close()
533+
528534

529535
try:
530536
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/vision_v1/services/image_annotator/client.py

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

374371
def batch_annotate_images(
@@ -704,6 +701,19 @@ def async_batch_annotate_files(
704701
# Done; return the response.
705702
return response
706703

704+
def __enter__(self):
705+
return self
706+
707+
def __exit__(self, type, value, traceback):
708+
"""Releases underlying transport's resources.
709+
710+
.. warning::
711+
ONLY use as a context manager if the transport is NOT shared
712+
with other clients! Exiting the with block will CLOSE the transport
713+
and may cause errors in other clients!
714+
"""
715+
self.transport.close()
716+
707717

708718
try:
709719
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/vision_v1/services/image_annotator/transports/base.py

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

222+
def close(self):
223+
"""Closes resources associated with the transport.
224+
225+
.. warning::
226+
Only call this method if the transport is NOT shared
227+
with other clients - this may cause errors in other clients!
228+
"""
229+
raise NotImplementedError()
230+
222231
@property
223232
def operations_client(self) -> operations_v1.OperationsClient:
224233
"""Return the client designed to process long-running operations."""

google/cloud/vision_v1/services/image_annotator/transports/grpc.py

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

387+
def close(self):
388+
self.grpc_channel.close()
389+
387390

388391
__all__ = ("ImageAnnotatorGrpcTransport",)

google/cloud/vision_v1/services/image_annotator/transports/grpc_asyncio.py

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

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

395398
__all__ = ("ImageAnnotatorGrpcAsyncIOTransport",)

google/cloud/vision_v1/services/product_search/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,6 +2080,12 @@ async def purge_products(
20802080
# Done; return the response.
20812081
return response
20822082

2083+
async def __aenter__(self):
2084+
return self
2085+
2086+
async def __aexit__(self, exc_type, exc, tb):
2087+
await self.transport.close()
2088+
20832089

20842090
try:
20852091
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/vision_v1/services/product_search/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,7 @@ def __init__(
404404
client_cert_source_for_mtls=client_cert_source_func,
405405
quota_project_id=client_options.quota_project_id,
406406
client_info=client_info,
407-
always_use_jwt_access=(
408-
Transport == type(self).get_transport_class("grpc")
409-
or Transport == type(self).get_transport_class("grpc_asyncio")
410-
),
407+
always_use_jwt_access=True,
411408
)
412409

413410
def create_product_set(
@@ -2141,6 +2138,19 @@ def purge_products(
21412138
# Done; return the response.
21422139
return response
21432140

2141+
def __enter__(self):
2142+
return self
2143+
2144+
def __exit__(self, type, value, traceback):
2145+
"""Releases underlying transport's resources.
2146+
2147+
.. warning::
2148+
ONLY use as a context manager if the transport is NOT shared
2149+
with other clients! Exiting the with block will CLOSE the transport
2150+
and may cause errors in other clients!
2151+
"""
2152+
self.transport.close()
2153+
21442154

21452155
try:
21462156
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/vision_v1/services/product_search/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ def _prep_wrapped_messages(self, client_info):
430430
),
431431
}
432432

433+
def close(self):
434+
"""Closes resources associated with the transport.
435+
436+
.. warning::
437+
Only call this method if the transport is NOT shared
438+
with other clients - this may cause errors in other clients!
439+
"""
440+
raise NotImplementedError()
441+
433442
@property
434443
def operations_client(self) -> operations_v1.OperationsClient:
435444
"""Return the client designed to process long-running operations."""

google/cloud/vision_v1/services/product_search/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,5 +951,8 @@ def purge_products(
951951
)
952952
return self._stubs["purge_products"]
953953

954+
def close(self):
955+
self.grpc_channel.close()
956+
954957

955958
__all__ = ("ProductSearchGrpcTransport",)

google/cloud/vision_v1/services/product_search/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,5 +968,8 @@ def purge_products(
968968
)
969969
return self._stubs["purge_products"]
970970

971+
def close(self):
972+
return self.grpc_channel.close()
973+
971974

972975
__all__ = ("ProductSearchGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)