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

Commit 2d8fbd5

Browse files
feat: add context manager support in client (#41)
- [ ] 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 9e9d4f0 commit 2d8fbd5

Some content is hidden

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

43 files changed

+573
-25
lines changed

google/cloud/dataflow_v1beta3/services/flex_templates_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ async def launch_flex_template(
205205
# Done; return the response.
206206
return response
207207

208+
async def __aenter__(self):
209+
return self
210+
211+
async def __aexit__(self, exc_type, exc, tb):
212+
await self.transport.close()
213+
208214

209215
try:
210216
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dataflow_v1beta3/services/flex_templates_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ def __init__(
332332
client_cert_source_for_mtls=client_cert_source_func,
333333
quota_project_id=client_options.quota_project_id,
334334
client_info=client_info,
335-
always_use_jwt_access=(
336-
Transport == type(self).get_transport_class("grpc")
337-
or Transport == type(self).get_transport_class("grpc_asyncio")
338-
),
335+
always_use_jwt_access=True,
339336
)
340337

341338
def launch_flex_template(
@@ -382,6 +379,19 @@ def launch_flex_template(
382379
# Done; return the response.
383380
return response
384381

382+
def __enter__(self):
383+
return self
384+
385+
def __exit__(self, type, value, traceback):
386+
"""Releases underlying transport's resources.
387+
388+
.. warning::
389+
ONLY use as a context manager if the transport is NOT shared
390+
with other clients! Exiting the with block will CLOSE the transport
391+
and may cause errors in other clients!
392+
"""
393+
self.transport.close()
394+
385395

386396
try:
387397
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dataflow_v1beta3/services/flex_templates_service/transports/base.py

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

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

google/cloud/dataflow_v1beta3/services/flex_templates_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,5 +254,8 @@ def launch_flex_template(
254254
)
255255
return self._stubs["launch_flex_template"]
256256

257+
def close(self):
258+
self.grpc_channel.close()
259+
257260

258261
__all__ = ("FlexTemplatesServiceGrpcTransport",)

google/cloud/dataflow_v1beta3/services/flex_templates_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,8 @@ def launch_flex_template(
258258
)
259259
return self._stubs["launch_flex_template"]
260260

261+
def close(self):
262+
return self.grpc_channel.close()
263+
261264

262265
__all__ = ("FlexTemplatesServiceGrpcAsyncIOTransport",)

google/cloud/dataflow_v1beta3/services/jobs_v1_beta3/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ async def snapshot_job(
517517
# Done; return the response.
518518
return response
519519

520+
async def __aenter__(self):
521+
return self
522+
523+
async def __aexit__(self, exc_type, exc, tb):
524+
await self.transport.close()
525+
520526

521527
try:
522528
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dataflow_v1beta3/services/jobs_v1_beta3/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,7 @@ def __init__(
333333
client_cert_source_for_mtls=client_cert_source_func,
334334
quota_project_id=client_options.quota_project_id,
335335
client_info=client_info,
336-
always_use_jwt_access=(
337-
Transport == type(self).get_transport_class("grpc")
338-
or Transport == type(self).get_transport_class("grpc_asyncio")
339-
),
336+
always_use_jwt_access=True,
340337
)
341338

342339
def create_job(
@@ -701,6 +698,19 @@ def snapshot_job(
701698
# Done; return the response.
702699
return response
703700

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

705715
try:
706716
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dataflow_v1beta3/services/jobs_v1_beta3/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ def _prep_wrapped_messages(self, client_info):
185185
),
186186
}
187187

188+
def close(self):
189+
"""Closes resources associated with the transport.
190+
191+
.. warning::
192+
Only call this method if the transport is NOT shared
193+
with other clients - this may cause errors in other clients!
194+
"""
195+
raise NotImplementedError()
196+
188197
@property
189198
def create_job(
190199
self,

google/cloud/dataflow_v1beta3/services/jobs_v1_beta3/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,5 +428,8 @@ def snapshot_job(self) -> Callable[[jobs.SnapshotJobRequest], snapshots.Snapshot
428428
)
429429
return self._stubs["snapshot_job"]
430430

431+
def close(self):
432+
self.grpc_channel.close()
433+
431434

432435
__all__ = ("JobsV1Beta3GrpcTransport",)

google/cloud/dataflow_v1beta3/services/jobs_v1_beta3/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,5 +437,8 @@ def snapshot_job(
437437
)
438438
return self._stubs["snapshot_job"]
439439

440+
def close(self):
441+
return self.grpc_channel.close()
442+
440443

441444
__all__ = ("JobsV1Beta3GrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)