Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f52eaca

Browse filesBrowse files
feat: add context manager support in client (#51)
- [ ] 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 20dd1a7 commit f52eaca
Copy full SHA for f52eaca

File tree

Expand file treeCollapse file tree

7 files changed

+89
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+89
-4
lines changed
Open diff view settings
Collapse file

‎packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/async_client.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/async_client.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ async def search_versions(
367367
# Done; return the response.
368368
return response
369369

370+
async def __aenter__(self):
371+
return self
372+
373+
async def __aexit__(self, exc_type, exc, tb):
374+
await self.transport.close()
375+
370376

371377
try:
372378
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Collapse file

‎packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/client.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/client.py
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,7 @@ def __init__(
388388
client_cert_source_for_mtls=client_cert_source_func,
389389
quota_project_id=client_options.quota_project_id,
390390
client_info=client_info,
391-
always_use_jwt_access=(
392-
Transport == type(self).get_transport_class("grpc")
393-
or Transport == type(self).get_transport_class("grpc_asyncio")
394-
),
391+
always_use_jwt_access=True,
395392
)
396393

397394
def search_catalogs(
@@ -577,6 +574,19 @@ def search_versions(
577574
# Done; return the response.
578575
return response
579576

577+
def __enter__(self):
578+
return self
579+
580+
def __exit__(self, type, value, traceback):
581+
"""Releases underlying transport's resources.
582+
583+
.. warning::
584+
ONLY use as a context manager if the transport is NOT shared
585+
with other clients! Exiting the with block will CLOSE the transport
586+
and may cause errors in other clients!
587+
"""
588+
self.transport.close()
589+
580590

581591
try:
582592
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Collapse file

‎packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/base.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/base.py
+9Lines 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 search_catalogs(
170179
self,
Collapse file

‎packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,5 +336,8 @@ def search_versions(
336336
)
337337
return self._stubs["search_versions"]
338338

339+
def close(self):
340+
self.grpc_channel.close()
341+
339342

340343
__all__ = ("PrivateCatalogGrpcTransport",)
Collapse file

‎packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc_asyncio.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/services/private_catalog/transports/grpc_asyncio.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,8 @@ def search_versions(
342342
)
343343
return self._stubs["search_versions"]
344344

345+
def close(self):
346+
return self.grpc_channel.close()
347+
345348

346349
__all__ = ("PrivateCatalogGrpcAsyncIOTransport",)
Collapse file

‎packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/types/private_catalog.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-private-catalog/google/cloud/privatecatalog_v1beta1/types/private_catalog.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ class Product(proto.Message):
435435

436436
class AssetReference(proto.Message):
437437
r"""Defines the reference of an asset belonging to a product.
438+
438439
Attributes:
439440
id (str):
440441
Output only. A unique identifier among asset
@@ -502,6 +503,7 @@ class AssetValidationState(proto.Enum):
502503

503504
class Inputs(proto.Message):
504505
r"""Defines definition of input parameters of asset templates.
506+
505507
Attributes:
506508
parameters (google.protobuf.struct_pb2.Struct):
507509
Output only. The JSON schema defining the
@@ -513,6 +515,7 @@ class Inputs(proto.Message):
513515

514516
class GcsSource(proto.Message):
515517
r"""Defines how to access Cloud Storage source.
518+
516519
Attributes:
517520
gcs_path (str):
518521
Output only. the cloud storage object path.
@@ -532,6 +535,7 @@ class GcsSource(proto.Message):
532535

533536
class GitSource(proto.Message):
534537
r"""Defines how to access a Git Source.
538+
535539
Attributes:
536540
repo (str):
537541
Location of the Git repo to build.
Collapse file

‎packages/google-cloud-private-catalog/tests/unit/gapic/privatecatalog_v1beta1/test_private_catalog.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-private-catalog/tests/unit/gapic/privatecatalog_v1beta1/test_private_catalog.py
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from google.api_core import gapic_v1
3030
from google.api_core import grpc_helpers
3131
from google.api_core import grpc_helpers_async
32+
from google.api_core import path_template
3233
from google.auth import credentials as ga_credentials
3334
from google.auth.exceptions import MutualTLSChannelError
3435
from google.cloud.privatecatalog_v1beta1.services.private_catalog import (
@@ -1431,6 +1432,9 @@ def test_private_catalog_base_transport():
14311432
with pytest.raises(NotImplementedError):
14321433
getattr(transport, method)(request=object())
14331434

1435+
with pytest.raises(NotImplementedError):
1436+
transport.close()
1437+
14341438

14351439
@requires_google_auth_gte_1_25_0
14361440
def test_private_catalog_base_transport_with_credentials_file():
@@ -1949,3 +1953,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
19491953
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
19501954
)
19511955
prep.assert_called_once_with(client_info)
1956+
1957+
1958+
@pytest.mark.asyncio
1959+
async def test_transport_close_async():
1960+
client = PrivateCatalogAsyncClient(
1961+
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
1962+
)
1963+
with mock.patch.object(
1964+
type(getattr(client.transport, "grpc_channel")), "close"
1965+
) as close:
1966+
async with client:
1967+
close.assert_not_called()
1968+
close.assert_called_once()
1969+
1970+
1971+
def test_transport_close():
1972+
transports = {
1973+
"grpc": "_grpc_channel",
1974+
}
1975+
1976+
for transport, close_name in transports.items():
1977+
client = PrivateCatalogClient(
1978+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
1979+
)
1980+
with mock.patch.object(
1981+
type(getattr(client.transport, close_name)), "close"
1982+
) as close:
1983+
with client:
1984+
close.assert_not_called()
1985+
close.assert_called_once()
1986+
1987+
1988+
def test_client_ctx():
1989+
transports = [
1990+
"grpc",
1991+
]
1992+
for transport in transports:
1993+
client = PrivateCatalogClient(
1994+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
1995+
)
1996+
# Test client calls underlying transport.
1997+
with mock.patch.object(type(client.transport), "close") as close:
1998+
close.assert_not_called()
1999+
with client:
2000+
pass
2001+
close.assert_called()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.