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

Commit 70d4776

Browse files
feat: add context manager support in client (#187)
- [ ] 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 470d389 commit 70d4776

File tree

216 files changed

+2696
-126
lines changed

Some content is hidden

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

216 files changed

+2696
-126
lines changed

google/cloud/dialogflowcx_v3/services/agents/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,12 @@ async def get_agent_validation_result(
881881
# Done; return the response.
882882
return response
883883

884+
async def __aenter__(self):
885+
return self
886+
887+
async def __aexit__(self, exc_type, exc, tb):
888+
await self.transport.close()
889+
884890

885891
try:
886892
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dialogflowcx_v3/services/agents/client.py

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

445442
def list_agents(
@@ -1151,6 +1148,19 @@ def get_agent_validation_result(
11511148
# Done; return the response.
11521149
return response
11531150

1151+
def __enter__(self):
1152+
return self
1153+
1154+
def __exit__(self, type, value, traceback):
1155+
"""Releases underlying transport's resources.
1156+
1157+
.. warning::
1158+
ONLY use as a context manager if the transport is NOT shared
1159+
with other clients! Exiting the with block will CLOSE the transport
1160+
and may cause errors in other clients!
1161+
"""
1162+
self.transport.close()
1163+
11541164

11551165
try:
11561166
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dialogflowcx_v3/services/agents/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ def _prep_wrapped_messages(self, client_info):
192192
),
193193
}
194194

195+
def close(self):
196+
"""Closes resources associated with the transport.
197+
198+
.. warning::
199+
Only call this method if the transport is NOT shared
200+
with other clients - this may cause errors in other clients!
201+
"""
202+
raise NotImplementedError()
203+
195204
@property
196205
def operations_client(self) -> operations_v1.OperationsClient:
197206
"""Return the client designed to process long-running operations."""

google/cloud/dialogflowcx_v3/services/agents/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,5 +515,8 @@ def get_agent_validation_result(
515515
)
516516
return self._stubs["get_agent_validation_result"]
517517

518+
def close(self):
519+
self.grpc_channel.close()
520+
518521

519522
__all__ = ("AgentsGrpcTransport",)

google/cloud/dialogflowcx_v3/services/agents/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,5 +524,8 @@ def get_agent_validation_result(
524524
)
525525
return self._stubs["get_agent_validation_result"]
526526

527+
def close(self):
528+
return self.grpc_channel.close()
529+
527530

528531
__all__ = ("AgentsGrpcAsyncIOTransport",)

google/cloud/dialogflowcx_v3/services/deployments/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ async def get_deployment(
330330
# Done; return the response.
331331
return response
332332

333+
async def __aenter__(self):
334+
return self
335+
336+
async def __aexit__(self, exc_type, exc, tb):
337+
await self.transport.close()
338+
333339

334340
try:
335341
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dialogflowcx_v3/services/deployments/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,7 @@ def __init__(
413413
client_cert_source_for_mtls=client_cert_source_func,
414414
quota_project_id=client_options.quota_project_id,
415415
client_info=client_info,
416-
always_use_jwt_access=(
417-
Transport == type(self).get_transport_class("grpc")
418-
or Transport == type(self).get_transport_class("grpc_asyncio")
419-
),
416+
always_use_jwt_access=True,
420417
)
421418

422419
def list_deployments(
@@ -581,6 +578,19 @@ def get_deployment(
581578
# Done; return the response.
582579
return response
583580

581+
def __enter__(self):
582+
return self
583+
584+
def __exit__(self, type, value, traceback):
585+
"""Releases underlying transport's resources.
586+
587+
.. warning::
588+
ONLY use as a context manager if the transport is NOT shared
589+
with other clients! Exiting the with block will CLOSE the transport
590+
and may cause errors in other clients!
591+
"""
592+
self.transport.close()
593+
584594

585595
try:
586596
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dialogflowcx_v3/services/deployments/transports/base.py

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

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

google/cloud/dialogflowcx_v3/services/deployments/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,8 @@ def get_deployment(
282282
)
283283
return self._stubs["get_deployment"]
284284

285+
def close(self):
286+
self.grpc_channel.close()
287+
285288

286289
__all__ = ("DeploymentsGrpcTransport",)

google/cloud/dialogflowcx_v3/services/deployments/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,8 @@ def get_deployment(
286286
)
287287
return self._stubs["get_deployment"]
288288

289+
def close(self):
290+
return self.grpc_channel.close()
291+
289292

290293
__all__ = ("DeploymentsGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)