From 7b5d09e313bc8bbd252ff0af1aa9a0d0114227e4 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Thu, 7 Oct 2021 00:34:41 +0000 Subject: [PATCH] feat: add context manager support in client (#129) - [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: https://github.com/googleapis/googleapis/commit/787f8c9a731f44e74a90b9847d48659ca9462d10 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9 --- .../services/web_risk_service/async_client.py | 6 +++ .../services/web_risk_service/client.py | 18 +++++-- .../web_risk_service/transports/base.py | 9 ++++ .../web_risk_service/transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ google/cloud/webrisk_v1/types/webrisk.py | 13 +++++ .../web_risk_service_v1_beta1/async_client.py | 6 +++ .../web_risk_service_v1_beta1/client.py | 18 +++++-- .../transports/base.py | 9 ++++ .../transports/grpc.py | 3 ++ .../transports/grpc_asyncio.py | 3 ++ google/cloud/webrisk_v1beta1/types/webrisk.py | 11 ++++ .../gapic/webrisk_v1/test_web_risk_service.py | 50 +++++++++++++++++++ .../test_web_risk_service_v1_beta1.py | 50 +++++++++++++++++++ 14 files changed, 194 insertions(+), 8 deletions(-) diff --git a/google/cloud/webrisk_v1/services/web_risk_service/async_client.py b/google/cloud/webrisk_v1/services/web_risk_service/async_client.py index 3d00601..e31e34e 100644 --- a/google/cloud/webrisk_v1/services/web_risk_service/async_client.py +++ b/google/cloud/webrisk_v1/services/web_risk_service/async_client.py @@ -538,6 +538,12 @@ async def create_submission( # Done; return the response. return response + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/webrisk_v1/services/web_risk_service/client.py b/google/cloud/webrisk_v1/services/web_risk_service/client.py index 213978f..7dfa4e8 100644 --- a/google/cloud/webrisk_v1/services/web_risk_service/client.py +++ b/google/cloud/webrisk_v1/services/web_risk_service/client.py @@ -330,10 +330,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def compute_threat_list_diff( @@ -683,6 +680,19 @@ def create_submission( # Done; return the response. return response + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/webrisk_v1/services/web_risk_service/transports/base.py b/google/cloud/webrisk_v1/services/web_risk_service/transports/base.py index 6aabcba..670c026 100644 --- a/google/cloud/webrisk_v1/services/web_risk_service/transports/base.py +++ b/google/cloud/webrisk_v1/services/web_risk_service/transports/base.py @@ -202,6 +202,15 @@ def _prep_wrapped_messages(self, client_info): ), } + def close(self): + """Closes resources associated with the transport. + + .. warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ + raise NotImplementedError() + @property def compute_threat_list_diff( self, diff --git a/google/cloud/webrisk_v1/services/web_risk_service/transports/grpc.py b/google/cloud/webrisk_v1/services/web_risk_service/transports/grpc.py index b36ca6b..9ec741a 100644 --- a/google/cloud/webrisk_v1/services/web_risk_service/transports/grpc.py +++ b/google/cloud/webrisk_v1/services/web_risk_service/transports/grpc.py @@ -356,5 +356,8 @@ def create_submission( ) return self._stubs["create_submission"] + def close(self): + self.grpc_channel.close() + __all__ = ("WebRiskServiceGrpcTransport",) diff --git a/google/cloud/webrisk_v1/services/web_risk_service/transports/grpc_asyncio.py b/google/cloud/webrisk_v1/services/web_risk_service/transports/grpc_asyncio.py index e2a1cbf..927b2d7 100644 --- a/google/cloud/webrisk_v1/services/web_risk_service/transports/grpc_asyncio.py +++ b/google/cloud/webrisk_v1/services/web_risk_service/transports/grpc_asyncio.py @@ -362,5 +362,8 @@ def create_submission( ) return self._stubs["create_submission"] + def close(self): + return self.grpc_channel.close() + __all__ = ("WebRiskServiceGrpcAsyncIOTransport",) diff --git a/google/cloud/webrisk_v1/types/webrisk.py b/google/cloud/webrisk_v1/types/webrisk.py index 732f1da..6196e15 100644 --- a/google/cloud/webrisk_v1/types/webrisk.py +++ b/google/cloud/webrisk_v1/types/webrisk.py @@ -59,6 +59,7 @@ class CompressionType(proto.Enum): class ComputeThreatListDiffRequest(proto.Message): r"""Describes an API diff request. + Attributes: threat_type (google.cloud.webrisk_v1.types.ThreatType): Required. The threat list to update. Only a @@ -78,6 +79,7 @@ class ComputeThreatListDiffRequest(proto.Message): class Constraints(proto.Message): r"""The constraints for this diff. + Attributes: max_diff_entries (int): The maximum size in number of entries. The diff will not @@ -107,6 +109,7 @@ class Constraints(proto.Message): class ComputeThreatListDiffResponse(proto.Message): r""" + Attributes: response_type (google.cloud.webrisk_v1.types.ComputeThreatListDiffResponse.ResponseType): The type of response. This may indicate that @@ -147,6 +150,7 @@ class ResponseType(proto.Enum): class Checksum(proto.Message): r"""The expected state of a client's local database. + Attributes: sha256 (bytes): The SHA256 hash of the client state; that is, @@ -168,6 +172,7 @@ class Checksum(proto.Message): class SearchUrisRequest(proto.Message): r"""Request to check URI entries against threatLists. + Attributes: uri (str): Required. The URI to be checked for matches. @@ -182,6 +187,7 @@ class SearchUrisRequest(proto.Message): class SearchUrisResponse(proto.Message): r""" + Attributes: threat (google.cloud.webrisk_v1.types.SearchUrisResponse.ThreatUri): The threat list matches. This may be empty if @@ -190,6 +196,7 @@ class SearchUrisResponse(proto.Message): class ThreatUri(proto.Message): r"""Contains threat information on a matching uri. + Attributes: threat_types (Sequence[google.cloud.webrisk_v1.types.ThreatType]): The ThreatList this threat belongs to. @@ -227,6 +234,7 @@ class SearchHashesRequest(proto.Message): class SearchHashesResponse(proto.Message): r""" + Attributes: threats (Sequence[google.cloud.webrisk_v1.types.SearchHashesResponse.ThreatHash]): The full hashes that matched the requested @@ -239,6 +247,7 @@ class SearchHashesResponse(proto.Message): class ThreatHash(proto.Message): r"""Contains threat information on a matching hash. + Attributes: threat_types (Sequence[google.cloud.webrisk_v1.types.ThreatType]): The ThreatList this threat belongs to. @@ -288,6 +297,7 @@ class ThreatEntryAdditions(proto.Message): class ThreatEntryRemovals(proto.Message): r"""Contains the set of entries to remove from a local database. + Attributes: raw_indices (google.cloud.webrisk_v1.types.RawIndices): The raw removal indices for a local list. @@ -305,6 +315,7 @@ class ThreatEntryRemovals(proto.Message): class RawIndices(proto.Message): r"""A set of raw indices to remove from a local list. + Attributes: indices (Sequence[int]): The indices to remove from a @@ -373,6 +384,7 @@ class RiceDeltaEncoding(proto.Message): class Submission(proto.Message): r"""Wraps a URI that might be displaying phishing content. + Attributes: uri (str): Required. The URI that is being reported for @@ -384,6 +396,7 @@ class Submission(proto.Message): class CreateSubmissionRequest(proto.Message): r"""Request to send a potentially phishy URI to WebRisk. + Attributes: parent (str): Required. The name of the project that is making the diff --git a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/async_client.py b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/async_client.py index 2bf114c..5843475 100644 --- a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/async_client.py +++ b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/async_client.py @@ -430,6 +430,12 @@ async def search_hashes( # Done; return the response. return response + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb): + await self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/client.py b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/client.py index 99a4df5..848b44b 100644 --- a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/client.py +++ b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/client.py @@ -332,10 +332,7 @@ def __init__( client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, - always_use_jwt_access=( - Transport == type(self).get_transport_class("grpc") - or Transport == type(self).get_transport_class("grpc_asyncio") - ), + always_use_jwt_access=True, ) def compute_threat_list_diff( @@ -574,6 +571,19 @@ def search_hashes( # Done; return the response. return response + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + """Releases underlying transport's resources. + + .. warning:: + ONLY use as a context manager if the transport is NOT shared + with other clients! Exiting the with block will CLOSE the transport + and may cause errors in other clients! + """ + self.transport.close() + try: DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( diff --git a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/base.py b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/base.py index 5d2ec55..3312229 100644 --- a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/base.py +++ b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/base.py @@ -199,6 +199,15 @@ def _prep_wrapped_messages(self, client_info): ), } + def close(self): + """Closes resources associated with the transport. + + .. warning:: + Only call this method if the transport is NOT shared + with other clients - this may cause errors in other clients! + """ + raise NotImplementedError() + @property def compute_threat_list_diff( self, diff --git a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/grpc.py b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/grpc.py index c843b40..cca44c0 100644 --- a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/grpc.py +++ b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/grpc.py @@ -312,5 +312,8 @@ def search_hashes( ) return self._stubs["search_hashes"] + def close(self): + self.grpc_channel.close() + __all__ = ("WebRiskServiceV1Beta1GrpcTransport",) diff --git a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/grpc_asyncio.py b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/grpc_asyncio.py index cef5be1..3f3d446 100644 --- a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/grpc_asyncio.py +++ b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/grpc_asyncio.py @@ -318,5 +318,8 @@ def search_hashes( ) return self._stubs["search_hashes"] + def close(self): + return self.grpc_channel.close() + __all__ = ("WebRiskServiceV1Beta1GrpcAsyncIOTransport",) diff --git a/google/cloud/webrisk_v1beta1/types/webrisk.py b/google/cloud/webrisk_v1beta1/types/webrisk.py index 89fd31f..66c0fe4 100644 --- a/google/cloud/webrisk_v1beta1/types/webrisk.py +++ b/google/cloud/webrisk_v1beta1/types/webrisk.py @@ -57,6 +57,7 @@ class CompressionType(proto.Enum): class ComputeThreatListDiffRequest(proto.Message): r"""Describes an API diff request. + Attributes: threat_type (google.cloud.webrisk_v1beta1.types.ThreatType): The ThreatList to update. @@ -71,6 +72,7 @@ class ComputeThreatListDiffRequest(proto.Message): class Constraints(proto.Message): r"""The constraints for this diff. + Attributes: max_diff_entries (int): The maximum size in number of entries. The diff will not @@ -100,6 +102,7 @@ class Constraints(proto.Message): class ComputeThreatListDiffResponse(proto.Message): r""" + Attributes: response_type (google.cloud.webrisk_v1beta1.types.ComputeThreatListDiffResponse.ResponseType): The type of response. This may indicate that @@ -137,6 +140,7 @@ class ResponseType(proto.Enum): class Checksum(proto.Message): r"""The expected state of a client's local database. + Attributes: sha256 (bytes): The SHA256 hash of the client state; that is, @@ -158,6 +162,7 @@ class Checksum(proto.Message): class SearchUrisRequest(proto.Message): r"""Request to check URI entries against threatLists. + Attributes: uri (str): Required. The URI to be checked for matches. @@ -171,6 +176,7 @@ class SearchUrisRequest(proto.Message): class SearchUrisResponse(proto.Message): r""" + Attributes: threat (google.cloud.webrisk_v1beta1.types.SearchUrisResponse.ThreatUri): The threat list matches. This may be empty if @@ -179,6 +185,7 @@ class SearchUrisResponse(proto.Message): class ThreatUri(proto.Message): r"""Contains threat information on a matching uri. + Attributes: threat_types (Sequence[google.cloud.webrisk_v1beta1.types.ThreatType]): The ThreatList this threat belongs to. @@ -215,6 +222,7 @@ class SearchHashesRequest(proto.Message): class SearchHashesResponse(proto.Message): r""" + Attributes: threats (Sequence[google.cloud.webrisk_v1beta1.types.SearchHashesResponse.ThreatHash]): The full hashes that matched the requested @@ -227,6 +235,7 @@ class SearchHashesResponse(proto.Message): class ThreatHash(proto.Message): r"""Contains threat information on a matching hash. + Attributes: threat_types (Sequence[google.cloud.webrisk_v1beta1.types.ThreatType]): The ThreatList this threat belongs to. @@ -276,6 +285,7 @@ class ThreatEntryAdditions(proto.Message): class ThreatEntryRemovals(proto.Message): r"""Contains the set of entries to remove from a local database. + Attributes: raw_indices (google.cloud.webrisk_v1beta1.types.RawIndices): The raw removal indices for a local list. @@ -293,6 +303,7 @@ class ThreatEntryRemovals(proto.Message): class RawIndices(proto.Message): r"""A set of raw indices to remove from a local list. + Attributes: indices (Sequence[int]): The indices to remove from a diff --git a/tests/unit/gapic/webrisk_v1/test_web_risk_service.py b/tests/unit/gapic/webrisk_v1/test_web_risk_service.py index 4d900fc..9ce2589 100644 --- a/tests/unit/gapic/webrisk_v1/test_web_risk_service.py +++ b/tests/unit/gapic/webrisk_v1/test_web_risk_service.py @@ -29,6 +29,7 @@ from google.api_core import gapic_v1 from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.webrisk_v1.services.web_risk_service import WebRiskServiceAsyncClient @@ -1337,6 +1338,9 @@ def test_web_risk_service_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + @requires_google_auth_gte_1_25_0 def test_web_risk_service_base_transport_with_credentials_file(): @@ -1795,3 +1799,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = WebRiskServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio", + ) + with mock.patch.object( + type(getattr(client.transport, "grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() + + +def test_transport_close(): + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): + client = WebRiskServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = WebRiskServiceClient( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called() diff --git a/tests/unit/gapic/webrisk_v1beta1/test_web_risk_service_v1_beta1.py b/tests/unit/gapic/webrisk_v1beta1/test_web_risk_service_v1_beta1.py index 3f79b00..ea7838d 100644 --- a/tests/unit/gapic/webrisk_v1beta1/test_web_risk_service_v1_beta1.py +++ b/tests/unit/gapic/webrisk_v1beta1/test_web_risk_service_v1_beta1.py @@ -29,6 +29,7 @@ from google.api_core import gapic_v1 from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async +from google.api_core import path_template from google.auth import credentials as ga_credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.webrisk_v1beta1.services.web_risk_service_v1_beta1 import ( @@ -1155,6 +1156,9 @@ def test_web_risk_service_v1_beta1_base_transport(): with pytest.raises(NotImplementedError): getattr(transport, method)(request=object()) + with pytest.raises(NotImplementedError): + transport.close() + @requires_google_auth_gte_1_25_0 def test_web_risk_service_v1_beta1_base_transport_with_credentials_file(): @@ -1617,3 +1621,49 @@ def test_client_withDEFAULT_CLIENT_INFO(): credentials=ga_credentials.AnonymousCredentials(), client_info=client_info, ) prep.assert_called_once_with(client_info) + + +@pytest.mark.asyncio +async def test_transport_close_async(): + client = WebRiskServiceV1Beta1AsyncClient( + credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio", + ) + with mock.patch.object( + type(getattr(client.transport, "grpc_channel")), "close" + ) as close: + async with client: + close.assert_not_called() + close.assert_called_once() + + +def test_transport_close(): + transports = { + "grpc": "_grpc_channel", + } + + for transport, close_name in transports.items(): + client = WebRiskServiceV1Beta1Client( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + with mock.patch.object( + type(getattr(client.transport, close_name)), "close" + ) as close: + with client: + close.assert_not_called() + close.assert_called_once() + + +def test_client_ctx(): + transports = [ + "grpc", + ] + for transport in transports: + client = WebRiskServiceV1Beta1Client( + credentials=ga_credentials.AnonymousCredentials(), transport=transport + ) + # Test client calls underlying transport. + with mock.patch.object(type(client.transport), "close") as close: + close.assert_not_called() + with client: + pass + close.assert_called()