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

Commit 0991f56

Browse files
feat: add context manager support in client (#84)
- [ ] 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 01085f4 commit 0991f56

File tree

27 files changed

+360
-16
lines changed

27 files changed

+360
-16
lines changed

google/cloud/binaryauthorization_v1/services/binauthz_management_service_v1/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,12 @@ async def delete_attestor(
826826
request, retry=retry, timeout=timeout, metadata=metadata,
827827
)
828828

829+
async def __aenter__(self):
830+
return self
831+
832+
async def __aexit__(self, exc_type, exc, tb):
833+
await self.transport.close()
834+
829835

830836
try:
831837
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/binaryauthorization_v1/services/binauthz_management_service_v1/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,7 @@ def __init__(
369369
client_cert_source_for_mtls=client_cert_source_func,
370370
quota_project_id=client_options.quota_project_id,
371371
client_info=client_info,
372-
always_use_jwt_access=(
373-
Transport == type(self).get_transport_class("grpc")
374-
or Transport == type(self).get_transport_class("grpc_asyncio")
375-
),
372+
always_use_jwt_access=True,
376373
)
377374

378375
def get_policy(
@@ -956,6 +953,19 @@ def delete_attestor(
956953
request, retry=retry, timeout=timeout, metadata=metadata,
957954
)
958955

956+
def __enter__(self):
957+
return self
958+
959+
def __exit__(self, type, value, traceback):
960+
"""Releases underlying transport's resources.
961+
962+
.. warning::
963+
ONLY use as a context manager if the transport is NOT shared
964+
with other clients! Exiting the with block will CLOSE the transport
965+
and may cause errors in other clients!
966+
"""
967+
self.transport.close()
968+
959969

960970
try:
961971
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/binaryauthorization_v1/services/binauthz_management_service_v1/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ def _prep_wrapped_messages(self, client_info):
251251
),
252252
}
253253

254+
def close(self):
255+
"""Closes resources associated with the transport.
256+
257+
.. warning::
258+
Only call this method if the transport is NOT shared
259+
with other clients - this may cause errors in other clients!
260+
"""
261+
raise NotImplementedError()
262+
254263
@property
255264
def get_policy(
256265
self,

google/cloud/binaryauthorization_v1/services/binauthz_management_service_v1/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,5 +450,8 @@ def delete_attestor(
450450
)
451451
return self._stubs["delete_attestor"]
452452

453+
def close(self):
454+
self.grpc_channel.close()
455+
453456

454457
__all__ = ("BinauthzManagementServiceV1GrpcTransport",)

google/cloud/binaryauthorization_v1/services/binauthz_management_service_v1/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,5 +459,8 @@ def delete_attestor(
459459
)
460460
return self._stubs["delete_attestor"]
461461

462+
def close(self):
463+
return self.grpc_channel.close()
464+
462465

463466
__all__ = ("BinauthzManagementServiceV1GrpcAsyncIOTransport",)

google/cloud/binaryauthorization_v1/services/system_policy_v1/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ async def get_system_policy(
235235
# Done; return the response.
236236
return response
237237

238+
async def __aenter__(self):
239+
return self
240+
241+
async def __aexit__(self, exc_type, exc, tb):
242+
await self.transport.close()
243+
238244

239245
try:
240246
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/binaryauthorization_v1/services/system_policy_v1/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,7 @@ def __init__(
340340
client_cert_source_for_mtls=client_cert_source_func,
341341
quota_project_id=client_options.quota_project_id,
342342
client_info=client_info,
343-
always_use_jwt_access=(
344-
Transport == type(self).get_transport_class("grpc")
345-
or Transport == type(self).get_transport_class("grpc_asyncio")
346-
),
343+
always_use_jwt_access=True,
347344
)
348345

349346
def get_system_policy(
@@ -419,6 +416,19 @@ def get_system_policy(
419416
# Done; return the response.
420417
return response
421418

419+
def __enter__(self):
420+
return self
421+
422+
def __exit__(self, type, value, traceback):
423+
"""Releases underlying transport's resources.
424+
425+
.. warning::
426+
ONLY use as a context manager if the transport is NOT shared
427+
with other clients! Exiting the with block will CLOSE the transport
428+
and may cause errors in other clients!
429+
"""
430+
self.transport.close()
431+
422432

423433
try:
424434
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/binaryauthorization_v1/services/system_policy_v1/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ def _prep_wrapped_messages(self, client_info):
160160
),
161161
}
162162

163+
def close(self):
164+
"""Closes resources associated with the transport.
165+
166+
.. warning::
167+
Only call this method if the transport is NOT shared
168+
with other clients - this may cause errors in other clients!
169+
"""
170+
raise NotImplementedError()
171+
163172
@property
164173
def get_system_policy(
165174
self,

google/cloud/binaryauthorization_v1/services/system_policy_v1/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,8 @@ def get_system_policy(
253253
)
254254
return self._stubs["get_system_policy"]
255255

256+
def close(self):
257+
self.grpc_channel.close()
258+
256259

257260
__all__ = ("SystemPolicyV1GrpcTransport",)

google/cloud/binaryauthorization_v1/services/system_policy_v1/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,8 @@ def get_system_policy(
256256
)
257257
return self._stubs["get_system_policy"]
258258

259+
def close(self):
260+
return self.grpc_channel.close()
261+
259262

260263
__all__ = ("SystemPolicyV1GrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)