Skip to content

Commit 58d911e

Browse files
feat: add context manager support in client (#88)
- [ ] 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 84a1e77 commit 58d911e

File tree

14 files changed

+172
-8
lines changed

14 files changed

+172
-8
lines changed

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1/services/hub_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,12 @@ async def delete_spoke(
10931093
# Done; return the response.
10941094
return response
10951095

1096+
async def __aenter__(self):
1097+
return self
1098+
1099+
async def __aexit__(self, exc_type, exc, tb):
1100+
await self.transport.close()
1101+
10961102

10971103
try:
10981104
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1/services/hub_service/client.py

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

442439
def list_hubs(
@@ -1352,6 +1349,19 @@ def delete_spoke(
13521349
# Done; return the response.
13531350
return response
13541351

1352+
def __enter__(self):
1353+
return self
1354+
1355+
def __exit__(self, type, value, traceback):
1356+
"""Releases underlying transport's resources.
1357+
1358+
.. warning::
1359+
ONLY use as a context manager if the transport is NOT shared
1360+
with other clients! Exiting the with block will CLOSE the transport
1361+
and may cause errors in other clients!
1362+
"""
1363+
self.transport.close()
1364+
13551365

13561366
try:
13571367
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1/services/hub_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ def _prep_wrapped_messages(self, client_info):
189189
),
190190
}
191191

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

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1/services/hub_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,5 +499,8 @@ def delete_spoke(
499499
)
500500
return self._stubs["delete_spoke"]
501501

502+
def close(self):
503+
self.grpc_channel.close()
504+
502505

503506
__all__ = ("HubServiceGrpcTransport",)

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1/services/hub_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,5 +510,8 @@ def delete_spoke(
510510
)
511511
return self._stubs["delete_spoke"]
512512

513+
def close(self):
514+
return self.grpc_channel.close()
515+
513516

514517
__all__ = ("HubServiceGrpcAsyncIOTransport",)

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1/types/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
class OperationMetadata(proto.Message):
2727
r"""Represents the metadata of the long-running operation.
28+
2829
Attributes:
2930
create_time (google.protobuf.timestamp_pb2.Timestamp):
3031
Output only. The time the operation was

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1alpha1/services/hub_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,12 @@ async def delete_spoke(
10761076
# Done; return the response.
10771077
return response
10781078

1079+
async def __aenter__(self):
1080+
return self
1081+
1082+
async def __aexit__(self, exc_type, exc, tb):
1083+
await self.transport.close()
1084+
10791085

10801086
try:
10811087
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1alpha1/services/hub_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,7 @@ def __init__(
418418
client_cert_source_for_mtls=client_cert_source_func,
419419
quota_project_id=client_options.quota_project_id,
420420
client_info=client_info,
421-
always_use_jwt_access=(
422-
Transport == type(self).get_transport_class("grpc")
423-
or Transport == type(self).get_transport_class("grpc_asyncio")
424-
),
421+
always_use_jwt_access=True,
425422
)
426423

427424
def list_hubs(
@@ -1322,6 +1319,19 @@ def delete_spoke(
13221319
# Done; return the response.
13231320
return response
13241321

1322+
def __enter__(self):
1323+
return self
1324+
1325+
def __exit__(self, type, value, traceback):
1326+
"""Releases underlying transport's resources.
1327+
1328+
.. warning::
1329+
ONLY use as a context manager if the transport is NOT shared
1330+
with other clients! Exiting the with block will CLOSE the transport
1331+
and may cause errors in other clients!
1332+
"""
1333+
self.transport.close()
1334+
13251335

13261336
try:
13271337
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1alpha1/services/hub_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ def _prep_wrapped_messages(self, client_info):
189189
),
190190
}
191191

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

packages/google-cloud-network-connectivity/google/cloud/networkconnectivity_v1alpha1/services/hub_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,5 +496,8 @@ def delete_spoke(
496496
)
497497
return self._stubs["delete_spoke"]
498498

499+
def close(self):
500+
self.grpc_channel.close()
501+
499502

500503
__all__ = ("HubServiceGrpcTransport",)

0 commit comments

Comments
 (0)