Skip to content

Commit a77ff6f

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: googleapis/googleapis-gen@81decff Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 6d0468d commit a77ff6f

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

packages/google-cloud-dialogflow-cx/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(

packages/google-cloud-dialogflow-cx/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(

packages/google-cloud-dialogflow-cx/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."""

packages/google-cloud-dialogflow-cx/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",)

packages/google-cloud-dialogflow-cx/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",)

packages/google-cloud-dialogflow-cx/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(

packages/google-cloud-dialogflow-cx/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(

packages/google-cloud-dialogflow-cx/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,

packages/google-cloud-dialogflow-cx/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",)

packages/google-cloud-dialogflow-cx/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",)

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/entity_types/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,12 @@ async def delete_entity_type(
646646
request, retry=retry, timeout=timeout, metadata=metadata,
647647
)
648648

649+
async def __aenter__(self):
650+
return self
651+
652+
async def __aexit__(self, exc_type, exc, tb):
653+
await self.transport.close()
654+
649655

650656
try:
651657
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/entity_types/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,7 @@ def __init__(
348348
client_cert_source_for_mtls=client_cert_source_func,
349349
quota_project_id=client_options.quota_project_id,
350350
client_info=client_info,
351-
always_use_jwt_access=(
352-
Transport == type(self).get_transport_class("grpc")
353-
or Transport == type(self).get_transport_class("grpc_asyncio")
354-
),
351+
always_use_jwt_access=True,
355352
)
356353

357354
def list_entity_types(
@@ -839,6 +836,19 @@ def delete_entity_type(
839836
request, retry=retry, timeout=timeout, metadata=metadata,
840837
)
841838

839+
def __enter__(self):
840+
return self
841+
842+
def __exit__(self, type, value, traceback):
843+
"""Releases underlying transport's resources.
844+
845+
.. warning::
846+
ONLY use as a context manager if the transport is NOT shared
847+
with other clients! Exiting the with block will CLOSE the transport
848+
and may cause errors in other clients!
849+
"""
850+
self.transport.close()
851+
842852

843853
try:
844854
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/entity_types/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ def _prep_wrapped_messages(self, client_info):
176176
),
177177
}
178178

179+
def close(self):
180+
"""Closes resources associated with the transport.
181+
182+
.. warning::
183+
Only call this method if the transport is NOT shared
184+
with other clients - this may cause errors in other clients!
185+
"""
186+
raise NotImplementedError()
187+
179188
@property
180189
def list_entity_types(
181190
self,

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/entity_types/transports/grpc.py

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

380+
def close(self):
381+
self.grpc_channel.close()
382+
380383

381384
__all__ = ("EntityTypesGrpcTransport",)

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/entity_types/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,5 +385,8 @@ def delete_entity_type(
385385
)
386386
return self._stubs["delete_entity_type"]
387387

388+
def close(self):
389+
return self.grpc_channel.close()
390+
388391

389392
__all__ = ("EntityTypesGrpcAsyncIOTransport",)

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/environments/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,12 @@ async def deploy_flow(
952952
# Done; return the response.
953953
return response
954954

955+
async def __aenter__(self):
956+
return self
957+
958+
async def __aexit__(self, exc_type, exc, tb):
959+
await self.transport.close()
960+
955961

956962
try:
957963
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/environments/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,7 @@ def __init__(
434434
client_cert_source_for_mtls=client_cert_source_func,
435435
quota_project_id=client_options.quota_project_id,
436436
client_info=client_info,
437-
always_use_jwt_access=(
438-
Transport == type(self).get_transport_class("grpc")
439-
or Transport == type(self).get_transport_class("grpc_asyncio")
440-
),
437+
always_use_jwt_access=True,
441438
)
442439

443440
def list_environments(
@@ -1219,6 +1216,19 @@ def deploy_flow(
12191216
# Done; return the response.
12201217
return response
12211218

1219+
def __enter__(self):
1220+
return self
1221+
1222+
def __exit__(self, type, value, traceback):
1223+
"""Releases underlying transport's resources.
1224+
1225+
.. warning::
1226+
ONLY use as a context manager if the transport is NOT shared
1227+
with other clients! Exiting the with block will CLOSE the transport
1228+
and may cause errors in other clients!
1229+
"""
1230+
self.transport.close()
1231+
12221232

12231233
try:
12241234
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/environments/transports/base.py

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

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

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/environments/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,5 +541,8 @@ def deploy_flow(
541541
)
542542
return self._stubs["deploy_flow"]
543543

544+
def close(self):
545+
self.grpc_channel.close()
546+
544547

545548
__all__ = ("EnvironmentsGrpcTransport",)

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/environments/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,5 +551,8 @@ def deploy_flow(
551551
)
552552
return self._stubs["deploy_flow"]
553553

554+
def close(self):
555+
return self.grpc_channel.close()
556+
554557

555558
__all__ = ("EnvironmentsGrpcAsyncIOTransport",)

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/experiments/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,12 @@ async def stop_experiment(
705705
# Done; return the response.
706706
return response
707707

708+
async def __aenter__(self):
709+
return self
710+
711+
async def __aexit__(self, exc_type, exc, tb):
712+
await self.transport.close()
713+
708714

709715
try:
710716
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/experiments/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,7 @@ def __init__(
372372
client_cert_source_for_mtls=client_cert_source_func,
373373
quota_project_id=client_options.quota_project_id,
374374
client_info=client_info,
375-
always_use_jwt_access=(
376-
Transport == type(self).get_transport_class("grpc")
377-
or Transport == type(self).get_transport_class("grpc_asyncio")
378-
),
375+
always_use_jwt_access=True,
379376
)
380377

381378
def list_experiments(
@@ -918,6 +915,19 @@ def stop_experiment(
918915
# Done; return the response.
919916
return response
920917

918+
def __enter__(self):
919+
return self
920+
921+
def __exit__(self, type, value, traceback):
922+
"""Releases underlying transport's resources.
923+
924+
.. warning::
925+
ONLY use as a context manager if the transport is NOT shared
926+
with other clients! Exiting the with block will CLOSE the transport
927+
and may cause errors in other clients!
928+
"""
929+
self.transport.close()
930+
921931

922932
try:
923933
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/experiments/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ def _prep_wrapped_messages(self, client_info):
182182
),
183183
}
184184

185+
def close(self):
186+
"""Closes resources associated with the transport.
187+
188+
.. warning::
189+
Only call this method if the transport is NOT shared
190+
with other clients - this may cause errors in other clients!
191+
"""
192+
raise NotImplementedError()
193+
185194
@property
186195
def list_experiments(
187196
self,

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/experiments/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,5 +427,8 @@ def stop_experiment(
427427
)
428428
return self._stubs["stop_experiment"]
429429

430+
def close(self):
431+
self.grpc_channel.close()
432+
430433

431434
__all__ = ("ExperimentsGrpcTransport",)

packages/google-cloud-dialogflow-cx/google/cloud/dialogflowcx_v3/services/experiments/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,5 +433,8 @@ def stop_experiment(
433433
)
434434
return self._stubs["stop_experiment"]
435435

436+
def close(self):
437+
return self.grpc_channel.close()
438+
436439

437440
__all__ = ("ExperimentsGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)