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

Commit 317187c

Browse files
feat: add context manager support in client (#416)
- [ ] 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 675a40f commit 317187c

File tree

206 files changed

+2682
-122
lines changed

Some content is hidden

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

206 files changed

+2682
-122
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,12 @@ async def get_validation_result(
915915
# Done; return the response.
916916
return response
917917

918+
async def __aenter__(self):
919+
return self
920+
921+
async def __aexit__(self, exc_type, exc, tb):
922+
await self.transport.close()
923+
918924

919925
try:
920926
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,7 @@ def __init__(
343343
client_cert_source_for_mtls=client_cert_source_func,
344344
quota_project_id=client_options.quota_project_id,
345345
client_info=client_info,
346-
always_use_jwt_access=(
347-
Transport == type(self).get_transport_class("grpc")
348-
or Transport == type(self).get_transport_class("grpc_asyncio")
349-
),
346+
always_use_jwt_access=True,
350347
)
351348

352349
def get_agent(
@@ -1110,6 +1107,19 @@ def get_validation_result(
11101107
# Done; return the response.
11111108
return response
11121109

1110+
def __enter__(self):
1111+
return self
1112+
1113+
def __exit__(self, type, value, traceback):
1114+
"""Releases underlying transport's resources.
1115+
1116+
.. warning::
1117+
ONLY use as a context manager if the transport is NOT shared
1118+
with other clients! Exiting the with block will CLOSE the transport
1119+
and may cause errors in other clients!
1120+
"""
1121+
self.transport.close()
1122+
11131123

11141124
try:
11151125
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

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

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

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,5 +565,8 @@ def get_validation_result(
565565
)
566566
return self._stubs["get_validation_result"]
567567

568+
def close(self):
569+
self.grpc_channel.close()
570+
568571

569572
__all__ = ("AgentsGrpcTransport",)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,5 +575,8 @@ def get_validation_result(
575575
)
576576
return self._stubs["get_validation_result"]
577577

578+
def close(self):
579+
return self.grpc_channel.close()
580+
578581

579582
__all__ = ("AgentsGrpcAsyncIOTransport",)

google/cloud/dialogflow_v2/services/answer_records/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ async def update_answer_record(
361361
# Done; return the response.
362362
return response
363363

364+
async def __aenter__(self):
365+
return self
366+
367+
async def __aexit__(self, exc_type, exc, tb):
368+
await self.transport.close()
369+
364370

365371
try:
366372
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dialogflow_v2/services/answer_records/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,7 @@ def __init__(
345345
client_cert_source_for_mtls=client_cert_source_func,
346346
quota_project_id=client_options.quota_project_id,
347347
client_info=client_info,
348-
always_use_jwt_access=(
349-
Transport == type(self).get_transport_class("grpc")
350-
or Transport == type(self).get_transport_class("grpc_asyncio")
351-
),
348+
always_use_jwt_access=True,
352349
)
353350

354351
def list_answer_records(
@@ -545,6 +542,19 @@ def update_answer_record(
545542
# Done; return the response.
546543
return response
547544

545+
def __enter__(self):
546+
return self
547+
548+
def __exit__(self, type, value, traceback):
549+
"""Releases underlying transport's resources.
550+
551+
.. warning::
552+
ONLY use as a context manager if the transport is NOT shared
553+
with other clients! Exiting the with block will CLOSE the transport
554+
and may cause errors in other clients!
555+
"""
556+
self.transport.close()
557+
548558

549559
try:
550560
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/dialogflow_v2/services/answer_records/transports/base.py

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

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

google/cloud/dialogflow_v2/services/answer_records/transports/grpc.py

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

288+
def close(self):
289+
self.grpc_channel.close()
290+
288291

289292
__all__ = ("AnswerRecordsGrpcTransport",)

google/cloud/dialogflow_v2/services/answer_records/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,8 @@ def update_answer_record(
289289
)
290290
return self._stubs["update_answer_record"]
291291

292+
def close(self):
293+
return self.grpc_channel.close()
294+
292295

293296
__all__ = ("AnswerRecordsGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)