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

Commit 83b7a3d

Browse files
feat: add context manager support in client (#264)
- [ ] 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 73c26ea commit 83b7a3d

Some content is hidden

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

57 files changed

+479
-38
lines changed

google/cloud/automl_v1/services/auto_ml/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,12 @@ async def list_model_evaluations(
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/automl_v1/services/auto_ml/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,7 @@ def __init__(
435435
client_cert_source_for_mtls=client_cert_source_func,
436436
quota_project_id=client_options.quota_project_id,
437437
client_info=client_info,
438-
always_use_jwt_access=(
439-
Transport == type(self).get_transport_class("grpc")
440-
or Transport == type(self).get_transport_class("grpc_asyncio")
441-
),
438+
always_use_jwt_access=True,
442439
)
443440

444441
def create_dataset(
@@ -2063,6 +2060,19 @@ def list_model_evaluations(
20632060
# Done; return the response.
20642061
return response
20652062

2063+
def __enter__(self):
2064+
return self
2065+
2066+
def __exit__(self, type, value, traceback):
2067+
"""Releases underlying transport's resources.
2068+
2069+
.. warning::
2070+
ONLY use as a context manager if the transport is NOT shared
2071+
with other clients! Exiting the with block will CLOSE the transport
2072+
and may cause errors in other clients!
2073+
"""
2074+
self.transport.close()
2075+
20662076

20672077
try:
20682078
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/automl_v1/services/auto_ml/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ def _prep_wrapped_messages(self, client_info):
324324
),
325325
}
326326

327+
def close(self):
328+
"""Closes resources associated with the transport.
329+
330+
.. warning::
331+
Only call this method if the transport is NOT shared
332+
with other clients - this may cause errors in other clients!
333+
"""
334+
raise NotImplementedError()
335+
327336
@property
328337
def operations_client(self) -> operations_v1.OperationsClient:
329338
"""Return the client designed to process long-running operations."""

google/cloud/automl_v1/services/auto_ml/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,5 +780,8 @@ def list_model_evaluations(
780780
)
781781
return self._stubs["list_model_evaluations"]
782782

783+
def close(self):
784+
self.grpc_channel.close()
785+
783786

784787
__all__ = ("AutoMlGrpcTransport",)

google/cloud/automl_v1/services/auto_ml/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,5 +794,8 @@ def list_model_evaluations(
794794
)
795795
return self._stubs["list_model_evaluations"]
796796

797+
def close(self):
798+
return self.grpc_channel.close()
799+
797800

798801
__all__ = ("AutoMlGrpcAsyncIOTransport",)

google/cloud/automl_v1/services/prediction_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ async def batch_predict(
544544
# Done; return the response.
545545
return response
546546

547+
async def __aenter__(self):
548+
return self
549+
550+
async def __aexit__(self, exc_type, exc, tb):
551+
await self.transport.close()
552+
547553

548554
try:
549555
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/automl_v1/services/prediction_service/client.py

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

364361
def predict(
@@ -733,6 +730,19 @@ def batch_predict(
733730
# Done; return the response.
734731
return response
735732

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

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

google/cloud/automl_v1/services/prediction_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ def _prep_wrapped_messages(self, client_info):
162162
),
163163
}
164164

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

google/cloud/automl_v1/services/prediction_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,5 +354,8 @@ def batch_predict(
354354
)
355355
return self._stubs["batch_predict"]
356356

357+
def close(self):
358+
self.grpc_channel.close()
359+
357360

358361
__all__ = ("PredictionServiceGrpcTransport",)

google/cloud/automl_v1/services/prediction_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,8 @@ def batch_predict(
362362
)
363363
return self._stubs["batch_predict"]
364364

365+
def close(self):
366+
return self.grpc_channel.close()
367+
365368

366369
__all__ = ("PredictionServiceGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)