Skip to content

Commit ff6ef1b

Browse files
gcf-owl-bot[bot]pull[bot]
authored andcommitted
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: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent af23fc5 commit ff6ef1b

File tree

13 files changed

+172
-9
lines changed

13 files changed

+172
-9
lines changed

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1/services/artifact_registry/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ async def get_repository(
411411
# Done; return the response.
412412
return response
413413

414+
async def __aenter__(self):
415+
return self
416+
417+
async def __aexit__(self, exc_type, exc, tb):
418+
await self.transport.close()
419+
414420

415421
try:
416422
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1/services/artifact_registry/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,7 @@ def __init__(
380380
client_cert_source_for_mtls=client_cert_source_func,
381381
quota_project_id=client_options.quota_project_id,
382382
client_info=client_info,
383-
always_use_jwt_access=(
384-
Transport == type(self).get_transport_class("grpc")
385-
or Transport == type(self).get_transport_class("grpc_asyncio")
386-
),
383+
always_use_jwt_access=True,
387384
)
388385

389386
def list_docker_images(
@@ -617,6 +614,19 @@ def get_repository(
617614
# Done; return the response.
618615
return response
619616

617+
def __enter__(self):
618+
return self
619+
620+
def __exit__(self, type, value, traceback):
621+
"""Releases underlying transport's resources.
622+
623+
.. warning::
624+
ONLY use as a context manager if the transport is NOT shared
625+
with other clients! Exiting the with block will CLOSE the transport
626+
and may cause errors in other clients!
627+
"""
628+
self.transport.close()
629+
620630

621631
try:
622632
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1/services/artifact_registry/transports/base.py

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

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

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,5 +320,8 @@ def get_repository(
320320
)
321321
return self._stubs["get_repository"]
322322

323+
def close(self):
324+
self.grpc_channel.close()
325+
323326

324327
__all__ = ("ArtifactRegistryGrpcTransport",)

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1/services/artifact_registry/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,8 @@ def get_repository(
324324
)
325325
return self._stubs["get_repository"]
326326

327+
def close(self):
328+
return self.grpc_channel.close()
329+
327330

328331
__all__ = ("ArtifactRegistryGrpcAsyncIOTransport",)

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1beta2/services/artifact_registry/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,12 @@ async def test_iam_permissions(
20182018
# Done; return the response.
20192019
return response
20202020

2021+
async def __aenter__(self):
2022+
return self
2023+
2024+
async def __aexit__(self, exc_type, exc, tb):
2025+
await self.transport.close()
2026+
20212027

20222028
try:
20232029
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1beta2/services/artifact_registry/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,7 @@ def __init__(
387387
client_cert_source_for_mtls=client_cert_source_func,
388388
quota_project_id=client_options.quota_project_id,
389389
client_info=client_info,
390-
always_use_jwt_access=(
391-
Transport == type(self).get_transport_class("grpc")
392-
or Transport == type(self).get_transport_class("grpc_asyncio")
393-
),
390+
always_use_jwt_access=True,
394391
)
395392

396393
def list_repositories(
@@ -2083,6 +2080,19 @@ def test_iam_permissions(
20832080
# Done; return the response.
20842081
return response
20852082

2083+
def __enter__(self):
2084+
return self
2085+
2086+
def __exit__(self, type, value, traceback):
2087+
"""Releases underlying transport's resources.
2088+
2089+
.. warning::
2090+
ONLY use as a context manager if the transport is NOT shared
2091+
with other clients! Exiting the with block will CLOSE the transport
2092+
and may cause errors in other clients!
2093+
"""
2094+
self.transport.close()
2095+
20862096

20872097
try:
20882098
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ def _prep_wrapped_messages(self, client_info):
400400
),
401401
}
402402

403+
def close(self):
404+
"""Closes resources associated with the transport.
405+
406+
.. warning::
407+
Only call this method if the transport is NOT shared
408+
with other clients - this may cause errors in other clients!
409+
"""
410+
raise NotImplementedError()
411+
403412
@property
404413
def operations_client(self) -> operations_v1.OperationsClient:
405414
"""Return the client designed to process long-running operations."""

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,5 +806,8 @@ def test_iam_permissions(
806806
)
807807
return self._stubs["test_iam_permissions"]
808808

809+
def close(self):
810+
self.grpc_channel.close()
811+
809812

810813
__all__ = ("ArtifactRegistryGrpcTransport",)

packages/google-cloud-artifact-registry/google/cloud/artifactregistry_v1beta2/services/artifact_registry/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,5 +836,8 @@ def test_iam_permissions(
836836
)
837837
return self._stubs["test_iam_permissions"]
838838

839+
def close(self):
840+
return self.grpc_channel.close()
841+
839842

840843
__all__ = ("ArtifactRegistryGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)