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

Commit 22efa38

Browse files
feat: add context manager support in client (#99)
- [ ] 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 92d6b71 commit 22efa38

File tree

25 files changed

+334
-12
lines changed

25 files changed

+334
-12
lines changed

google/cloud/notebooks_v1/services/managed_notebook_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,12 @@ async def report_runtime_event(
938938
# Done; return the response.
939939
return response
940940

941+
async def __aenter__(self):
942+
return self
943+
944+
async def __aexit__(self, exc_type, exc, tb):
945+
await self.transport.close()
946+
941947

942948
try:
943949
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/notebooks_v1/services/managed_notebook_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,7 @@ def __init__(
353353
client_cert_source_for_mtls=client_cert_source_func,
354354
quota_project_id=client_options.quota_project_id,
355355
client_info=client_info,
356-
always_use_jwt_access=(
357-
Transport == type(self).get_transport_class("grpc")
358-
or Transport == type(self).get_transport_class("grpc_asyncio")
359-
),
356+
always_use_jwt_access=True,
360357
)
361358

362359
def list_runtimes(
@@ -1126,6 +1123,19 @@ def report_runtime_event(
11261123
# Done; return the response.
11271124
return response
11281125

1126+
def __enter__(self):
1127+
return self
1128+
1129+
def __exit__(self, type, value, traceback):
1130+
"""Releases underlying transport's resources.
1131+
1132+
.. warning::
1133+
ONLY use as a context manager if the transport is NOT shared
1134+
with other clients! Exiting the with block will CLOSE the transport
1135+
and may cause errors in other clients!
1136+
"""
1137+
self.transport.close()
1138+
11291139

11301140
try:
11311141
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/notebooks_v1/services/managed_notebook_service/transports/base.py

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

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

google/cloud/notebooks_v1/services/managed_notebook_service/transports/grpc.py

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

498+
def close(self):
499+
self.grpc_channel.close()
500+
498501

499502
__all__ = ("ManagedNotebookServiceGrpcTransport",)

google/cloud/notebooks_v1/services/managed_notebook_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,5 +513,8 @@ def report_runtime_event(
513513
)
514514
return self._stubs["report_runtime_event"]
515515

516+
def close(self):
517+
return self.grpc_channel.close()
518+
516519

517520
__all__ = ("ManagedNotebookServiceGrpcAsyncIOTransport",)

google/cloud/notebooks_v1/services/notebook_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,6 +2530,12 @@ async def create_execution(
25302530
# Done; return the response.
25312531
return response
25322532

2533+
async def __aenter__(self):
2534+
return self
2535+
2536+
async def __aexit__(self, exc_type, exc, tb):
2537+
await self.transport.close()
2538+
25332539

25342540
try:
25352541
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/notebooks_v1/services/notebook_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,7 @@ def __init__(
400400
client_cert_source_for_mtls=client_cert_source_func,
401401
quota_project_id=client_options.quota_project_id,
402402
client_info=client_info,
403-
always_use_jwt_access=(
404-
Transport == type(self).get_transport_class("grpc")
405-
or Transport == type(self).get_transport_class("grpc_asyncio")
406-
),
403+
always_use_jwt_access=True,
407404
)
408405

409406
def list_instances(
@@ -2778,6 +2775,19 @@ def create_execution(
27782775
# Done; return the response.
27792776
return response
27802777

2778+
def __enter__(self):
2779+
return self
2780+
2781+
def __exit__(self, type, value, traceback):
2782+
"""Releases underlying transport's resources.
2783+
2784+
.. warning::
2785+
ONLY use as a context manager if the transport is NOT shared
2786+
with other clients! Exiting the with block will CLOSE the transport
2787+
and may cause errors in other clients!
2788+
"""
2789+
self.transport.close()
2790+
27812791

27822792
try:
27832793
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/notebooks_v1/services/notebook_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ def _prep_wrapped_messages(self, client_info):
270270
),
271271
}
272272

273+
def close(self):
274+
"""Closes resources associated with the transport.
275+
276+
.. warning::
277+
Only call this method if the transport is NOT shared
278+
with other clients - this may cause errors in other clients!
279+
"""
280+
raise NotImplementedError()
281+
273282
@property
274283
def operations_client(self) -> operations_v1.OperationsClient:
275284
"""Return the client designed to process long-running operations."""

google/cloud/notebooks_v1/services/notebook_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,5 +1099,8 @@ def create_execution(
10991099
)
11001100
return self._stubs["create_execution"]
11011101

1102+
def close(self):
1103+
self.grpc_channel.close()
1104+
11021105

11031106
__all__ = ("NotebookServiceGrpcTransport",)

google/cloud/notebooks_v1/services/notebook_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,5 +1146,8 @@ def create_execution(
11461146
)
11471147
return self._stubs["create_execution"]
11481148

1149+
def close(self):
1150+
return self.grpc_channel.close()
1151+
11491152

11501153
__all__ = ("NotebookServiceGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)