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

Commit b54fb76

Browse files
feat: add context manager support in client (#285)
- [ ] 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 747b52c commit b54fb76

29 files changed

Lines changed: 397 additions & 16 deletions

File tree

google/cloud/dataproc_v1/services/autoscaling_policy_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@ async def delete_autoscaling_policy(
621621
request, retry=retry, timeout=timeout, metadata=metadata,
622622
)
623623

624+
async def __aenter__(self):
625+
return self
626+
627+
async def __aexit__(self, exc_type, exc, tb):
628+
await self.transport.close()
629+
624630

625631
try:
626632
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dataproc_v1/services/autoscaling_policy_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,7 @@ def __init__(
350350
client_cert_source_for_mtls=client_cert_source_func,
351351
quota_project_id=client_options.quota_project_id,
352352
client_info=client_info,
353-
always_use_jwt_access=(
354-
Transport == type(self).get_transport_class("grpc")
355-
or Transport == type(self).get_transport_class("grpc_asyncio")
356-
),
353+
always_use_jwt_access=True,
357354
)
358355

359356
def create_autoscaling_policy(
@@ -791,6 +788,19 @@ def delete_autoscaling_policy(
791788
request, retry=retry, timeout=timeout, metadata=metadata,
792789
)
793790

791+
def __enter__(self):
792+
return self
793+
794+
def __exit__(self, type, value, traceback):
795+
"""Releases underlying transport's resources.
796+
797+
.. warning::
798+
ONLY use as a context manager if the transport is NOT shared
799+
with other clients! Exiting the with block will CLOSE the transport
800+
and may cause errors in other clients!
801+
"""
802+
self.transport.close()
803+
794804

795805
try:
796806
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dataproc_v1/services/autoscaling_policy_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ def _prep_wrapped_messages(self, client_info):
210210
),
211211
}
212212

213+
def close(self):
214+
"""Closes resources associated with the transport.
215+
216+
.. warning::
217+
Only call this method if the transport is NOT shared
218+
with other clients - this may cause errors in other clients!
219+
"""
220+
raise NotImplementedError()
221+
213222
@property
214223
def create_autoscaling_policy(
215224
self,

google/cloud/dataproc_v1/services/autoscaling_policy_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,5 +376,8 @@ def delete_autoscaling_policy(
376376
)
377377
return self._stubs["delete_autoscaling_policy"]
378378

379+
def close(self):
380+
self.grpc_channel.close()
381+
379382

380383
__all__ = ("AutoscalingPolicyServiceGrpcTransport",)

google/cloud/dataproc_v1/services/autoscaling_policy_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,5 +380,8 @@ def delete_autoscaling_policy(
380380
)
381381
return self._stubs["delete_autoscaling_policy"]
382382

383+
def close(self):
384+
return self.grpc_channel.close()
385+
383386

384387
__all__ = ("AutoscalingPolicyServiceGrpcAsyncIOTransport",)

google/cloud/dataproc_v1/services/cluster_controller/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,12 @@ async def diagnose_cluster(
10151015
# Done; return the response.
10161016
return response
10171017

1018+
async def __aenter__(self):
1019+
return self
1020+
1021+
async def __aexit__(self, exc_type, exc, tb):
1022+
await self.transport.close()
1023+
10181024

10191025
try:
10201026
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dataproc_v1/services/cluster_controller/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,7 @@ def __init__(
369369
client_cert_source_for_mtls=client_cert_source_func,
370370
quota_project_id=client_options.quota_project_id,
371371
client_info=client_info,
372-
always_use_jwt_access=(
373-
Transport == type(self).get_transport_class("grpc")
374-
or Transport == type(self).get_transport_class("grpc_asyncio")
375-
),
372+
always_use_jwt_access=True,
376373
)
377374

378375
def create_cluster(
@@ -1164,6 +1161,19 @@ def diagnose_cluster(
11641161
# Done; return the response.
11651162
return response
11661163

1164+
def __enter__(self):
1165+
return self
1166+
1167+
def __exit__(self, type, value, traceback):
1168+
"""Releases underlying transport's resources.
1169+
1170+
.. warning::
1171+
ONLY use as a context manager if the transport is NOT shared
1172+
with other clients! Exiting the with block will CLOSE the transport
1173+
and may cause errors in other clients!
1174+
"""
1175+
self.transport.close()
1176+
11671177

11681178
try:
11691179
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dataproc_v1/services/cluster_controller/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ def _prep_wrapped_messages(self, client_info):
250250
),
251251
}
252252

253+
def close(self):
254+
"""Closes resources associated with the transport.
255+
256+
.. warning::
257+
Only call this method if the transport is NOT shared
258+
with other clients - this may cause errors in other clients!
259+
"""
260+
raise NotImplementedError()
261+
253262
@property
254263
def operations_client(self) -> operations_v1.OperationsClient:
255264
"""Return the client designed to process long-running operations."""

google/cloud/dataproc_v1/services/cluster_controller/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,5 +467,8 @@ def diagnose_cluster(
467467
)
468468
return self._stubs["diagnose_cluster"]
469469

470+
def close(self):
471+
self.grpc_channel.close()
472+
470473

471474
__all__ = ("ClusterControllerGrpcTransport",)

google/cloud/dataproc_v1/services/cluster_controller/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,5 +478,8 @@ def diagnose_cluster(
478478
)
479479
return self._stubs["diagnose_cluster"]
480480

481+
def close(self):
482+
return self.grpc_channel.close()
483+
481484

482485
__all__ = ("ClusterControllerGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)