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

Commit ceec8f1

Browse files
feat: add context manager support in client (#173)
- [ ] 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 61a5857 commit ceec8f1

File tree

24 files changed

+266
-13
lines changed

24 files changed

+266
-13
lines changed

google/cloud/tasks_v2/services/cloud_tasks/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,12 @@ async def run_task(
17551755
# Done; return the response.
17561756
return response
17571757

1758+
async def __aenter__(self):
1759+
return self
1760+
1761+
async def __aexit__(self, exc_type, exc, tb):
1762+
await self.transport.close()
1763+
17581764

17591765
try:
17601766
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/tasks_v2/services/cloud_tasks/client.py

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

379376
def list_queues(
@@ -1879,6 +1876,19 @@ def run_task(
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/tasks_v2/services/cloud_tasks/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,15 @@ def _prep_wrapped_messages(self, client_info):
305305
),
306306
}
307307

308+
def close(self):
309+
"""Closes resources associated with the transport.
310+
311+
.. warning::
312+
Only call this method if the transport is NOT shared
313+
with other clients - this may cause errors in other clients!
314+
"""
315+
raise NotImplementedError()
316+
308317
@property
309318
def list_queues(
310319
self,

google/cloud/tasks_v2/services/cloud_tasks/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,5 +768,8 @@ def run_task(self) -> Callable[[cloudtasks.RunTaskRequest], task.Task]:
768768
)
769769
return self._stubs["run_task"]
770770

771+
def close(self):
772+
self.grpc_channel.close()
773+
771774

772775
__all__ = ("CloudTasksGrpcTransport",)

google/cloud/tasks_v2/services/cloud_tasks/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,5 +787,8 @@ def run_task(self) -> Callable[[cloudtasks.RunTaskRequest], Awaitable[task.Task]
787787
)
788788
return self._stubs["run_task"]
789789

790+
def close(self):
791+
return self.grpc_channel.close()
792+
790793

791794
__all__ = ("CloudTasksGrpcAsyncIOTransport",)

google/cloud/tasks_v2/types/task.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
class Task(proto.Message):
3030
r"""A unit of scheduled work.
31+
3132
Attributes:
3233
name (str):
3334
Optionally caller-specified in
@@ -177,6 +178,7 @@ class View(proto.Enum):
177178

178179
class Attempt(proto.Message):
179180
r"""The status of a task attempt.
181+
180182
Attributes:
181183
schedule_time (google.protobuf.timestamp_pb2.Timestamp):
182184
Output only. The time that this attempt was scheduled.

google/cloud/tasks_v2beta2/services/cloud_tasks/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,12 @@ async def run_task(
21892189
# Done; return the response.
21902190
return response
21912191

2192+
async def __aenter__(self):
2193+
return self
2194+
2195+
async def __aexit__(self, exc_type, exc, tb):
2196+
await self.transport.close()
2197+
21922198

21932199
try:
21942200
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/tasks_v2beta2/services/cloud_tasks/client.py

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

379376
def list_queues(
@@ -2313,6 +2310,19 @@ def run_task(
23132310
# Done; return the response.
23142311
return response
23152312

2313+
def __enter__(self):
2314+
return self
2315+
2316+
def __exit__(self, type, value, traceback):
2317+
"""Releases underlying transport's resources.
2318+
2319+
.. warning::
2320+
ONLY use as a context manager if the transport is NOT shared
2321+
with other clients! Exiting the with block will CLOSE the transport
2322+
and may cause errors in other clients!
2323+
"""
2324+
self.transport.close()
2325+
23162326

23172327
try:
23182328
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/tasks_v2beta2/services/cloud_tasks/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ def _prep_wrapped_messages(self, client_info):
317317
),
318318
}
319319

320+
def close(self):
321+
"""Closes resources associated with the transport.
322+
323+
.. warning::
324+
Only call this method if the transport is NOT shared
325+
with other clients - this may cause errors in other clients!
326+
"""
327+
raise NotImplementedError()
328+
320329
@property
321330
def list_queues(
322331
self,

google/cloud/tasks_v2beta2/services/cloud_tasks/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,5 +927,8 @@ def run_task(self) -> Callable[[cloudtasks.RunTaskRequest], task.Task]:
927927
)
928928
return self._stubs["run_task"]
929929

930+
def close(self):
931+
self.grpc_channel.close()
932+
930933

931934
__all__ = ("CloudTasksGrpcTransport",)

0 commit comments

Comments
 (0)