From 3f09199d2a9aa40a74dc7f83ac3d4b27bb4e3791 Mon Sep 17 00:00:00 2001 From: Yoshi Automation Bot Date: Wed, 23 Dec 2020 15:32:02 -0800 Subject: [PATCH] feat: add common resource helpers; expose client transport; remove gRPC send/recv limits (#40) * changes without context autosynth cannot find the source of changes triggered by earlier changes in this repository, or by version upgrades to tools such as linters. * chore: use gapic-generator-python 0.28.1 Also re-adds the grpc_service_config to all the Python libraries that have been moved to the microgenerator. PiperOrigin-RevId: 321807443 Source-Author: Google APIs Source-Date: Fri Jul 17 10:39:39 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: abb30a02aa8583950595ca92b6fb4a3c2a3e9f03 Source-Link: https://github.com/googleapis/googleapis/commit/abb30a02aa8583950595ca92b6fb4a3c2a3e9f03 * fix: Fix Java and PHP bazel builds on Mac Specifically it should resolve the broken tar.gz archives issue on mac. PiperOrigin-RevId: 321904251 Source-Author: Google APIs Source-Date: Fri Jul 17 20:57:58 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 5340d28e386f4a70b6e9da146858b3f129b790a7 Source-Link: https://github.com/googleapis/googleapis/commit/5340d28e386f4a70b6e9da146858b3f129b790a7 * fix!: Remove fields for un-available feature. PiperOrigin-RevId: 322174882 Source-Author: Google APIs Source-Date: Mon Jul 20 10:35:53 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 4ceb9b6ff45b5665d85a164cc59941e17f827d85 Source-Link: https://github.com/googleapis/googleapis/commit/4ceb9b6ff45b5665d85a164cc59941e17f827d85 * chore: upgrade to gapic-generator-python v0.29.0 Also enables the 'add-iam-method' option for KMS and Pub/Sub. https://github.com/googleapis/gapic-generator-python/releases/tag/v0.29.0 PiperOrigin-RevId: 322659285 Source-Author: Google APIs Source-Date: Wed Jul 22 14:44:27 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 5d0eacc7ba0d9199ef1fcd7c542dd4b53949c285 Source-Link: https://github.com/googleapis/googleapis/commit/5d0eacc7ba0d9199ef1fcd7c542dd4b53949c285 * chore: upgrade to gapic-generator-python 0.29.1 PiperOrigin-RevId: 322700241 Source-Author: Google APIs Source-Date: Wed Jul 22 18:41:34 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: a1c9dba23852947c0232f7c13dd1e92108f92153 Source-Link: https://github.com/googleapis/googleapis/commit/a1c9dba23852947c0232f7c13dd1e92108f92153 * chore: migrate securitycenter to python microgenerator PiperOrigin-RevId: 323472217 Source-Author: Google APIs Source-Date: Mon Jul 27 17:03:43 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 6a813acf535e4746fa4a135ce23547bb6425c26d Source-Link: https://github.com/googleapis/googleapis/commit/6a813acf535e4746fa4a135ce23547bb6425c26d * Add/correct PHP namespace for BigQuery connection/reservation PiperOrigin-RevId: 323605868 Source-Author: Google APIs Source-Date: Tue Jul 28 10:47:24 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 0c6a73081373ff3ff3d470efe554f2f1efd64041 Source-Link: https://github.com/googleapis/googleapis/commit/0c6a73081373ff3ff3d470efe554f2f1efd64041 * chore: upgrade to gapic-generator-python 0.30.0 PiperOrigin-RevId: 323630732 Source-Author: Google APIs Source-Date: Tue Jul 28 12:37:10 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 74e8cf85f23d3cef04fcc75b4f59a21a2b41886c Source-Link: https://github.com/googleapis/googleapis/commit/74e8cf85f23d3cef04fcc75b4f59a21a2b41886c * migrate cloud asset BUILD.bazel to python microgen PiperOrigin-RevId: 323858574 Source-Author: Google APIs Source-Date: Wed Jul 29 13:40:36 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: c8fc4b79cc54749520e6fc7726deee7a22d1f988 Source-Link: https://github.com/googleapis/googleapis/commit/c8fc4b79cc54749520e6fc7726deee7a22d1f988 * update python micro-gen version PiperOrigin-RevId: 324035013 Source-Author: Google APIs Source-Date: Thu Jul 30 10:46:03 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 8cda089accaead072caea33c5081be8a4b8f9ea7 Source-Link: https://github.com/googleapis/googleapis/commit/8cda089accaead072caea33c5081be8a4b8f9ea7 * chore: cleaning up unnecessary comment block in WORKSPACE file. PiperOrigin-RevId: 324046932 Source-Author: Google APIs Source-Date: Thu Jul 30 11:38:24 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 26e80fe6cffa4963139373b9a8300bddad606daa Source-Link: https://github.com/googleapis/googleapis/commit/26e80fe6cffa4963139373b9a8300bddad606daa * Update protobuf workspace dependency to v3.13.0. PiperOrigin-RevId: 327026955 Source-Author: Google APIs Source-Date: Mon Aug 17 08:40:22 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 0dc0a6c0f1a9f979bc0690f0caa5fbafa3000c2c Source-Link: https://github.com/googleapis/googleapis/commit/0dc0a6c0f1a9f979bc0690f0caa5fbafa3000c2c * chore: upgrade to gapic-generator-python 0.32.0 PiperOrigin-RevId: 327246996 Source-Author: Google APIs Source-Date: Tue Aug 18 09:48:39 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 09c2579cfa9f37eb91e21ba53c0eaca1481a13cf Source-Link: https://github.com/googleapis/googleapis/commit/09c2579cfa9f37eb91e21ba53c0eaca1481a13cf * Updates the gapic-generator-ruby import to the latest version using bundler. PiperOrigin-RevId: 327261444 Source-Author: Google APIs Source-Date: Tue Aug 18 10:51:33 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 18ffeb71f9433cb6f82b2ddccb9e11d553be4cdc Source-Link: https://github.com/googleapis/googleapis/commit/18ffeb71f9433cb6f82b2ddccb9e11d553be4cdc * feat: (a) Add additional binding for session with environment. (b) Allow force delete entity types and webhooks. PiperOrigin-RevId: 327289448 Source-Author: Google APIs Source-Date: Tue Aug 18 13:00:19 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 6ee93932840fb3ccbaaa1527d549ddc04e4812fc Source-Link: https://github.com/googleapis/googleapis/commit/6ee93932840fb3ccbaaa1527d549ddc04e4812fc * chore: upgrade gapic-generator-python to 0.32.2 PiperOrigin-RevId: 328157915 Source-Author: Google APIs Source-Date: Mon Aug 24 09:54:09 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: f75a734e2c257765764074562f191f0e130c34e5 Source-Link: https://github.com/googleapis/googleapis/commit/f75a734e2c257765764074562f191f0e130c34e5 * feat: add media translation v1alpha1 API PiperOrigin-RevId: 328476173 Source-Author: Google APIs Source-Date: Tue Aug 25 23:29:03 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 1a85976e6bcecdaee1242f6e7fd8b3db81904f56 Source-Link: https://github.com/googleapis/googleapis/commit/1a85976e6bcecdaee1242f6e7fd8b3db81904f56 * fix: revert removal of WorkerPool API PiperOrigin-RevId: 330974505 Source-Author: Google APIs Source-Date: Thu Sep 10 11:08:18 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 3257ba52160dd8a9c7f83c34d13793de843bdb4a Source-Link: https://github.com/googleapis/googleapis/commit/3257ba52160dd8a9c7f83c34d13793de843bdb4a * chore: update python microgen version to 0.33.0 PiperOrigin-RevId: 331031262 Source-Author: Google APIs Source-Date: Thu Sep 10 15:35:17 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 6a18a3c50ffd62de53dd2e44f599d6696580f90b Source-Link: https://github.com/googleapis/googleapis/commit/6a18a3c50ffd62de53dd2e44f599d6696580f90b * feat: use https://github.com/googleapis/gapic-generator-csharp for generating C# libraries. PiperOrigin-RevId: 331104099 Source-Author: Google APIs Source-Date: Fri Sep 11 01:15:53 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: b821f320473c8ec05a1c7fb9a496c958b1ab9834 Source-Link: https://github.com/googleapis/googleapis/commit/b821f320473c8ec05a1c7fb9a496c958b1ab9834 * Update gapic-generator-python pin to 0.33.4 PiperOrigin-RevId: 333159182 Source-Author: Google APIs Source-Date: Tue Sep 22 14:22:59 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 3dbeac0d54125b123c8dfd39c774b37473c36944 Source-Link: https://github.com/googleapis/googleapis/commit/3dbeac0d54125b123c8dfd39c774b37473c36944 * chore: use Python microgenerator rules for aiplatform PiperOrigin-RevId: 333302099 Source-Author: Google APIs Source-Date: Wed Sep 23 08:58:22 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: acd4aabf1c1e3e5bf7db0e81f323003fb317fdaa Source-Link: https://github.com/googleapis/googleapis/commit/acd4aabf1c1e3e5bf7db0e81f323003fb317fdaa * chore: use gapic-generator-python 0.33.6 PiperOrigin-RevId: 333323660 Source-Author: Google APIs Source-Date: Wed Sep 23 10:41:17 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 7e377ce8f06ced48a79b45d97eebccb8a51f1e28 Source-Link: https://github.com/googleapis/googleapis/commit/7e377ce8f06ced48a79b45d97eebccb8a51f1e28 * chore: move talent v4 to python microgenerator rules PiperOrigin-RevId: 333372432 Source-Author: Google APIs Source-Date: Wed Sep 23 14:23:38 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 0fd468f90d1236193ee4cbdfb22265b674de121f Source-Link: https://github.com/googleapis/googleapis/commit/0fd468f90d1236193ee4cbdfb22265b674de121f * fix: naming style updates Source-Author: Mira Leung Source-Date: Wed Sep 23 16:33:18 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 3737889dafd012d7f7f724fa04390d42db496e6c Source-Link: https://github.com/googleapis/googleapis/commit/3737889dafd012d7f7f724fa04390d42db496e6c * Add Service Direcotry v1 protos and configs PiperOrigin-RevId: 333502684 Source-Author: Google APIs Source-Date: Thu Sep 24 06:33:53 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: d3697d47929f34c43bcc4de16c7c761a80e97cf1 Source-Link: https://github.com/googleapis/googleapis/commit/d3697d47929f34c43bcc4de16c7c761a80e97cf1 * GA launch for PersonDetection and FaceDetection features. PiperOrigin-RevId: 334093713 Source-Author: Google APIs Source-Date: Sun Sep 27 23:41:00 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: e7375a91e7942c562c61495a908f4f6d50b4ea3f Source-Link: https://github.com/googleapis/googleapis/commit/e7375a91e7942c562c61495a908f4f6d50b4ea3f * migrate monitoring_v3 python part to use microgen PiperOrigin-RevId: 335029068 Source-Author: Google APIs Source-Date: Fri Oct 2 08:16:35 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 4f2a18a10e89afa485f4023bf31f2bb120a2d54d Source-Link: https://github.com/googleapis/googleapis/commit/4f2a18a10e89afa485f4023bf31f2bb120a2d54d * chore: upgrade to gapic-generator 0.35.9 PiperOrigin-RevId: 339292950 Source-Author: Google APIs Source-Date: Tue Oct 27 11:32:46 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 07d41a7e5cade45aba6f0d277c89722b48f2c956 Source-Link: https://github.com/googleapis/googleapis/commit/07d41a7e5cade45aba6f0d277c89722b48f2c956 * fix: remove client recv msg limit fix: add enums to `types/__init__.py` PiperOrigin-RevId: 347055288 Source-Author: Google APIs Source-Date: Fri Dec 11 12:44:37 2020 -0800 Source-Repo: googleapis/googleapis Source-Sha: dd372aa22ded7a8ba6f0e03a80e06358a3fa0907 Source-Link: https://github.com/googleapis/googleapis/commit/dd372aa22ded7a8ba6f0e03a80e06358a3fa0907 * chore: Fix C# and PHP namespaces and Ruby package This is a breaking change for anyone generating C#, PHP or Ruby themselves, but Google Cloud Client Libraries have not been published in those languages. This is effectively a no-op (modulo protobuf reflection) in other languages. PiperOrigin-RevId: 348813319 Source-Author: Google APIs Source-Date: Wed Dec 23 10:18:11 2020 -0800 Source-Repo: googleapis/googleapis Source-Sha: 57fc4a8a94a5bd015a83fb0f0a1707f62254b2cd Source-Link: https://github.com/googleapis/googleapis/commit/57fc4a8a94a5bd015a83fb0f0a1707f62254b2cd * chore: clean up dependencies Co-authored-by: Bu Sun Kim Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> --- UPGRADING.MD | 4 +- docs/webrisk_v1/types.rst | 1 + docs/webrisk_v1beta1/types.rst | 1 + .../services/web_risk_service/async_client.py | 131 ++- .../services/web_risk_service/client.py | 286 ++++-- .../web_risk_service/transports/__init__.py | 1 - .../web_risk_service/transports/base.py | 79 +- .../web_risk_service/transports/grpc.py | 99 +- .../transports/grpc_asyncio.py | 88 +- google/cloud/webrisk_v1/types/__init__.py | 5 +- google/cloud/webrisk_v1/types/webrisk.py | 6 +- .../web_risk_service_v1_beta1/async_client.py | 126 ++- .../web_risk_service_v1_beta1/client.py | 258 ++++-- .../transports/__init__.py | 1 - .../transports/base.py | 76 +- .../transports/grpc.py | 99 +- .../transports/grpc_asyncio.py | 88 +- .../cloud/webrisk_v1beta1/types/__init__.py | 5 +- google/cloud/webrisk_v1beta1/types/webrisk.py | 4 +- scripts/fixup_webrisk_v1_keywords.py | 2 + scripts/fixup_webrisk_v1beta1_keywords.py | 2 + setup.py | 5 +- synth.metadata | 8 +- synth.py | 3 - tests/unit/gapic/webrisk_v1/__init__.py | 1 + .../gapic/webrisk_v1/test_web_risk_service.py | 864 ++++++++++++------ tests/unit/gapic/webrisk_v1beta1/__init__.py | 1 + .../test_web_risk_service_v1_beta1.py | 832 +++++++++++------ webrisk-v1-py.tar.gz | 0 29 files changed, 2196 insertions(+), 880 deletions(-) create mode 100644 webrisk-v1-py.tar.gz diff --git a/UPGRADING.MD b/UPGRADING.MD index edf2153..4a211e0 100644 --- a/UPGRADING.MD +++ b/UPGRADING.MD @@ -17,10 +17,10 @@ The 2.0.0 release requires Python 3.6+. Methods expect request objects. We provide a script that will convert most common use cases. -* Install the library +* Install the library and `libcst >= 0.2.5`. ```py -python3 -m pip install google-cloud-webrisk +python3 -m pip install google-cloud-webrisk libcst >= 0.2.5 ``` * The scripts `fixup_webrisk_v1_keywords.py` and `fixup_webrisk_v1beta1_keywords.py` shipped with the library. It expects diff --git a/docs/webrisk_v1/types.rst b/docs/webrisk_v1/types.rst index a19ca60..d5b56e6 100644 --- a/docs/webrisk_v1/types.rst +++ b/docs/webrisk_v1/types.rst @@ -3,3 +3,4 @@ Types for Google Cloud Webrisk v1 API .. automodule:: google.cloud.webrisk_v1.types :members: + :show-inheritance: diff --git a/docs/webrisk_v1beta1/types.rst b/docs/webrisk_v1beta1/types.rst index ffb6ace..d6aefb6 100644 --- a/docs/webrisk_v1beta1/types.rst +++ b/docs/webrisk_v1beta1/types.rst @@ -3,3 +3,4 @@ Types for Google Cloud Webrisk v1beta1 API .. automodule:: google.cloud.webrisk_v1beta1.types :members: + :show-inheritance: 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 667f71a..8747552 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 @@ -31,7 +31,7 @@ from google.cloud.webrisk_v1.types import webrisk from google.protobuf import timestamp_pb2 as timestamp # type: ignore -from .transports.base import WebRiskServiceTransport +from .transports.base import WebRiskServiceTransport, DEFAULT_CLIENT_INFO from .transports.grpc_asyncio import WebRiskServiceGrpcAsyncIOTransport from .client import WebRiskServiceClient @@ -46,9 +46,47 @@ class WebRiskServiceAsyncClient: DEFAULT_ENDPOINT = WebRiskServiceClient.DEFAULT_ENDPOINT DEFAULT_MTLS_ENDPOINT = WebRiskServiceClient.DEFAULT_MTLS_ENDPOINT + common_billing_account_path = staticmethod( + WebRiskServiceClient.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + WebRiskServiceClient.parse_common_billing_account_path + ) + + common_folder_path = staticmethod(WebRiskServiceClient.common_folder_path) + parse_common_folder_path = staticmethod( + WebRiskServiceClient.parse_common_folder_path + ) + + common_organization_path = staticmethod( + WebRiskServiceClient.common_organization_path + ) + parse_common_organization_path = staticmethod( + WebRiskServiceClient.parse_common_organization_path + ) + + common_project_path = staticmethod(WebRiskServiceClient.common_project_path) + parse_common_project_path = staticmethod( + WebRiskServiceClient.parse_common_project_path + ) + + common_location_path = staticmethod(WebRiskServiceClient.common_location_path) + parse_common_location_path = staticmethod( + WebRiskServiceClient.parse_common_location_path + ) + from_service_account_file = WebRiskServiceClient.from_service_account_file from_service_account_json = from_service_account_file + @property + def transport(self) -> WebRiskServiceTransport: + """Return the transport used by the client instance. + + Returns: + WebRiskServiceTransport: The transport used by the client instance. + """ + return self._client.transport + get_transport_class = functools.partial( type(WebRiskServiceClient).get_transport_class, type(WebRiskServiceClient) ) @@ -59,6 +97,7 @@ def __init__( credentials: credentials.Credentials = None, transport: Union[str, WebRiskServiceTransport] = "grpc_asyncio", client_options: ClientOptions = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the web risk service client. @@ -74,16 +113,19 @@ def __init__( client_options (ClientOptions): Custom options for the client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport @@ -91,7 +133,10 @@ def __init__( """ self._client = WebRiskServiceClient( - credentials=credentials, transport=transport, client_options=client_options, + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, ) async def compute_threat_list_diff( @@ -157,7 +202,8 @@ async def compute_threat_list_diff( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([threat_type, version_token, constraints]): + has_flattened_params = any([threat_type, version_token, constraints]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." @@ -179,8 +225,16 @@ async def compute_threat_list_diff( # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.compute_threat_list_diff, - default_timeout=None, - client_info=_client_info, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=DEFAULT_CLIENT_INFO, ) # Send the request. @@ -237,7 +291,8 @@ async def search_uris( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([uri, threat_types]): + has_flattened_params = any([uri, threat_types]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." @@ -250,15 +305,24 @@ async def search_uris( if uri is not None: request.uri = uri - if threat_types is not None: - request.threat_types = threat_types + + if threat_types: + request.threat_types.extend(threat_types) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.search_uris, - default_timeout=None, - client_info=_client_info, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=DEFAULT_CLIENT_INFO, ) # Send the request. @@ -317,7 +381,8 @@ async def search_hashes( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([hash_prefix, threat_types]): + has_flattened_params = any([hash_prefix, threat_types]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." @@ -330,15 +395,24 @@ async def search_hashes( if hash_prefix is not None: request.hash_prefix = hash_prefix - if threat_types is not None: - request.threat_types = threat_types + + if threat_types: + request.threat_types.extend(threat_types) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.search_hashes, - default_timeout=None, - client_info=_client_info, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=DEFAULT_CLIENT_INFO, ) # Send the request. @@ -400,7 +474,8 @@ async def create_submission( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([parent, submission]): + has_flattened_params = any([parent, submission]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." @@ -420,8 +495,8 @@ async def create_submission( # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.create_submission, - default_timeout=None, - client_info=_client_info, + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, ) # Certain fields should be provided within the metadata header; @@ -438,11 +513,11 @@ async def create_submission( try: - _client_info = gapic_v1.client_info.ClientInfo( + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( gapic_version=pkg_resources.get_distribution("google-cloud-webrisk",).version, ) except pkg_resources.DistributionNotFound: - _client_info = gapic_v1.client_info.ClientInfo() + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() __all__ = ("WebRiskServiceAsyncClient",) 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 2926404..d5793d7 100644 --- a/google/cloud/webrisk_v1/services/web_risk_service/client.py +++ b/google/cloud/webrisk_v1/services/web_risk_service/client.py @@ -16,24 +16,26 @@ # from collections import OrderedDict +from distutils import util import os import re -from typing import Callable, Dict, Sequence, Tuple, Type, Union +from typing import Callable, Dict, Optional, Sequence, Tuple, Type, Union import pkg_resources -import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import client_options as client_options_lib # type: ignore from google.api_core import exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore from google.auth import credentials # type: ignore from google.auth.transport import mtls # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore from google.cloud.webrisk_v1.types import webrisk from google.protobuf import timestamp_pb2 as timestamp # type: ignore -from .transports.base import WebRiskServiceTransport +from .transports.base import WebRiskServiceTransport, DEFAULT_CLIENT_INFO from .transports.grpc import WebRiskServiceGrpcTransport from .transports.grpc_asyncio import WebRiskServiceGrpcAsyncIOTransport @@ -130,12 +132,81 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): from_service_account_json = from_service_account_file + @property + def transport(self) -> WebRiskServiceTransport: + """Return the transport used by the client instance. + + Returns: + WebRiskServiceTransport: The transport used by the client instance. + """ + return self._transport + + @staticmethod + def common_billing_account_path(billing_account: str,) -> str: + """Return a fully-qualified billing_account string.""" + return "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + + @staticmethod + def parse_common_billing_account_path(path: str) -> Dict[str, str]: + """Parse a billing_account path into its component segments.""" + m = re.match(r"^billingAccounts/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_folder_path(folder: str,) -> str: + """Return a fully-qualified folder string.""" + return "folders/{folder}".format(folder=folder,) + + @staticmethod + def parse_common_folder_path(path: str) -> Dict[str, str]: + """Parse a folder path into its component segments.""" + m = re.match(r"^folders/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_organization_path(organization: str,) -> str: + """Return a fully-qualified organization string.""" + return "organizations/{organization}".format(organization=organization,) + + @staticmethod + def parse_common_organization_path(path: str) -> Dict[str, str]: + """Parse a organization path into its component segments.""" + m = re.match(r"^organizations/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_project_path(project: str,) -> str: + """Return a fully-qualified project string.""" + return "projects/{project}".format(project=project,) + + @staticmethod + def parse_common_project_path(path: str) -> Dict[str, str]: + """Parse a project path into its component segments.""" + m = re.match(r"^projects/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_location_path(project: str, location: str,) -> str: + """Return a fully-qualified location string.""" + return "projects/{project}/locations/{location}".format( + project=project, location=location, + ) + + @staticmethod + def parse_common_location_path(path: str) -> Dict[str, str]: + """Parse a location path into its component segments.""" + m = re.match(r"^projects/(?P.+?)/locations/(?P.+?)$", path) + return m.groupdict() if m else {} + def __init__( self, *, - credentials: credentials.Credentials = None, - transport: Union[str, WebRiskServiceTransport] = None, - client_options: ClientOptions = None, + credentials: Optional[credentials.Credentials] = None, + transport: Union[str, WebRiskServiceTransport, None] = None, + client_options: Optional[client_options_lib.ClientOptions] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the web risk service client. @@ -148,48 +219,74 @@ def __init__( transport (Union[str, ~.WebRiskServiceTransport]): The transport to use. If set to None, a transport is chosen automatically. - client_options (ClientOptions): Custom options for the client. It - won't take effect if a ``transport`` instance is provided. + client_options (client_options_lib.ClientOptions): Custom options for the + client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport creation failed for any reason. """ if isinstance(client_options, dict): - client_options = ClientOptions.from_dict(client_options) + client_options = client_options_lib.from_dict(client_options) if client_options is None: - client_options = ClientOptions.ClientOptions() + client_options = client_options_lib.ClientOptions() + + # Create SSL credentials for mutual TLS if needed. + use_client_cert = bool( + util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) + ) + + ssl_credentials = None + is_mtls = False + if use_client_cert: + if client_options.client_cert_source: + import grpc # type: ignore + + cert, key = client_options.client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + is_mtls = True + else: + creds = SslCredentials() + is_mtls = creds.is_mtls + ssl_credentials = creds.ssl_credentials if is_mtls else None - if client_options.api_endpoint is None: - use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS", "never") + # Figure out which api endpoint to use. + if client_options.api_endpoint is not None: + api_endpoint = client_options.api_endpoint + else: + use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_env == "never": - client_options.api_endpoint = self.DEFAULT_ENDPOINT + api_endpoint = self.DEFAULT_ENDPOINT elif use_mtls_env == "always": - client_options.api_endpoint = self.DEFAULT_MTLS_ENDPOINT + api_endpoint = self.DEFAULT_MTLS_ENDPOINT elif use_mtls_env == "auto": - has_client_cert_source = ( - client_options.client_cert_source is not None - or mtls.has_default_client_cert_source() - ) - client_options.api_endpoint = ( - self.DEFAULT_MTLS_ENDPOINT - if has_client_cert_source - else self.DEFAULT_ENDPOINT + api_endpoint = ( + self.DEFAULT_MTLS_ENDPOINT if is_mtls else self.DEFAULT_ENDPOINT ) else: raise MutualTLSChannelError( - "Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always" + "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always" ) # Save or instantiate the transport. @@ -213,10 +310,11 @@ def __init__( self._transport = Transport( credentials=credentials, credentials_file=client_options.credentials_file, - host=client_options.api_endpoint, + host=api_endpoint, scopes=client_options.scopes, - api_mtls_endpoint=client_options.api_endpoint, - client_cert_source=client_options.client_cert_source, + ssl_channel_credentials=ssl_credentials, + quota_project_id=client_options.quota_project_id, + client_info=client_info, ) def compute_threat_list_diff( @@ -282,31 +380,33 @@ def compute_threat_list_diff( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([threat_type, version_token, constraints]): + has_flattened_params = any([threat_type, version_token, constraints]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = webrisk.ComputeThreatListDiffRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a webrisk.ComputeThreatListDiffRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, webrisk.ComputeThreatListDiffRequest): + request = webrisk.ComputeThreatListDiffRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if threat_type is not None: - request.threat_type = threat_type - if version_token is not None: - request.version_token = version_token - if constraints is not None: - request.constraints = constraints + if threat_type is not None: + request.threat_type = threat_type + if version_token is not None: + request.version_token = version_token + if constraints is not None: + request.constraints = constraints # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.compute_threat_list_diff, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.compute_threat_list_diff] # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) @@ -362,27 +462,32 @@ def search_uris( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([uri, threat_types]): + has_flattened_params = any([uri, threat_types]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = webrisk.SearchUrisRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a webrisk.SearchUrisRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, webrisk.SearchUrisRequest): + request = webrisk.SearchUrisRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if uri is not None: - request.uri = uri - if threat_types is not None: - request.threat_types = threat_types + if uri is not None: + request.uri = uri + + if threat_types: + request.threat_types.extend(threat_types) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.search_uris, default_timeout=None, client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.search_uris] # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) @@ -440,29 +545,32 @@ def search_hashes( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([hash_prefix, threat_types]): + has_flattened_params = any([hash_prefix, threat_types]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = webrisk.SearchHashesRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a webrisk.SearchHashesRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, webrisk.SearchHashesRequest): + request = webrisk.SearchHashesRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. - # If we have keyword arguments corresponding to fields on the - # request, apply these. + if hash_prefix is not None: + request.hash_prefix = hash_prefix - if hash_prefix is not None: - request.hash_prefix = hash_prefix - if threat_types is not None: - request.threat_types = threat_types + if threat_types: + request.threat_types.extend(threat_types) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.search_hashes, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.search_hashes] # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) @@ -523,29 +631,31 @@ def create_submission( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([parent, submission]): + has_flattened_params = any([parent, submission]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = webrisk.CreateSubmissionRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a webrisk.CreateSubmissionRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, webrisk.CreateSubmissionRequest): + request = webrisk.CreateSubmissionRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if parent is not None: - request.parent = parent - if submission is not None: - request.submission = submission + if parent is not None: + request.parent = parent + if submission is not None: + request.submission = submission # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.create_submission, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.create_submission] # Certain fields should be provided within the metadata header; # add these here. @@ -561,11 +671,11 @@ def create_submission( try: - _client_info = gapic_v1.client_info.ClientInfo( + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( gapic_version=pkg_resources.get_distribution("google-cloud-webrisk",).version, ) except pkg_resources.DistributionNotFound: - _client_info = gapic_v1.client_info.ClientInfo() + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() __all__ = ("WebRiskServiceClient",) diff --git a/google/cloud/webrisk_v1/services/web_risk_service/transports/__init__.py b/google/cloud/webrisk_v1/services/web_risk_service/transports/__init__.py index f39de6c..5b2203f 100644 --- a/google/cloud/webrisk_v1/services/web_risk_service/transports/__init__.py +++ b/google/cloud/webrisk_v1/services/web_risk_service/transports/__init__.py @@ -28,7 +28,6 @@ _transport_registry["grpc"] = WebRiskServiceGrpcTransport _transport_registry["grpc_asyncio"] = WebRiskServiceGrpcAsyncIOTransport - __all__ = ( "WebRiskServiceTransport", "WebRiskServiceGrpcTransport", 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 f87e245..317ae12 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 @@ -17,14 +17,25 @@ import abc import typing +import pkg_resources -from google import auth +from google import auth # type: ignore from google.api_core import exceptions # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google.api_core import retry as retries # type: ignore from google.auth import credentials # type: ignore from google.cloud.webrisk_v1.types import webrisk +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution("google-cloud-webrisk",).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + class WebRiskServiceTransport(abc.ABC): """Abstract transport class for WebRiskService.""" @@ -37,6 +48,8 @@ def __init__( credentials: credentials.Credentials = None, credentials_file: typing.Optional[str] = None, scopes: typing.Optional[typing.Sequence[str]] = AUTH_SCOPES, + quota_project_id: typing.Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, **kwargs, ) -> None: """Instantiate the transport. @@ -52,6 +65,13 @@ def __init__( be loaded with :func:`google.auth.load_credentials_from_file`. This argument is mutually exclusive with credentials. scope (Optional[Sequence[str]]): A list of scopes. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. """ # Save the hostname. Default to port 443 (HTTPS) if none is specified. if ":" not in host: @@ -67,14 +87,67 @@ def __init__( if credentials_file is not None: credentials, _ = auth.load_credentials_from_file( - credentials_file, scopes=scopes + credentials_file, scopes=scopes, quota_project_id=quota_project_id ) + elif credentials is None: - credentials, _ = auth.default(scopes=scopes) + credentials, _ = auth.default( + scopes=scopes, quota_project_id=quota_project_id + ) # Save the credentials. self._credentials = credentials + # Lifted into its own function so it can be stubbed out during tests. + self._prep_wrapped_messages(client_info) + + def _prep_wrapped_messages(self, client_info): + # Precompute the wrapped methods. + self._wrapped_methods = { + self.compute_threat_list_diff: gapic_v1.method.wrap_method( + self.compute_threat_list_diff, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=client_info, + ), + self.search_uris: gapic_v1.method.wrap_method( + self.search_uris, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=client_info, + ), + self.search_hashes: gapic_v1.method.wrap_method( + self.search_hashes, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=client_info, + ), + self.create_submission: gapic_v1.method.wrap_method( + self.create_submission, default_timeout=60.0, client_info=client_info, + ), + } + @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 8a44485..536093a 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 @@ -15,19 +15,20 @@ # limitations under the License. # +import warnings from typing import Callable, Dict, Optional, Sequence, Tuple from google.api_core import grpc_helpers # type: ignore +from google.api_core import gapic_v1 # type: ignore from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore - import grpc # type: ignore from google.cloud.webrisk_v1.types import webrisk -from .base import WebRiskServiceTransport +from .base import WebRiskServiceTransport, DEFAULT_CLIENT_INFO class WebRiskServiceGrpcTransport(WebRiskServiceTransport): @@ -55,7 +56,10 @@ def __init__( scopes: Sequence[str] = None, channel: grpc.Channel = None, api_mtls_endpoint: str = None, - client_cert_source: Callable[[], Tuple[bytes, bytes]] = None + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the transport. @@ -74,14 +78,23 @@ def __init__( ignored if ``channel`` is provided. channel (Optional[grpc.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport @@ -89,6 +102,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -96,7 +111,13 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint @@ -104,7 +125,9 @@ def __init__( ) if credentials is None: - credentials, _ = auth.default(scopes=self.AUTH_SCOPES) + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) # Create SSL credentials with client_cert_source or application # default SSL credentials. @@ -123,7 +146,36 @@ def __init__( credentials_file=credentials_file, ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + self._stubs = {} # type: Dict[str, Callable] # Run the base constructor. super().__init__( @@ -131,10 +183,10 @@ def __init__( credentials=credentials, credentials_file=credentials_file, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, ) - self._stubs = {} # type: Dict[str, Callable] - @classmethod def create_channel( cls, @@ -142,11 +194,12 @@ def create_channel( credentials: credentials.Credentials = None, credentials_file: str = None, scopes: Optional[Sequence[str]] = None, - **kwargs + quota_project_id: Optional[str] = None, + **kwargs, ) -> grpc.Channel: """Create and return a gRPC channel object. Args: - address (Optionsl[str]): The host for the channel to use. + address (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If @@ -158,6 +211,8 @@ def create_channel( scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: @@ -173,24 +228,14 @@ def create_channel( credentials=credentials, credentials_file=credentials_file, scopes=scopes, - **kwargs + quota_project_id=quota_project_id, + **kwargs, ) @property def grpc_channel(self) -> grpc.Channel: - """Create the channel designed to connect to this service. - - This property caches on the instance; repeated calls return - the same channel. + """Return the channel designed to connect to this service. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - - # Return the channel from cache. return self._grpc_channel @property 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 1e8ae0c..1f0b4ed 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 @@ -15,9 +15,12 @@ # limitations under the License. # +import warnings from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple +from google.api_core import gapic_v1 # type: ignore from google.api_core import grpc_helpers_async # type: ignore +from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore @@ -26,7 +29,7 @@ from google.cloud.webrisk_v1.types import webrisk -from .base import WebRiskServiceTransport +from .base import WebRiskServiceTransport, DEFAULT_CLIENT_INFO from .grpc import WebRiskServiceGrpcTransport @@ -54,7 +57,8 @@ def create_channel( credentials: credentials.Credentials = None, credentials_file: Optional[str] = None, scopes: Optional[Sequence[str]] = None, - **kwargs + quota_project_id: Optional[str] = None, + **kwargs, ) -> aio.Channel: """Create and return a gRPC AsyncIO channel object. Args: @@ -70,6 +74,8 @@ def create_channel( scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: @@ -81,7 +87,8 @@ def create_channel( credentials=credentials, credentials_file=credentials_file, scopes=scopes, - **kwargs + quota_project_id=quota_project_id, + **kwargs, ) def __init__( @@ -93,7 +100,10 @@ def __init__( scopes: Optional[Sequence[str]] = None, channel: aio.Channel = None, api_mtls_endpoint: str = None, - client_cert_source: Callable[[], Tuple[bytes, bytes]] = None + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, + quota_project_id=None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the transport. @@ -113,14 +123,23 @@ def __init__( are passed to :func:`google.auth.default`. channel (Optional[aio.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport @@ -128,6 +147,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -135,13 +156,24 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443" ) + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + # Create SSL credentials with client_cert_source or application # default SSL credentials. if client_cert_source: @@ -159,6 +191,33 @@ def __init__( credentials_file=credentials_file, ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], ) # Run the base constructor. @@ -167,6 +226,8 @@ def __init__( credentials=credentials, credentials_file=credentials_file, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, ) self._stubs = {} @@ -178,13 +239,6 @@ def grpc_channel(self) -> aio.Channel: This property caches on the instance; repeated calls return the same channel. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - # Return the channel from cache. return self._grpc_channel diff --git a/google/cloud/webrisk_v1/types/__init__.py b/google/cloud/webrisk_v1/types/__init__.py index 07ef2cc..7333db4 100644 --- a/google/cloud/webrisk_v1/types/__init__.py +++ b/google/cloud/webrisk_v1/types/__init__.py @@ -29,9 +29,10 @@ RiceDeltaEncoding, Submission, CreateSubmissionRequest, + ThreatType, + CompressionType, ) - __all__ = ( "ComputeThreatListDiffRequest", "ComputeThreatListDiffResponse", @@ -46,4 +47,6 @@ "RiceDeltaEncoding", "Submission", "CreateSubmissionRequest", + "ThreatType", + "CompressionType", ) diff --git a/google/cloud/webrisk_v1/types/webrisk.py b/google/cloud/webrisk_v1/types/webrisk.py index 0c0717d..bdc7628 100644 --- a/google/cloud/webrisk_v1/types/webrisk.py +++ b/google/cloud/webrisk_v1/types/webrisk.py @@ -265,7 +265,7 @@ class ThreatHash(proto.Message): threat_types (Sequence[~.webrisk.ThreatType]): The ThreatList this threat belongs to. This must contain at least one entry. - hash (bytes): + hash_ (bytes): A 32 byte SHA256 hash. This field is in binary format. For JSON requests, hashes are base64-encoded. @@ -277,7 +277,7 @@ class ThreatHash(proto.Message): threat_types = proto.RepeatedField(proto.ENUM, number=1, enum="ThreatType",) - hash = proto.Field(proto.BYTES, number=2) + hash_ = proto.Field(proto.BYTES, number=2) expire_time = proto.Field(proto.MESSAGE, number=3, message=timestamp.Timestamp,) @@ -429,7 +429,7 @@ class CreateSubmissionRequest(proto.Message): parent = proto.Field(proto.STRING, number=1) - submission = proto.Field(proto.MESSAGE, number=2, message=Submission,) + submission = proto.Field(proto.MESSAGE, number=2, message="Submission",) __all__ = tuple(sorted(__protobuf__.manifest)) 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 b56c370..3804db5 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 @@ -31,7 +31,7 @@ from google.cloud.webrisk_v1beta1.types import webrisk from google.protobuf import timestamp_pb2 as timestamp # type: ignore -from .transports.base import WebRiskServiceV1Beta1Transport +from .transports.base import WebRiskServiceV1Beta1Transport, DEFAULT_CLIENT_INFO from .transports.grpc_asyncio import WebRiskServiceV1Beta1GrpcAsyncIOTransport from .client import WebRiskServiceV1Beta1Client @@ -46,9 +46,49 @@ class WebRiskServiceV1Beta1AsyncClient: DEFAULT_ENDPOINT = WebRiskServiceV1Beta1Client.DEFAULT_ENDPOINT DEFAULT_MTLS_ENDPOINT = WebRiskServiceV1Beta1Client.DEFAULT_MTLS_ENDPOINT + common_billing_account_path = staticmethod( + WebRiskServiceV1Beta1Client.common_billing_account_path + ) + parse_common_billing_account_path = staticmethod( + WebRiskServiceV1Beta1Client.parse_common_billing_account_path + ) + + common_folder_path = staticmethod(WebRiskServiceV1Beta1Client.common_folder_path) + parse_common_folder_path = staticmethod( + WebRiskServiceV1Beta1Client.parse_common_folder_path + ) + + common_organization_path = staticmethod( + WebRiskServiceV1Beta1Client.common_organization_path + ) + parse_common_organization_path = staticmethod( + WebRiskServiceV1Beta1Client.parse_common_organization_path + ) + + common_project_path = staticmethod(WebRiskServiceV1Beta1Client.common_project_path) + parse_common_project_path = staticmethod( + WebRiskServiceV1Beta1Client.parse_common_project_path + ) + + common_location_path = staticmethod( + WebRiskServiceV1Beta1Client.common_location_path + ) + parse_common_location_path = staticmethod( + WebRiskServiceV1Beta1Client.parse_common_location_path + ) + from_service_account_file = WebRiskServiceV1Beta1Client.from_service_account_file from_service_account_json = from_service_account_file + @property + def transport(self) -> WebRiskServiceV1Beta1Transport: + """Return the transport used by the client instance. + + Returns: + WebRiskServiceV1Beta1Transport: The transport used by the client instance. + """ + return self._client.transport + get_transport_class = functools.partial( type(WebRiskServiceV1Beta1Client).get_transport_class, type(WebRiskServiceV1Beta1Client), @@ -60,6 +100,7 @@ def __init__( credentials: credentials.Credentials = None, transport: Union[str, WebRiskServiceV1Beta1Transport] = "grpc_asyncio", client_options: ClientOptions = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the web risk service v1 beta1 client. @@ -75,16 +116,19 @@ def __init__( client_options (ClientOptions): Custom options for the client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport @@ -92,7 +136,10 @@ def __init__( """ self._client = WebRiskServiceV1Beta1Client( - credentials=credentials, transport=transport, client_options=client_options, + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, ) async def compute_threat_list_diff( @@ -144,7 +191,8 @@ async def compute_threat_list_diff( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([threat_type, version_token, constraints]): + has_flattened_params = any([threat_type, version_token, constraints]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." @@ -166,8 +214,16 @@ async def compute_threat_list_diff( # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.compute_threat_list_diff, - default_timeout=None, - client_info=_client_info, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=DEFAULT_CLIENT_INFO, ) # Send the request. @@ -219,7 +275,8 @@ async def search_uris( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([uri, threat_types]): + has_flattened_params = any([uri, threat_types]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." @@ -232,15 +289,24 @@ async def search_uris( if uri is not None: request.uri = uri - if threat_types is not None: - request.threat_types = threat_types + + if threat_types: + request.threat_types.extend(threat_types) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.search_uris, - default_timeout=None, - client_info=_client_info, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=DEFAULT_CLIENT_INFO, ) # Send the request. @@ -298,7 +364,8 @@ async def search_hashes( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([hash_prefix, threat_types]): + has_flattened_params = any([hash_prefix, threat_types]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." @@ -311,15 +378,24 @@ async def search_hashes( if hash_prefix is not None: request.hash_prefix = hash_prefix - if threat_types is not None: - request.threat_types = threat_types + + if threat_types: + request.threat_types.extend(threat_types) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.search_hashes, - default_timeout=None, - client_info=_client_info, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=DEFAULT_CLIENT_INFO, ) # Send the request. @@ -330,11 +406,11 @@ async def search_hashes( try: - _client_info = gapic_v1.client_info.ClientInfo( + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( gapic_version=pkg_resources.get_distribution("google-cloud-webrisk",).version, ) except pkg_resources.DistributionNotFound: - _client_info = gapic_v1.client_info.ClientInfo() + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() __all__ = ("WebRiskServiceV1Beta1AsyncClient",) 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 527db93..d3acddd 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 @@ -16,24 +16,26 @@ # from collections import OrderedDict +from distutils import util import os import re -from typing import Callable, Dict, Sequence, Tuple, Type, Union +from typing import Callable, Dict, Optional, Sequence, Tuple, Type, Union import pkg_resources -import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import client_options as client_options_lib # type: ignore from google.api_core import exceptions # type: ignore from google.api_core import gapic_v1 # type: ignore from google.api_core import retry as retries # type: ignore from google.auth import credentials # type: ignore from google.auth.transport import mtls # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore from google.cloud.webrisk_v1beta1.types import webrisk from google.protobuf import timestamp_pb2 as timestamp # type: ignore -from .transports.base import WebRiskServiceV1Beta1Transport +from .transports.base import WebRiskServiceV1Beta1Transport, DEFAULT_CLIENT_INFO from .transports.grpc import WebRiskServiceV1Beta1GrpcTransport from .transports.grpc_asyncio import WebRiskServiceV1Beta1GrpcAsyncIOTransport @@ -132,12 +134,81 @@ def from_service_account_file(cls, filename: str, *args, **kwargs): from_service_account_json = from_service_account_file + @property + def transport(self) -> WebRiskServiceV1Beta1Transport: + """Return the transport used by the client instance. + + Returns: + WebRiskServiceV1Beta1Transport: The transport used by the client instance. + """ + return self._transport + + @staticmethod + def common_billing_account_path(billing_account: str,) -> str: + """Return a fully-qualified billing_account string.""" + return "billingAccounts/{billing_account}".format( + billing_account=billing_account, + ) + + @staticmethod + def parse_common_billing_account_path(path: str) -> Dict[str, str]: + """Parse a billing_account path into its component segments.""" + m = re.match(r"^billingAccounts/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_folder_path(folder: str,) -> str: + """Return a fully-qualified folder string.""" + return "folders/{folder}".format(folder=folder,) + + @staticmethod + def parse_common_folder_path(path: str) -> Dict[str, str]: + """Parse a folder path into its component segments.""" + m = re.match(r"^folders/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_organization_path(organization: str,) -> str: + """Return a fully-qualified organization string.""" + return "organizations/{organization}".format(organization=organization,) + + @staticmethod + def parse_common_organization_path(path: str) -> Dict[str, str]: + """Parse a organization path into its component segments.""" + m = re.match(r"^organizations/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_project_path(project: str,) -> str: + """Return a fully-qualified project string.""" + return "projects/{project}".format(project=project,) + + @staticmethod + def parse_common_project_path(path: str) -> Dict[str, str]: + """Parse a project path into its component segments.""" + m = re.match(r"^projects/(?P.+?)$", path) + return m.groupdict() if m else {} + + @staticmethod + def common_location_path(project: str, location: str,) -> str: + """Return a fully-qualified location string.""" + return "projects/{project}/locations/{location}".format( + project=project, location=location, + ) + + @staticmethod + def parse_common_location_path(path: str) -> Dict[str, str]: + """Parse a location path into its component segments.""" + m = re.match(r"^projects/(?P.+?)/locations/(?P.+?)$", path) + return m.groupdict() if m else {} + def __init__( self, *, - credentials: credentials.Credentials = None, - transport: Union[str, WebRiskServiceV1Beta1Transport] = None, - client_options: ClientOptions = None, + credentials: Optional[credentials.Credentials] = None, + transport: Union[str, WebRiskServiceV1Beta1Transport, None] = None, + client_options: Optional[client_options_lib.ClientOptions] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the web risk service v1 beta1 client. @@ -150,48 +221,74 @@ def __init__( transport (Union[str, ~.WebRiskServiceV1Beta1Transport]): The transport to use. If set to None, a transport is chosen automatically. - client_options (ClientOptions): Custom options for the client. It - won't take effect if a ``transport`` instance is provided. + client_options (client_options_lib.ClientOptions): Custom options for the + client. It won't take effect if a ``transport`` instance is provided. (1) The ``api_endpoint`` property can be used to override the - default endpoint provided by the client. GOOGLE_API_USE_MTLS + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT environment variable can also be used to override the endpoint: "always" (always use the default mTLS endpoint), "never" (always - use the default regular endpoint, this is the default value for - the environment variable) and "auto" (auto switch to the default - mTLS endpoint if client SSL credentials is present). However, - the ``api_endpoint`` property takes precedence if provided. - (2) The ``client_cert_source`` property is used to provide client - SSL credentials for mutual TLS transport. If not provided, the - default SSL credentials will be used if present. + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport creation failed for any reason. """ if isinstance(client_options, dict): - client_options = ClientOptions.from_dict(client_options) + client_options = client_options_lib.from_dict(client_options) if client_options is None: - client_options = ClientOptions.ClientOptions() + client_options = client_options_lib.ClientOptions() + + # Create SSL credentials for mutual TLS if needed. + use_client_cert = bool( + util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) + ) - if client_options.api_endpoint is None: - use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS", "never") + ssl_credentials = None + is_mtls = False + if use_client_cert: + if client_options.client_cert_source: + import grpc # type: ignore + + cert, key = client_options.client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + is_mtls = True + else: + creds = SslCredentials() + is_mtls = creds.is_mtls + ssl_credentials = creds.ssl_credentials if is_mtls else None + + # Figure out which api endpoint to use. + if client_options.api_endpoint is not None: + api_endpoint = client_options.api_endpoint + else: + use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") if use_mtls_env == "never": - client_options.api_endpoint = self.DEFAULT_ENDPOINT + api_endpoint = self.DEFAULT_ENDPOINT elif use_mtls_env == "always": - client_options.api_endpoint = self.DEFAULT_MTLS_ENDPOINT + api_endpoint = self.DEFAULT_MTLS_ENDPOINT elif use_mtls_env == "auto": - has_client_cert_source = ( - client_options.client_cert_source is not None - or mtls.has_default_client_cert_source() - ) - client_options.api_endpoint = ( - self.DEFAULT_MTLS_ENDPOINT - if has_client_cert_source - else self.DEFAULT_ENDPOINT + api_endpoint = ( + self.DEFAULT_MTLS_ENDPOINT if is_mtls else self.DEFAULT_ENDPOINT ) else: raise MutualTLSChannelError( - "Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always" + "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always" ) # Save or instantiate the transport. @@ -215,10 +312,11 @@ def __init__( self._transport = Transport( credentials=credentials, credentials_file=client_options.credentials_file, - host=client_options.api_endpoint, + host=api_endpoint, scopes=client_options.scopes, - api_mtls_endpoint=client_options.api_endpoint, - client_cert_source=client_options.client_cert_source, + ssl_channel_credentials=ssl_credentials, + quota_project_id=client_options.quota_project_id, + client_info=client_info, ) def compute_threat_list_diff( @@ -270,31 +368,33 @@ def compute_threat_list_diff( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([threat_type, version_token, constraints]): + has_flattened_params = any([threat_type, version_token, constraints]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = webrisk.ComputeThreatListDiffRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a webrisk.ComputeThreatListDiffRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, webrisk.ComputeThreatListDiffRequest): + request = webrisk.ComputeThreatListDiffRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if threat_type is not None: - request.threat_type = threat_type - if version_token is not None: - request.version_token = version_token - if constraints is not None: - request.constraints = constraints + if threat_type is not None: + request.threat_type = threat_type + if version_token is not None: + request.version_token = version_token + if constraints is not None: + request.constraints = constraints # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.compute_threat_list_diff, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.compute_threat_list_diff] # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) @@ -345,27 +445,32 @@ def search_uris( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([uri, threat_types]): + has_flattened_params = any([uri, threat_types]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = webrisk.SearchUrisRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a webrisk.SearchUrisRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, webrisk.SearchUrisRequest): + request = webrisk.SearchUrisRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if uri is not None: - request.uri = uri - if threat_types is not None: - request.threat_types = threat_types + if uri is not None: + request.uri = uri + + if threat_types: + request.threat_types.extend(threat_types) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.search_uris, default_timeout=None, client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.search_uris] # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) @@ -422,29 +527,32 @@ def search_hashes( # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. - if request is not None and any([hash_prefix, threat_types]): + has_flattened_params = any([hash_prefix, threat_types]) + if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) - request = webrisk.SearchHashesRequest(request) + # Minor optimization to avoid making a copy if the user passes + # in a webrisk.SearchHashesRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, webrisk.SearchHashesRequest): + request = webrisk.SearchHashesRequest(request) - # If we have keyword arguments corresponding to fields on the - # request, apply these. + # If we have keyword arguments corresponding to fields on the + # request, apply these. - if hash_prefix is not None: - request.hash_prefix = hash_prefix - if threat_types is not None: - request.threat_types = threat_types + if hash_prefix is not None: + request.hash_prefix = hash_prefix + + if threat_types: + request.threat_types.extend(threat_types) # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. - rpc = gapic_v1.method.wrap_method( - self._transport.search_hashes, - default_timeout=None, - client_info=_client_info, - ) + rpc = self._transport._wrapped_methods[self._transport.search_hashes] # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) @@ -454,11 +562,11 @@ def search_hashes( try: - _client_info = gapic_v1.client_info.ClientInfo( + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( gapic_version=pkg_resources.get_distribution("google-cloud-webrisk",).version, ) except pkg_resources.DistributionNotFound: - _client_info = gapic_v1.client_info.ClientInfo() + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() __all__ = ("WebRiskServiceV1Beta1Client",) diff --git a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/__init__.py b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/__init__.py index ff1b518..7dbf769 100644 --- a/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/__init__.py +++ b/google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/transports/__init__.py @@ -30,7 +30,6 @@ _transport_registry["grpc"] = WebRiskServiceV1Beta1GrpcTransport _transport_registry["grpc_asyncio"] = WebRiskServiceV1Beta1GrpcAsyncIOTransport - __all__ = ( "WebRiskServiceV1Beta1Transport", "WebRiskServiceV1Beta1GrpcTransport", 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 5bb1c49..d26cc02 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 @@ -17,14 +17,25 @@ import abc import typing +import pkg_resources -from google import auth +from google import auth # type: ignore from google.api_core import exceptions # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google.api_core import retry as retries # type: ignore from google.auth import credentials # type: ignore from google.cloud.webrisk_v1beta1.types import webrisk +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution("google-cloud-webrisk",).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + class WebRiskServiceV1Beta1Transport(abc.ABC): """Abstract transport class for WebRiskServiceV1Beta1.""" @@ -37,6 +48,8 @@ def __init__( credentials: credentials.Credentials = None, credentials_file: typing.Optional[str] = None, scopes: typing.Optional[typing.Sequence[str]] = AUTH_SCOPES, + quota_project_id: typing.Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, **kwargs, ) -> None: """Instantiate the transport. @@ -52,6 +65,13 @@ def __init__( be loaded with :func:`google.auth.load_credentials_from_file`. This argument is mutually exclusive with credentials. scope (Optional[Sequence[str]]): A list of scopes. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. """ # Save the hostname. Default to port 443 (HTTPS) if none is specified. if ":" not in host: @@ -67,14 +87,64 @@ def __init__( if credentials_file is not None: credentials, _ = auth.load_credentials_from_file( - credentials_file, scopes=scopes + credentials_file, scopes=scopes, quota_project_id=quota_project_id ) + elif credentials is None: - credentials, _ = auth.default(scopes=scopes) + credentials, _ = auth.default( + scopes=scopes, quota_project_id=quota_project_id + ) # Save the credentials. self._credentials = credentials + # Lifted into its own function so it can be stubbed out during tests. + self._prep_wrapped_messages(client_info) + + def _prep_wrapped_messages(self, client_info): + # Precompute the wrapped methods. + self._wrapped_methods = { + self.compute_threat_list_diff: gapic_v1.method.wrap_method( + self.compute_threat_list_diff, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=client_info, + ), + self.search_uris: gapic_v1.method.wrap_method( + self.search_uris, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=client_info, + ), + self.search_hashes: gapic_v1.method.wrap_method( + self.search_hashes, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=600.0, + client_info=client_info, + ), + } + @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 a585953..e225633 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 @@ -15,19 +15,20 @@ # limitations under the License. # +import warnings from typing import Callable, Dict, Optional, Sequence, Tuple from google.api_core import grpc_helpers # type: ignore +from google.api_core import gapic_v1 # type: ignore from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore - import grpc # type: ignore from google.cloud.webrisk_v1beta1.types import webrisk -from .base import WebRiskServiceV1Beta1Transport +from .base import WebRiskServiceV1Beta1Transport, DEFAULT_CLIENT_INFO class WebRiskServiceV1Beta1GrpcTransport(WebRiskServiceV1Beta1Transport): @@ -55,7 +56,10 @@ def __init__( scopes: Sequence[str] = None, channel: grpc.Channel = None, api_mtls_endpoint: str = None, - client_cert_source: Callable[[], Tuple[bytes, bytes]] = None + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the transport. @@ -74,14 +78,23 @@ def __init__( ignored if ``channel`` is provided. channel (Optional[grpc.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport @@ -89,6 +102,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -96,7 +111,13 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint @@ -104,7 +125,9 @@ def __init__( ) if credentials is None: - credentials, _ = auth.default(scopes=self.AUTH_SCOPES) + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) # Create SSL credentials with client_cert_source or application # default SSL credentials. @@ -123,7 +146,36 @@ def __init__( credentials_file=credentials_file, ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + + self._stubs = {} # type: Dict[str, Callable] # Run the base constructor. super().__init__( @@ -131,10 +183,10 @@ def __init__( credentials=credentials, credentials_file=credentials_file, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, ) - self._stubs = {} # type: Dict[str, Callable] - @classmethod def create_channel( cls, @@ -142,11 +194,12 @@ def create_channel( credentials: credentials.Credentials = None, credentials_file: str = None, scopes: Optional[Sequence[str]] = None, - **kwargs + quota_project_id: Optional[str] = None, + **kwargs, ) -> grpc.Channel: """Create and return a gRPC channel object. Args: - address (Optionsl[str]): The host for the channel to use. + address (Optional[str]): The host for the channel to use. credentials (Optional[~.Credentials]): The authorization credentials to attach to requests. These credentials identify this application to the service. If @@ -158,6 +211,8 @@ def create_channel( scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: @@ -173,24 +228,14 @@ def create_channel( credentials=credentials, credentials_file=credentials_file, scopes=scopes, - **kwargs + quota_project_id=quota_project_id, + **kwargs, ) @property def grpc_channel(self) -> grpc.Channel: - """Create the channel designed to connect to this service. - - This property caches on the instance; repeated calls return - the same channel. + """Return the channel designed to connect to this service. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - - # Return the channel from cache. return self._grpc_channel @property 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 b8c81d6..7d94c10 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 @@ -15,9 +15,12 @@ # limitations under the License. # +import warnings from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple +from google.api_core import gapic_v1 # type: ignore from google.api_core import grpc_helpers_async # type: ignore +from google import auth # type: ignore from google.auth import credentials # type: ignore from google.auth.transport.grpc import SslCredentials # type: ignore @@ -26,7 +29,7 @@ from google.cloud.webrisk_v1beta1.types import webrisk -from .base import WebRiskServiceV1Beta1Transport +from .base import WebRiskServiceV1Beta1Transport, DEFAULT_CLIENT_INFO from .grpc import WebRiskServiceV1Beta1GrpcTransport @@ -54,7 +57,8 @@ def create_channel( credentials: credentials.Credentials = None, credentials_file: Optional[str] = None, scopes: Optional[Sequence[str]] = None, - **kwargs + quota_project_id: Optional[str] = None, + **kwargs, ) -> aio.Channel: """Create and return a gRPC AsyncIO channel object. Args: @@ -70,6 +74,8 @@ def create_channel( scopes (Optional[Sequence[str]]): A optional list of scopes needed for this service. These are only used when credentials are not specified and are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. kwargs (Optional[dict]): Keyword arguments, which are passed to the channel creation. Returns: @@ -81,7 +87,8 @@ def create_channel( credentials=credentials, credentials_file=credentials_file, scopes=scopes, - **kwargs + quota_project_id=quota_project_id, + **kwargs, ) def __init__( @@ -93,7 +100,10 @@ def __init__( scopes: Optional[Sequence[str]] = None, channel: aio.Channel = None, api_mtls_endpoint: str = None, - client_cert_source: Callable[[], Tuple[bytes, bytes]] = None + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, + quota_project_id=None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, ) -> None: """Instantiate the transport. @@ -113,14 +123,23 @@ def __init__( are passed to :func:`google.auth.default`. channel (Optional[aio.Channel]): A ``Channel`` instance through which to make calls. - api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If - provided, it overrides the ``host`` argument and tries to create + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create a mutual TLS channel with client SSL credentials from ``client_cert_source`` or applicatin default SSL credentials. - client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A - callback to provide client SSL certificate bytes and private key - bytes, both in PEM format. It is ignored if ``api_mtls_endpoint`` - is None. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. Raises: google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport @@ -128,6 +147,8 @@ def __init__( google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` and ``credentials_file`` are passed. """ + self._ssl_channel_credentials = ssl_channel_credentials + if channel: # Sanity check: Ensure that channel and credentials are not both # provided. @@ -135,13 +156,24 @@ def __init__( # If a channel was explicitly provided, set it. self._grpc_channel = channel + self._ssl_channel_credentials = None elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + host = ( api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443" ) + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + # Create SSL credentials with client_cert_source or application # default SSL credentials. if client_cert_source: @@ -159,6 +191,33 @@ def __init__( credentials_file=credentials_file, ssl_credentials=ssl_credentials, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + self._ssl_channel_credentials = ssl_credentials + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], ) # Run the base constructor. @@ -167,6 +226,8 @@ def __init__( credentials=credentials, credentials_file=credentials_file, scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, ) self._stubs = {} @@ -178,13 +239,6 @@ def grpc_channel(self) -> aio.Channel: This property caches on the instance; repeated calls return the same channel. """ - # Sanity check: Only create a new channel if we do not already - # have one. - if not hasattr(self, "_grpc_channel"): - self._grpc_channel = self.create_channel( - self._host, credentials=self._credentials, - ) - # Return the channel from cache. return self._grpc_channel diff --git a/google/cloud/webrisk_v1beta1/types/__init__.py b/google/cloud/webrisk_v1beta1/types/__init__.py index 5a964ca..3453411 100644 --- a/google/cloud/webrisk_v1beta1/types/__init__.py +++ b/google/cloud/webrisk_v1beta1/types/__init__.py @@ -27,9 +27,10 @@ RawIndices, RawHashes, RiceDeltaEncoding, + ThreatType, + CompressionType, ) - __all__ = ( "ComputeThreatListDiffRequest", "ComputeThreatListDiffResponse", @@ -42,4 +43,6 @@ "RawIndices", "RawHashes", "RiceDeltaEncoding", + "ThreatType", + "CompressionType", ) diff --git a/google/cloud/webrisk_v1beta1/types/webrisk.py b/google/cloud/webrisk_v1beta1/types/webrisk.py index 1f660fd..711cd90 100644 --- a/google/cloud/webrisk_v1beta1/types/webrisk.py +++ b/google/cloud/webrisk_v1beta1/types/webrisk.py @@ -253,7 +253,7 @@ class ThreatHash(proto.Message): threat_types (Sequence[~.webrisk.ThreatType]): The ThreatList this threat belongs to. This must contain at least one entry. - hash (bytes): + hash_ (bytes): A 32 byte SHA256 hash. This field is in binary format. For JSON requests, hashes are base64-encoded. @@ -265,7 +265,7 @@ class ThreatHash(proto.Message): threat_types = proto.RepeatedField(proto.ENUM, number=1, enum="ThreatType",) - hash = proto.Field(proto.BYTES, number=2) + hash_ = proto.Field(proto.BYTES, number=2) expire_time = proto.Field(proto.MESSAGE, number=3, message=timestamp.Timestamp,) diff --git a/scripts/fixup_webrisk_v1_keywords.py b/scripts/fixup_webrisk_v1_keywords.py index 3b554e2..12cef51 100644 --- a/scripts/fixup_webrisk_v1_keywords.py +++ b/scripts/fixup_webrisk_v1_keywords.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2020 Google LLC @@ -44,6 +45,7 @@ class webriskCallTransformer(cst.CSTTransformer): 'create_submission': ('parent', 'submission', ), 'search_hashes': ('threat_types', 'hash_prefix', ), 'search_uris': ('uri', 'threat_types', ), + } def leave_Call(self, original: cst.Call, updated: cst.Call) -> cst.CSTNode: diff --git a/scripts/fixup_webrisk_v1beta1_keywords.py b/scripts/fixup_webrisk_v1beta1_keywords.py index 0b3a191..bca0bc4 100644 --- a/scripts/fixup_webrisk_v1beta1_keywords.py +++ b/scripts/fixup_webrisk_v1beta1_keywords.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2020 Google LLC @@ -43,6 +44,7 @@ class webriskCallTransformer(cst.CSTTransformer): 'compute_threat_list_diff': ('threat_type', 'constraints', 'version_token', ), 'search_hashes': ('threat_types', 'hash_prefix', ), 'search_uris': ('uri', 'threat_types', ), + } def leave_Call(self, original: cst.Call, updated: cst.Call) -> cst.CSTNode: diff --git a/setup.py b/setup.py index e336127..add5f5f 100644 --- a/setup.py +++ b/setup.py @@ -29,9 +29,8 @@ # 'Development Status :: 5 - Production/Stable' release_status = "Development Status :: 3 - Alpha" dependencies = [ - "google-api-core[grpc] >= 1.21.0, < 2.0.0dev", - "proto-plus >= 0.4.0", - "libcst >= 0.2.5", + "google-api-core[grpc] >= 1.22.2, < 2.0.0dev", + "proto-plus >= 1.10.0", ] extras = {} diff --git a/synth.metadata b/synth.metadata index b99acd4..2ca978c 100644 --- a/synth.metadata +++ b/synth.metadata @@ -11,8 +11,8 @@ "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "50ae1c72fd94a3ae4269394b09e4b7fbb9251146", - "internalRef": "320484049" + "sha": "57fc4a8a94a5bd015a83fb0f0a1707f62254b2cd", + "internalRef": "348813319" } }, { @@ -137,7 +137,9 @@ "scripts/readme-gen/templates/install_portaudio.tmpl.rst", "setup.cfg", "testing/.gitignore", + "tests/unit/gapic/webrisk_v1/__init__.py", "tests/unit/gapic/webrisk_v1/test_web_risk_service.py", - "tests/unit/gapic/webrisk_v1beta1/test_web_risk_service_v1_beta1.py" + "tests/unit/gapic/webrisk_v1beta1/__init__.py", + "tests/unit/gapic/webrisk_v1beta1/test_web_risk_service_v1_beta1.py", ] } \ No newline at end of file diff --git a/synth.py b/synth.py index b60cc84..308e97d 100644 --- a/synth.py +++ b/synth.py @@ -53,7 +53,4 @@ ) s.move(templated_files, excludes=[".coveragerc"]) # microgenerator has a good .coveragerc file -# TODO(busunkim): Use latest sphinx after microgenerator transition -s.replace("noxfile.py", """['"]sphinx['"]""", '"sphinx<3.0.0"') - s.shell.run(["nox", "-s", "blacken"], hide_output=False) diff --git a/tests/unit/gapic/webrisk_v1/__init__.py b/tests/unit/gapic/webrisk_v1/__init__.py index e69de29..8b13789 100644 --- a/tests/unit/gapic/webrisk_v1/__init__.py +++ b/tests/unit/gapic/webrisk_v1/__init__.py @@ -0,0 +1 @@ + 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 5bf8bb6..1120b73 100644 --- a/tests/unit/gapic/webrisk_v1/test_web_risk_service.py +++ b/tests/unit/gapic/webrisk_v1/test_web_risk_service.py @@ -22,10 +22,12 @@ from grpc.experimental import aio import math import pytest +from proto.marshal.rules.dates import DurationRule, TimestampRule from google import auth from google.api_core import client_options from google.api_core import exceptions +from google.api_core import gapic_v1 from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async from google.auth import credentials @@ -42,6 +44,17 @@ def client_cert_source_callback(): return b"cert bytes", b"key bytes" +# If default endpoint is localhost, then default mtls endpoint will be the same. +# This method modifies the default endpoint so the client can produce a different +# mtls endpoint for endpoint testing purposes. +def modify_default_endpoint(client): + return ( + "foo.googleapis.com" + if ("localhost" in client.DEFAULT_ENDPOINT) + else client.DEFAULT_ENDPOINT + ) + + def test__get_default_mtls_endpoint(): api_endpoint = "example.googleapis.com" api_mtls_endpoint = "example.mtls.googleapis.com" @@ -81,12 +94,12 @@ def test_web_risk_service_client_from_service_account_file(client_class): ) as factory: factory.return_value = creds client = client_class.from_service_account_file("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds client = client_class.from_service_account_json("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds - assert client._transport._host == "webrisk.googleapis.com:443" + assert client.transport._host == "webrisk.googleapis.com:443" def test_web_risk_service_client_get_transport_class(): @@ -108,6 +121,16 @@ def test_web_risk_service_client_get_transport_class(): ), ], ) +@mock.patch.object( + WebRiskServiceClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(WebRiskServiceClient), +) +@mock.patch.object( + WebRiskServiceAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(WebRiskServiceAsyncClient), +) def test_web_risk_service_client_client_options( client_class, transport_class, transport_name ): @@ -132,103 +155,207 @@ def test_web_risk_service_client_client_options( credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is # "never". - os.environ["GOOGLE_API_USE_MTLS"] = "never" - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - ) - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is - # "always". - os.environ["GOOGLE_API_USE_MTLS"] = "always" - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and client_cert_source is provided. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - options = client_options.ClientOptions( - client_cert_source=client_cert_source_callback - ) - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=client_cert_source_callback, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and default_client_cert_source is provided. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, + host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", but client_cert_source and default_client_cert_source are None. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=False, - ): + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is + # "always". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): + with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_ENDPOINT, + host=client.DEFAULT_MTLS_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has # unsupported value. - os.environ["GOOGLE_API_USE_MTLS"] = "Unsupported" - with pytest.raises(MutualTLSChannelError): - client = client_class() + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError): + client = client_class() + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError): + client = client_class() + + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,use_client_cert_env", + [ + (WebRiskServiceClient, transports.WebRiskServiceGrpcTransport, "grpc", "true"), + ( + WebRiskServiceAsyncClient, + transports.WebRiskServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (WebRiskServiceClient, transports.WebRiskServiceGrpcTransport, "grpc", "false"), + ( + WebRiskServiceAsyncClient, + transports.WebRiskServiceGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), + ], +) +@mock.patch.object( + WebRiskServiceClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(WebRiskServiceClient), +) +@mock.patch.object( + WebRiskServiceAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(WebRiskServiceAsyncClient), +) +@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) +def test_web_risk_service_client_mtls_env_auto( + client_class, transport_class, transport_name, use_client_cert_env +): + # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default + # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. - del os.environ["GOOGLE_API_USE_MTLS"] + # Check the case client_cert_source is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + options = client_options.ClientOptions( + client_cert_source=client_cert_source_callback + ) + with mock.patch.object(transport_class, "__init__") as patched: + ssl_channel_creds = mock.Mock() + with mock.patch( + "grpc.ssl_channel_credentials", return_value=ssl_channel_creds + ): + patched.return_value = None + client = client_class(client_options=options) + + if use_client_cert_env == "false": + expected_ssl_channel_creds = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_ssl_channel_creds = ssl_channel_creds + expected_host = client.DEFAULT_MTLS_ENDPOINT + + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case ADC client cert is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.ssl_credentials", + new_callable=mock.PropertyMock, + ) as ssl_credentials_mock: + if use_client_cert_env == "false": + is_mtls_mock.return_value = False + ssl_credentials_mock.return_value = None + expected_host = client.DEFAULT_ENDPOINT + expected_ssl_channel_creds = None + else: + is_mtls_mock.return_value = True + ssl_credentials_mock.return_value = mock.Mock() + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_ssl_channel_creds = ( + ssl_credentials_mock.return_value + ) + + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + is_mtls_mock.return_value = False + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) @pytest.mark.parametrize( @@ -255,8 +382,9 @@ def test_web_risk_service_client_client_options_scopes( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=["1", "2"], - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -284,8 +412,9 @@ def test_web_risk_service_client_client_options_credentials_file( credentials_file="credentials.json", host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -302,23 +431,26 @@ def test_web_risk_service_client_client_options_from_dict(): credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) -def test_compute_threat_list_diff(transport: str = "grpc"): +def test_compute_threat_list_diff( + transport: str = "grpc", request_type=webrisk.ComputeThreatListDiffRequest +): client = WebRiskServiceClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.ComputeThreatListDiffRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.compute_threat_list_diff), "__call__" + type(client.transport.compute_threat_list_diff), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.ComputeThreatListDiffResponse( @@ -332,9 +464,10 @@ def test_compute_threat_list_diff(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.ComputeThreatListDiffRequest() # Establish that the response is the type that we expect. + assert isinstance(response, webrisk.ComputeThreatListDiffResponse) assert ( @@ -345,19 +478,25 @@ def test_compute_threat_list_diff(transport: str = "grpc"): assert response.new_version_token == b"new_version_token_blob" +def test_compute_threat_list_diff_from_dict(): + test_compute_threat_list_diff(request_type=dict) + + @pytest.mark.asyncio -async def test_compute_threat_list_diff_async(transport: str = "grpc_asyncio"): +async def test_compute_threat_list_diff_async( + transport: str = "grpc_asyncio", request_type=webrisk.ComputeThreatListDiffRequest +): client = WebRiskServiceAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.ComputeThreatListDiffRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.compute_threat_list_diff), "__call__" + type(client.transport.compute_threat_list_diff), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( @@ -373,7 +512,7 @@ async def test_compute_threat_list_diff_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.ComputeThreatListDiffRequest() # Establish that the response is the type that we expect. assert isinstance(response, webrisk.ComputeThreatListDiffResponse) @@ -386,12 +525,17 @@ async def test_compute_threat_list_diff_async(transport: str = "grpc_asyncio"): assert response.new_version_token == b"new_version_token_blob" +@pytest.mark.asyncio +async def test_compute_threat_list_diff_async_from_dict(): + await test_compute_threat_list_diff_async(request_type=dict) + + def test_compute_threat_list_diff_flattened(): client = WebRiskServiceClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.compute_threat_list_diff), "__call__" + type(client.transport.compute_threat_list_diff), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.ComputeThreatListDiffResponse() @@ -410,8 +554,11 @@ def test_compute_threat_list_diff_flattened(): # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] + assert args[0].threat_type == webrisk.ThreatType.MALWARE + assert args[0].version_token == b"version_token_blob" + assert args[0].constraints == webrisk.ComputeThreatListDiffRequest.Constraints( max_diff_entries=1687 ) @@ -439,7 +586,7 @@ async def test_compute_threat_list_diff_flattened_async(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.compute_threat_list_diff), "__call__" + type(client.transport.compute_threat_list_diff), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.ComputeThreatListDiffResponse() @@ -461,8 +608,11 @@ async def test_compute_threat_list_diff_flattened_async(): # request object values. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] + assert args[0].threat_type == webrisk.ThreatType.MALWARE + assert args[0].version_token == b"version_token_blob" + assert args[0].constraints == webrisk.ComputeThreatListDiffRequest.Constraints( max_diff_entries=1687 ) @@ -485,17 +635,17 @@ async def test_compute_threat_list_diff_flattened_error_async(): ) -def test_search_uris(transport: str = "grpc"): +def test_search_uris(transport: str = "grpc", request_type=webrisk.SearchUrisRequest): client = WebRiskServiceClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.SearchUrisRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.search_uris), "__call__") as call: + with mock.patch.object(type(client.transport.search_uris), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchUrisResponse() @@ -505,26 +655,31 @@ def test_search_uris(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.SearchUrisRequest() # Establish that the response is the type that we expect. + assert isinstance(response, webrisk.SearchUrisResponse) +def test_search_uris_from_dict(): + test_search_uris(request_type=dict) + + @pytest.mark.asyncio -async def test_search_uris_async(transport: str = "grpc_asyncio"): +async def test_search_uris_async( + transport: str = "grpc_asyncio", request_type=webrisk.SearchUrisRequest +): client = WebRiskServiceAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.SearchUrisRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.search_uris), "__call__" - ) as call: + with mock.patch.object(type(client.transport.search_uris), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( webrisk.SearchUrisResponse() @@ -536,17 +691,22 @@ async def test_search_uris_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.SearchUrisRequest() # Establish that the response is the type that we expect. assert isinstance(response, webrisk.SearchUrisResponse) +@pytest.mark.asyncio +async def test_search_uris_async_from_dict(): + await test_search_uris_async(request_type=dict) + + def test_search_uris_flattened(): client = WebRiskServiceClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.search_uris), "__call__") as call: + with mock.patch.object(type(client.transport.search_uris), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchUrisResponse() @@ -560,7 +720,9 @@ def test_search_uris_flattened(): # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] + assert args[0].uri == "uri_value" + assert args[0].threat_types == [webrisk.ThreatType.MALWARE] @@ -582,9 +744,7 @@ async def test_search_uris_flattened_async(): client = WebRiskServiceAsyncClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.search_uris), "__call__" - ) as call: + with mock.patch.object(type(client.transport.search_uris), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchUrisResponse() @@ -601,7 +761,9 @@ async def test_search_uris_flattened_async(): # request object values. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] + assert args[0].uri == "uri_value" + assert args[0].threat_types == [webrisk.ThreatType.MALWARE] @@ -619,17 +781,19 @@ async def test_search_uris_flattened_error_async(): ) -def test_search_hashes(transport: str = "grpc"): +def test_search_hashes( + transport: str = "grpc", request_type=webrisk.SearchHashesRequest +): client = WebRiskServiceClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.SearchHashesRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.search_hashes), "__call__") as call: + with mock.patch.object(type(client.transport.search_hashes), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchHashesResponse() @@ -639,26 +803,31 @@ def test_search_hashes(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.SearchHashesRequest() # Establish that the response is the type that we expect. + assert isinstance(response, webrisk.SearchHashesResponse) +def test_search_hashes_from_dict(): + test_search_hashes(request_type=dict) + + @pytest.mark.asyncio -async def test_search_hashes_async(transport: str = "grpc_asyncio"): +async def test_search_hashes_async( + transport: str = "grpc_asyncio", request_type=webrisk.SearchHashesRequest +): client = WebRiskServiceAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.SearchHashesRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.search_hashes), "__call__" - ) as call: + with mock.patch.object(type(client.transport.search_hashes), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( webrisk.SearchHashesResponse() @@ -670,17 +839,22 @@ async def test_search_hashes_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.SearchHashesRequest() # Establish that the response is the type that we expect. assert isinstance(response, webrisk.SearchHashesResponse) +@pytest.mark.asyncio +async def test_search_hashes_async_from_dict(): + await test_search_hashes_async(request_type=dict) + + def test_search_hashes_flattened(): client = WebRiskServiceClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.search_hashes), "__call__") as call: + with mock.patch.object(type(client.transport.search_hashes), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchHashesResponse() @@ -694,7 +868,9 @@ def test_search_hashes_flattened(): # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] + assert args[0].hash_prefix == b"hash_prefix_blob" + assert args[0].threat_types == [webrisk.ThreatType.MALWARE] @@ -716,9 +892,7 @@ async def test_search_hashes_flattened_async(): client = WebRiskServiceAsyncClient(credentials=credentials.AnonymousCredentials(),) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.search_hashes), "__call__" - ) as call: + with mock.patch.object(type(client.transport.search_hashes), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchHashesResponse() @@ -735,7 +909,9 @@ async def test_search_hashes_flattened_async(): # request object values. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] + assert args[0].hash_prefix == b"hash_prefix_blob" + assert args[0].threat_types == [webrisk.ThreatType.MALWARE] @@ -753,18 +929,20 @@ async def test_search_hashes_flattened_error_async(): ) -def test_create_submission(transport: str = "grpc"): +def test_create_submission( + transport: str = "grpc", request_type=webrisk.CreateSubmissionRequest +): client = WebRiskServiceClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.CreateSubmissionRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.create_submission), "__call__" + type(client.transport.create_submission), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.Submission(uri="uri_value",) @@ -775,27 +953,34 @@ def test_create_submission(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.CreateSubmissionRequest() # Establish that the response is the type that we expect. + assert isinstance(response, webrisk.Submission) assert response.uri == "uri_value" +def test_create_submission_from_dict(): + test_create_submission(request_type=dict) + + @pytest.mark.asyncio -async def test_create_submission_async(transport: str = "grpc_asyncio"): +async def test_create_submission_async( + transport: str = "grpc_asyncio", request_type=webrisk.CreateSubmissionRequest +): client = WebRiskServiceAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.CreateSubmissionRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.create_submission), "__call__" + type(client.transport.create_submission), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( @@ -808,7 +993,7 @@ async def test_create_submission_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.CreateSubmissionRequest() # Establish that the response is the type that we expect. assert isinstance(response, webrisk.Submission) @@ -816,6 +1001,11 @@ async def test_create_submission_async(transport: str = "grpc_asyncio"): assert response.uri == "uri_value" +@pytest.mark.asyncio +async def test_create_submission_async_from_dict(): + await test_create_submission_async(request_type=dict) + + def test_create_submission_field_headers(): client = WebRiskServiceClient(credentials=credentials.AnonymousCredentials(),) @@ -826,7 +1016,7 @@ def test_create_submission_field_headers(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.create_submission), "__call__" + type(client.transport.create_submission), "__call__" ) as call: call.return_value = webrisk.Submission() @@ -853,7 +1043,7 @@ async def test_create_submission_field_headers_async(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.create_submission), "__call__" + type(client.transport.create_submission), "__call__" ) as call: call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(webrisk.Submission()) @@ -874,7 +1064,7 @@ def test_create_submission_flattened(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.create_submission), "__call__" + type(client.transport.create_submission), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.Submission() @@ -889,7 +1079,9 @@ def test_create_submission_flattened(): # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] + assert args[0].parent == "parent_value" + assert args[0].submission == webrisk.Submission(uri="uri_value") @@ -912,7 +1104,7 @@ async def test_create_submission_flattened_async(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.create_submission), "__call__" + type(client.transport.create_submission), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.Submission() @@ -928,7 +1120,9 @@ async def test_create_submission_flattened_async(): # request object values. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] + assert args[0].parent == "parent_value" + assert args[0].submission == webrisk.Submission(uri="uri_value") @@ -982,7 +1176,7 @@ def test_transport_instance(): credentials=credentials.AnonymousCredentials(), ) client = WebRiskServiceClient(transport=transport) - assert client._transport is transport + assert client.transport is transport def test_transport_get_channel(): @@ -1000,10 +1194,25 @@ def test_transport_get_channel(): assert channel +@pytest.mark.parametrize( + "transport_class", + [ + transports.WebRiskServiceGrpcTransport, + transports.WebRiskServiceGrpcAsyncIOTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + def test_transport_grpc_default(): # A client should use the gRPC transport by default. client = WebRiskServiceClient(credentials=credentials.AnonymousCredentials(),) - assert isinstance(client._transport, transports.WebRiskServiceGrpcTransport,) + assert isinstance(client.transport, transports.WebRiskServiceGrpcTransport,) def test_web_risk_service_base_transport_error(): @@ -1017,9 +1226,13 @@ def test_web_risk_service_base_transport_error(): def test_web_risk_service_base_transport(): # Instantiate the base transport. - transport = transports.WebRiskServiceTransport( - credentials=credentials.AnonymousCredentials(), - ) + with mock.patch( + "google.cloud.webrisk_v1.services.web_risk_service.transports.WebRiskServiceTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.WebRiskServiceTransport( + credentials=credentials.AnonymousCredentials(), + ) # Every method on the transport should just blindly # raise NotImplementedError. @@ -1036,24 +1249,42 @@ def test_web_risk_service_base_transport(): def test_web_risk_service_base_transport_with_credentials_file(): # Instantiate the base transport with a credentials file - with mock.patch.object(auth, "load_credentials_from_file") as load_creds: + with mock.patch.object( + auth, "load_credentials_from_file" + ) as load_creds, mock.patch( + "google.cloud.webrisk_v1.services.web_risk_service.transports.WebRiskServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None load_creds.return_value = (credentials.AnonymousCredentials(), None) transport = transports.WebRiskServiceTransport( - credentials_file="credentials.json", + credentials_file="credentials.json", quota_project_id="octopus", ) load_creds.assert_called_once_with( "credentials.json", scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", ) +def test_web_risk_service_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(auth, "default") as adc, mock.patch( + "google.cloud.webrisk_v1.services.web_risk_service.transports.WebRiskServiceTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.WebRiskServiceTransport() + adc.assert_called_once() + + def test_web_risk_service_auth_adc(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(auth, "default") as adc: adc.return_value = (credentials.AnonymousCredentials(), None) WebRiskServiceClient() adc.assert_called_once_with( - scopes=("https://www.googleapis.com/auth/cloud-platform",) + scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, ) @@ -1062,9 +1293,12 @@ def test_web_risk_service_transport_auth_adc(): # ADC credentials. with mock.patch.object(auth, "default") as adc: adc.return_value = (credentials.AnonymousCredentials(), None) - transports.WebRiskServiceGrpcTransport(host="squid.clam.whelk") + transports.WebRiskServiceGrpcTransport( + host="squid.clam.whelk", quota_project_id="octopus" + ) adc.assert_called_once_with( - scopes=("https://www.googleapis.com/auth/cloud-platform",) + scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", ) @@ -1075,7 +1309,7 @@ def test_web_risk_service_host_no_port(): api_endpoint="webrisk.googleapis.com" ), ) - assert client._transport._host == "webrisk.googleapis.com:443" + assert client.transport._host == "webrisk.googleapis.com:443" def test_web_risk_service_host_with_port(): @@ -1085,178 +1319,246 @@ def test_web_risk_service_host_with_port(): api_endpoint="webrisk.googleapis.com:8000" ), ) - assert client._transport._host == "webrisk.googleapis.com:8000" + assert client.transport._host == "webrisk.googleapis.com:8000" def test_web_risk_service_grpc_transport_channel(): channel = grpc.insecure_channel("http://localhost/") - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.WebRiskServiceGrpcTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None def test_web_risk_service_grpc_asyncio_transport_channel(): channel = aio.insecure_channel("http://localhost/") - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.WebRiskServiceGrpcAsyncIOTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_web_risk_service_grpc_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred +@pytest.mark.parametrize( + "transport_class", + [ + transports.WebRiskServiceGrpcTransport, + transports.WebRiskServiceGrpcAsyncIOTransport, + ], +) +def test_web_risk_service_transport_channel_mtls_with_client_cert_source( + transport_class, ): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_ssl_cred = mock.Mock() + grpc_ssl_channel_cred.return_value = mock_ssl_cred + + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + + cred = credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(auth, "default") as adc: + adc.return_value = (cred, None) + transport = transport_class( + host="squid.clam.whelk", + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=client_cert_source_callback, + ) + adc.assert_called_once() + + grpc_ssl_channel_cred.assert_called_once_with( + certificate_chain=b"cert bytes", private_key=b"key bytes" + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=("https://www.googleapis.com/auth/cloud-platform",), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + +@pytest.mark.parametrize( + "transport_class", + [ + transports.WebRiskServiceGrpcTransport, + transports.WebRiskServiceGrpcAsyncIOTransport, + ], +) +def test_web_risk_service_transport_channel_mtls_with_adc(transport_class): mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + with mock.patch.multiple( + "google.auth.transport.grpc.SslCredentials", + __init__=mock.Mock(return_value=None), + ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), + ): + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + mock_cred = mock.Mock() + + with pytest.warns(DeprecationWarning): + transport = transport_class( + host="squid.clam.whelk", + credentials=mock_cred, + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=None, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=("https://www.googleapis.com/auth/cloud-platform",), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - transport = transports.WebRiskServiceGrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, +def test_common_billing_account_path(): + billing_account = "squid" + + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) - assert transport.grpc_channel == mock_grpc_channel + actual = WebRiskServiceClient.common_billing_account_path(billing_account) + assert expected == actual -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_web_risk_service_grpc_asyncio_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred -): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "clam", + } + path = WebRiskServiceClient.common_billing_account_path(**expected) - mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + # Check that the path construction is reversible. + actual = WebRiskServiceClient.parse_common_billing_account_path(path) + assert expected == actual - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - transport = transports.WebRiskServiceGrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, +def test_common_folder_path(): + folder = "whelk" + + expected = "folders/{folder}".format(folder=folder,) + actual = WebRiskServiceClient.common_folder_path(folder) + assert expected == actual + + +def test_parse_common_folder_path(): + expected = { + "folder": "octopus", + } + path = WebRiskServiceClient.common_folder_path(**expected) + + # Check that the path construction is reversible. + actual = WebRiskServiceClient.parse_common_folder_path(path) + assert expected == actual + + +def test_common_organization_path(): + organization = "oyster" + + expected = "organizations/{organization}".format(organization=organization,) + actual = WebRiskServiceClient.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "nudibranch", + } + path = WebRiskServiceClient.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = WebRiskServiceClient.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "cuttlefish" + + expected = "projects/{project}".format(project=project,) + actual = WebRiskServiceClient.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "mussel", + } + path = WebRiskServiceClient.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = WebRiskServiceClient.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "winkle" + location = "nautilus" + + expected = "projects/{project}/locations/{location}".format( + project=project, location=location, ) - assert transport.grpc_channel == mock_grpc_channel + actual = WebRiskServiceClient.common_location_path(project, location) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_web_risk_service_grpc_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel +def test_parse_common_location_path(): + expected = { + "project": "scallop", + "location": "abalone", + } + path = WebRiskServiceClient.common_location_path(**expected) - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.WebRiskServiceGrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - ) - assert transport.grpc_channel == mock_grpc_channel + # Check that the path construction is reversible. + actual = WebRiskServiceClient.parse_common_location_path(path) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_web_risk_service_grpc_asyncio_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel +def test_client_withDEFAULT_CLIENT_INFO(): + client_info = gapic_v1.client_info.ClientInfo() - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.WebRiskServiceGrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, + with mock.patch.object( + transports.WebRiskServiceTransport, "_prep_wrapped_messages" + ) as prep: + client = WebRiskServiceClient( + credentials=credentials.AnonymousCredentials(), client_info=client_info, ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.WebRiskServiceTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = WebRiskServiceClient.get_transport_class() + transport = transport_class( + credentials=credentials.AnonymousCredentials(), client_info=client_info, ) - assert transport.grpc_channel == mock_grpc_channel + prep.assert_called_once_with(client_info) diff --git a/tests/unit/gapic/webrisk_v1beta1/__init__.py b/tests/unit/gapic/webrisk_v1beta1/__init__.py index e69de29..8b13789 100644 --- a/tests/unit/gapic/webrisk_v1beta1/__init__.py +++ b/tests/unit/gapic/webrisk_v1beta1/__init__.py @@ -0,0 +1 @@ + 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 4840ecd..8c90db9 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 @@ -22,10 +22,12 @@ from grpc.experimental import aio import math import pytest +from proto.marshal.rules.dates import DurationRule, TimestampRule from google import auth from google.api_core import client_options from google.api_core import exceptions +from google.api_core import gapic_v1 from google.api_core import grpc_helpers from google.api_core import grpc_helpers_async from google.auth import credentials @@ -46,6 +48,17 @@ def client_cert_source_callback(): return b"cert bytes", b"key bytes" +# If default endpoint is localhost, then default mtls endpoint will be the same. +# This method modifies the default endpoint so the client can produce a different +# mtls endpoint for endpoint testing purposes. +def modify_default_endpoint(client): + return ( + "foo.googleapis.com" + if ("localhost" in client.DEFAULT_ENDPOINT) + else client.DEFAULT_ENDPOINT + ) + + def test__get_default_mtls_endpoint(): api_endpoint = "example.googleapis.com" api_mtls_endpoint = "example.mtls.googleapis.com" @@ -86,12 +99,12 @@ def test_web_risk_service_v1_beta1_client_from_service_account_file(client_class ) as factory: factory.return_value = creds client = client_class.from_service_account_file("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds client = client_class.from_service_account_json("dummy/file/path.json") - assert client._transport._credentials == creds + assert client.transport._credentials == creds - assert client._transport._host == "webrisk.googleapis.com:443" + assert client.transport._host == "webrisk.googleapis.com:443" def test_web_risk_service_v1_beta1_client_get_transport_class(): @@ -117,6 +130,16 @@ def test_web_risk_service_v1_beta1_client_get_transport_class(): ), ], ) +@mock.patch.object( + WebRiskServiceV1Beta1Client, + "DEFAULT_ENDPOINT", + modify_default_endpoint(WebRiskServiceV1Beta1Client), +) +@mock.patch.object( + WebRiskServiceV1Beta1AsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(WebRiskServiceV1Beta1AsyncClient), +) def test_web_risk_service_v1_beta1_client_client_options( client_class, transport_class, transport_name ): @@ -141,103 +164,217 @@ def test_web_risk_service_v1_beta1_client_client_options( credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is # "never". - os.environ["GOOGLE_API_USE_MTLS"] = "never" - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, - ) - - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is - # "always". - os.environ["GOOGLE_API_USE_MTLS"] = "always" - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class() - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and client_cert_source is provided. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - options = client_options.ClientOptions( - client_cert_source=client_cert_source_callback - ) - with mock.patch.object(transport_class, "__init__") as patched: - patched.return_value = None - client = client_class(client_options=options) - patched.assert_called_once_with( - credentials=None, - credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, - scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=client_cert_source_callback, - ) - - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", and default_client_cert_source is provided. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, - ): + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_MTLS_ENDPOINT, + host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is - # "auto", but client_cert_source and default_client_cert_source are None. - os.environ["GOOGLE_API_USE_MTLS"] = "auto" - with mock.patch.object(transport_class, "__init__") as patched: - with mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=False, - ): + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is + # "always". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): + with mock.patch.object(transport_class, "__init__") as patched: patched.return_value = None client = client_class() patched.assert_called_once_with( credentials=None, credentials_file=None, - host=client.DEFAULT_ENDPOINT, + host=client.DEFAULT_MTLS_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) - # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has # unsupported value. - os.environ["GOOGLE_API_USE_MTLS"] = "Unsupported" - with pytest.raises(MutualTLSChannelError): - client = client_class() + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError): + client = client_class() + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError): + client = client_class() + + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,use_client_cert_env", + [ + ( + WebRiskServiceV1Beta1Client, + transports.WebRiskServiceV1Beta1GrpcTransport, + "grpc", + "true", + ), + ( + WebRiskServiceV1Beta1AsyncClient, + transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + ( + WebRiskServiceV1Beta1Client, + transports.WebRiskServiceV1Beta1GrpcTransport, + "grpc", + "false", + ), + ( + WebRiskServiceV1Beta1AsyncClient, + transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), + ], +) +@mock.patch.object( + WebRiskServiceV1Beta1Client, + "DEFAULT_ENDPOINT", + modify_default_endpoint(WebRiskServiceV1Beta1Client), +) +@mock.patch.object( + WebRiskServiceV1Beta1AsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(WebRiskServiceV1Beta1AsyncClient), +) +@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) +def test_web_risk_service_v1_beta1_client_mtls_env_auto( + client_class, transport_class, transport_name, use_client_cert_env +): + # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default + # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. - del os.environ["GOOGLE_API_USE_MTLS"] + # Check the case client_cert_source is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + options = client_options.ClientOptions( + client_cert_source=client_cert_source_callback + ) + with mock.patch.object(transport_class, "__init__") as patched: + ssl_channel_creds = mock.Mock() + with mock.patch( + "grpc.ssl_channel_credentials", return_value=ssl_channel_creds + ): + patched.return_value = None + client = client_class(client_options=options) + + if use_client_cert_env == "false": + expected_ssl_channel_creds = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_ssl_channel_creds = ssl_channel_creds + expected_host = client.DEFAULT_MTLS_ENDPOINT + + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case ADC client cert is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.ssl_credentials", + new_callable=mock.PropertyMock, + ) as ssl_credentials_mock: + if use_client_cert_env == "false": + is_mtls_mock.return_value = False + ssl_credentials_mock.return_value = None + expected_host = client.DEFAULT_ENDPOINT + expected_ssl_channel_creds = None + else: + is_mtls_mock.return_value = True + ssl_credentials_mock.return_value = mock.Mock() + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_ssl_channel_creds = ( + ssl_credentials_mock.return_value + ) + + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + is_mtls_mock.return_value = False + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) @pytest.mark.parametrize( @@ -268,8 +405,9 @@ def test_web_risk_service_v1_beta1_client_client_options_scopes( credentials_file=None, host=client.DEFAULT_ENDPOINT, scopes=["1", "2"], - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -301,8 +439,9 @@ def test_web_risk_service_v1_beta1_client_client_options_credentials_file( credentials_file="credentials.json", host=client.DEFAULT_ENDPOINT, scopes=None, - api_mtls_endpoint=client.DEFAULT_ENDPOINT, - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) @@ -319,23 +458,26 @@ def test_web_risk_service_v1_beta1_client_client_options_from_dict(): credentials_file=None, host="squid.clam.whelk", scopes=None, - api_mtls_endpoint="squid.clam.whelk", - client_cert_source=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, ) -def test_compute_threat_list_diff(transport: str = "grpc"): +def test_compute_threat_list_diff( + transport: str = "grpc", request_type=webrisk.ComputeThreatListDiffRequest +): client = WebRiskServiceV1Beta1Client( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.ComputeThreatListDiffRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.compute_threat_list_diff), "__call__" + type(client.transport.compute_threat_list_diff), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.ComputeThreatListDiffResponse( @@ -349,9 +491,10 @@ def test_compute_threat_list_diff(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.ComputeThreatListDiffRequest() # Establish that the response is the type that we expect. + assert isinstance(response, webrisk.ComputeThreatListDiffResponse) assert ( @@ -362,19 +505,25 @@ def test_compute_threat_list_diff(transport: str = "grpc"): assert response.new_version_token == b"new_version_token_blob" +def test_compute_threat_list_diff_from_dict(): + test_compute_threat_list_diff(request_type=dict) + + @pytest.mark.asyncio -async def test_compute_threat_list_diff_async(transport: str = "grpc_asyncio"): +async def test_compute_threat_list_diff_async( + transport: str = "grpc_asyncio", request_type=webrisk.ComputeThreatListDiffRequest +): client = WebRiskServiceV1Beta1AsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.ComputeThreatListDiffRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.compute_threat_list_diff), "__call__" + type(client.transport.compute_threat_list_diff), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( @@ -390,7 +539,7 @@ async def test_compute_threat_list_diff_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.ComputeThreatListDiffRequest() # Establish that the response is the type that we expect. assert isinstance(response, webrisk.ComputeThreatListDiffResponse) @@ -403,6 +552,11 @@ async def test_compute_threat_list_diff_async(transport: str = "grpc_asyncio"): assert response.new_version_token == b"new_version_token_blob" +@pytest.mark.asyncio +async def test_compute_threat_list_diff_async_from_dict(): + await test_compute_threat_list_diff_async(request_type=dict) + + def test_compute_threat_list_diff_flattened(): client = WebRiskServiceV1Beta1Client( credentials=credentials.AnonymousCredentials(), @@ -410,7 +564,7 @@ def test_compute_threat_list_diff_flattened(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._transport.compute_threat_list_diff), "__call__" + type(client.transport.compute_threat_list_diff), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.ComputeThreatListDiffResponse() @@ -429,8 +583,11 @@ def test_compute_threat_list_diff_flattened(): # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] + assert args[0].threat_type == webrisk.ThreatType.MALWARE + assert args[0].version_token == b"version_token_blob" + assert args[0].constraints == webrisk.ComputeThreatListDiffRequest.Constraints( max_diff_entries=1687 ) @@ -462,7 +619,7 @@ async def test_compute_threat_list_diff_flattened_async(): # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( - type(client._client._transport.compute_threat_list_diff), "__call__" + type(client.transport.compute_threat_list_diff), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = webrisk.ComputeThreatListDiffResponse() @@ -484,8 +641,11 @@ async def test_compute_threat_list_diff_flattened_async(): # request object values. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] + assert args[0].threat_type == webrisk.ThreatType.MALWARE + assert args[0].version_token == b"version_token_blob" + assert args[0].constraints == webrisk.ComputeThreatListDiffRequest.Constraints( max_diff_entries=1687 ) @@ -510,17 +670,17 @@ async def test_compute_threat_list_diff_flattened_error_async(): ) -def test_search_uris(transport: str = "grpc"): +def test_search_uris(transport: str = "grpc", request_type=webrisk.SearchUrisRequest): client = WebRiskServiceV1Beta1Client( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.SearchUrisRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.search_uris), "__call__") as call: + with mock.patch.object(type(client.transport.search_uris), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchUrisResponse() @@ -530,26 +690,31 @@ def test_search_uris(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.SearchUrisRequest() # Establish that the response is the type that we expect. + assert isinstance(response, webrisk.SearchUrisResponse) +def test_search_uris_from_dict(): + test_search_uris(request_type=dict) + + @pytest.mark.asyncio -async def test_search_uris_async(transport: str = "grpc_asyncio"): +async def test_search_uris_async( + transport: str = "grpc_asyncio", request_type=webrisk.SearchUrisRequest +): client = WebRiskServiceV1Beta1AsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.SearchUrisRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.search_uris), "__call__" - ) as call: + with mock.patch.object(type(client.transport.search_uris), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( webrisk.SearchUrisResponse() @@ -561,19 +726,24 @@ async def test_search_uris_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.SearchUrisRequest() # Establish that the response is the type that we expect. assert isinstance(response, webrisk.SearchUrisResponse) +@pytest.mark.asyncio +async def test_search_uris_async_from_dict(): + await test_search_uris_async(request_type=dict) + + def test_search_uris_flattened(): client = WebRiskServiceV1Beta1Client( credentials=credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.search_uris), "__call__") as call: + with mock.patch.object(type(client.transport.search_uris), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchUrisResponse() @@ -587,7 +757,9 @@ def test_search_uris_flattened(): # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] + assert args[0].uri == "uri_value" + assert args[0].threat_types == [webrisk.ThreatType.MALWARE] @@ -613,9 +785,7 @@ async def test_search_uris_flattened_async(): ) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.search_uris), "__call__" - ) as call: + with mock.patch.object(type(client.transport.search_uris), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchUrisResponse() @@ -632,7 +802,9 @@ async def test_search_uris_flattened_async(): # request object values. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] + assert args[0].uri == "uri_value" + assert args[0].threat_types == [webrisk.ThreatType.MALWARE] @@ -652,17 +824,19 @@ async def test_search_uris_flattened_error_async(): ) -def test_search_hashes(transport: str = "grpc"): +def test_search_hashes( + transport: str = "grpc", request_type=webrisk.SearchHashesRequest +): client = WebRiskServiceV1Beta1Client( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.SearchHashesRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.search_hashes), "__call__") as call: + with mock.patch.object(type(client.transport.search_hashes), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchHashesResponse() @@ -672,26 +846,31 @@ def test_search_hashes(transport: str = "grpc"): assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.SearchHashesRequest() # Establish that the response is the type that we expect. + assert isinstance(response, webrisk.SearchHashesResponse) +def test_search_hashes_from_dict(): + test_search_hashes(request_type=dict) + + @pytest.mark.asyncio -async def test_search_hashes_async(transport: str = "grpc_asyncio"): +async def test_search_hashes_async( + transport: str = "grpc_asyncio", request_type=webrisk.SearchHashesRequest +): client = WebRiskServiceV1Beta1AsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. - request = webrisk.SearchHashesRequest() + request = request_type() # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.search_hashes), "__call__" - ) as call: + with mock.patch.object(type(client.transport.search_hashes), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( webrisk.SearchHashesResponse() @@ -703,19 +882,24 @@ async def test_search_hashes_async(transport: str = "grpc_asyncio"): assert len(call.mock_calls) _, args, _ = call.mock_calls[0] - assert args[0] == request + assert args[0] == webrisk.SearchHashesRequest() # Establish that the response is the type that we expect. assert isinstance(response, webrisk.SearchHashesResponse) +@pytest.mark.asyncio +async def test_search_hashes_async_from_dict(): + await test_search_hashes_async(request_type=dict) + + def test_search_hashes_flattened(): client = WebRiskServiceV1Beta1Client( credentials=credentials.AnonymousCredentials(), ) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object(type(client._transport.search_hashes), "__call__") as call: + with mock.patch.object(type(client.transport.search_hashes), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchHashesResponse() @@ -729,7 +913,9 @@ def test_search_hashes_flattened(): # request object values. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] + assert args[0].hash_prefix == b"hash_prefix_blob" + assert args[0].threat_types == [webrisk.ThreatType.MALWARE] @@ -755,9 +941,7 @@ async def test_search_hashes_flattened_async(): ) # Mock the actual call within the gRPC stub, and fake the request. - with mock.patch.object( - type(client._client._transport.search_hashes), "__call__" - ) as call: + with mock.patch.object(type(client.transport.search_hashes), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = webrisk.SearchHashesResponse() @@ -774,7 +958,9 @@ async def test_search_hashes_flattened_async(): # request object values. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] + assert args[0].hash_prefix == b"hash_prefix_blob" + assert args[0].threat_types == [webrisk.ThreatType.MALWARE] @@ -830,7 +1016,7 @@ def test_transport_instance(): credentials=credentials.AnonymousCredentials(), ) client = WebRiskServiceV1Beta1Client(transport=transport) - assert client._transport is transport + assert client.transport is transport def test_transport_get_channel(): @@ -848,12 +1034,27 @@ def test_transport_get_channel(): assert channel +@pytest.mark.parametrize( + "transport_class", + [ + transports.WebRiskServiceV1Beta1GrpcTransport, + transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + def test_transport_grpc_default(): # A client should use the gRPC transport by default. client = WebRiskServiceV1Beta1Client( credentials=credentials.AnonymousCredentials(), ) - assert isinstance(client._transport, transports.WebRiskServiceV1Beta1GrpcTransport,) + assert isinstance(client.transport, transports.WebRiskServiceV1Beta1GrpcTransport,) def test_web_risk_service_v1_beta1_base_transport_error(): @@ -867,9 +1068,13 @@ def test_web_risk_service_v1_beta1_base_transport_error(): def test_web_risk_service_v1_beta1_base_transport(): # Instantiate the base transport. - transport = transports.WebRiskServiceV1Beta1Transport( - credentials=credentials.AnonymousCredentials(), - ) + with mock.patch( + "google.cloud.webrisk_v1beta1.services.web_risk_service_v1_beta1.transports.WebRiskServiceV1Beta1Transport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.WebRiskServiceV1Beta1Transport( + credentials=credentials.AnonymousCredentials(), + ) # Every method on the transport should just blindly # raise NotImplementedError. @@ -885,24 +1090,42 @@ def test_web_risk_service_v1_beta1_base_transport(): def test_web_risk_service_v1_beta1_base_transport_with_credentials_file(): # Instantiate the base transport with a credentials file - with mock.patch.object(auth, "load_credentials_from_file") as load_creds: + with mock.patch.object( + auth, "load_credentials_from_file" + ) as load_creds, mock.patch( + "google.cloud.webrisk_v1beta1.services.web_risk_service_v1_beta1.transports.WebRiskServiceV1Beta1Transport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None load_creds.return_value = (credentials.AnonymousCredentials(), None) transport = transports.WebRiskServiceV1Beta1Transport( - credentials_file="credentials.json", + credentials_file="credentials.json", quota_project_id="octopus", ) load_creds.assert_called_once_with( "credentials.json", scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", ) +def test_web_risk_service_v1_beta1_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(auth, "default") as adc, mock.patch( + "google.cloud.webrisk_v1beta1.services.web_risk_service_v1_beta1.transports.WebRiskServiceV1Beta1Transport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.WebRiskServiceV1Beta1Transport() + adc.assert_called_once() + + def test_web_risk_service_v1_beta1_auth_adc(): # If no credentials are provided, we should use ADC credentials. with mock.patch.object(auth, "default") as adc: adc.return_value = (credentials.AnonymousCredentials(), None) WebRiskServiceV1Beta1Client() adc.assert_called_once_with( - scopes=("https://www.googleapis.com/auth/cloud-platform",) + scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id=None, ) @@ -911,9 +1134,12 @@ def test_web_risk_service_v1_beta1_transport_auth_adc(): # ADC credentials. with mock.patch.object(auth, "default") as adc: adc.return_value = (credentials.AnonymousCredentials(), None) - transports.WebRiskServiceV1Beta1GrpcTransport(host="squid.clam.whelk") + transports.WebRiskServiceV1Beta1GrpcTransport( + host="squid.clam.whelk", quota_project_id="octopus" + ) adc.assert_called_once_with( - scopes=("https://www.googleapis.com/auth/cloud-platform",) + scopes=("https://www.googleapis.com/auth/cloud-platform",), + quota_project_id="octopus", ) @@ -924,7 +1150,7 @@ def test_web_risk_service_v1_beta1_host_no_port(): api_endpoint="webrisk.googleapis.com" ), ) - assert client._transport._host == "webrisk.googleapis.com:443" + assert client.transport._host == "webrisk.googleapis.com:443" def test_web_risk_service_v1_beta1_host_with_port(): @@ -934,178 +1160,246 @@ def test_web_risk_service_v1_beta1_host_with_port(): api_endpoint="webrisk.googleapis.com:8000" ), ) - assert client._transport._host == "webrisk.googleapis.com:8000" + assert client.transport._host == "webrisk.googleapis.com:8000" def test_web_risk_service_v1_beta1_grpc_transport_channel(): channel = grpc.insecure_channel("http://localhost/") - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.WebRiskServiceV1Beta1GrpcTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None def test_web_risk_service_v1_beta1_grpc_asyncio_transport_channel(): channel = aio.insecure_channel("http://localhost/") - # Check that if channel is provided, mtls endpoint and client_cert_source - # won't be used. - callback = mock.MagicMock() + # Check that channel is used if provided. transport = transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport( - host="squid.clam.whelk", - channel=channel, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=callback, + host="squid.clam.whelk", channel=channel, ) assert transport.grpc_channel == channel assert transport._host == "squid.clam.whelk:443" - assert not callback.called + assert transport._ssl_channel_credentials == None -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_web_risk_service_v1_beta1_grpc_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred +@pytest.mark.parametrize( + "transport_class", + [ + transports.WebRiskServiceV1Beta1GrpcTransport, + transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport, + ], +) +def test_web_risk_service_v1_beta1_transport_channel_mtls_with_client_cert_source( + transport_class, ): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_ssl_cred = mock.Mock() + grpc_ssl_channel_cred.return_value = mock_ssl_cred + + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + + cred = credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(auth, "default") as adc: + adc.return_value = (cred, None) + transport = transport_class( + host="squid.clam.whelk", + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=client_cert_source_callback, + ) + adc.assert_called_once() + + grpc_ssl_channel_cred.assert_called_once_with( + certificate_chain=b"cert bytes", private_key=b"key bytes" + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=("https://www.googleapis.com/auth/cloud-platform",), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel + assert transport._ssl_channel_credentials == mock_ssl_cred + +@pytest.mark.parametrize( + "transport_class", + [ + transports.WebRiskServiceV1Beta1GrpcTransport, + transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport, + ], +) +def test_web_risk_service_v1_beta1_transport_channel_mtls_with_adc(transport_class): mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + with mock.patch.multiple( + "google.auth.transport.grpc.SslCredentials", + __init__=mock.Mock(return_value=None), + ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), + ): + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + mock_cred = mock.Mock() + + with pytest.warns(DeprecationWarning): + transport = transport_class( + host="squid.clam.whelk", + credentials=mock_cred, + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=None, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=("https://www.googleapis.com/auth/cloud-platform",), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + options=[ + ("grpc.max_send_message_length", -1), + ("grpc.max_receive_message_length", -1), + ], + ) + assert transport.grpc_channel == mock_grpc_channel - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - transport = transports.WebRiskServiceV1Beta1GrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, +def test_common_billing_account_path(): + billing_account = "squid" + + expected = "billingAccounts/{billing_account}".format( + billing_account=billing_account, ) - assert transport.grpc_channel == mock_grpc_channel + actual = WebRiskServiceV1Beta1Client.common_billing_account_path(billing_account) + assert expected == actual -@mock.patch("grpc.ssl_channel_credentials", autospec=True) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_web_risk_service_v1_beta1_grpc_asyncio_transport_channel_mtls_with_client_cert_source( - grpc_create_channel, grpc_ssl_channel_cred -): - # Check that if channel is None, but api_mtls_endpoint and client_cert_source - # are provided, then a mTLS channel will be created. - mock_cred = mock.Mock() +def test_parse_common_billing_account_path(): + expected = { + "billing_account": "clam", + } + path = WebRiskServiceV1Beta1Client.common_billing_account_path(**expected) - mock_ssl_cred = mock.Mock() - grpc_ssl_channel_cred.return_value = mock_ssl_cred + # Check that the path construction is reversible. + actual = WebRiskServiceV1Beta1Client.parse_common_billing_account_path(path) + assert expected == actual - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel - transport = transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint="mtls.squid.clam.whelk", - client_cert_source=client_cert_source_callback, - ) - grpc_ssl_channel_cred.assert_called_once_with( - certificate_chain=b"cert bytes", private_key=b"key bytes" - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, +def test_common_folder_path(): + folder = "whelk" + + expected = "folders/{folder}".format(folder=folder,) + actual = WebRiskServiceV1Beta1Client.common_folder_path(folder) + assert expected == actual + + +def test_parse_common_folder_path(): + expected = { + "folder": "octopus", + } + path = WebRiskServiceV1Beta1Client.common_folder_path(**expected) + + # Check that the path construction is reversible. + actual = WebRiskServiceV1Beta1Client.parse_common_folder_path(path) + assert expected == actual + + +def test_common_organization_path(): + organization = "oyster" + + expected = "organizations/{organization}".format(organization=organization,) + actual = WebRiskServiceV1Beta1Client.common_organization_path(organization) + assert expected == actual + + +def test_parse_common_organization_path(): + expected = { + "organization": "nudibranch", + } + path = WebRiskServiceV1Beta1Client.common_organization_path(**expected) + + # Check that the path construction is reversible. + actual = WebRiskServiceV1Beta1Client.parse_common_organization_path(path) + assert expected == actual + + +def test_common_project_path(): + project = "cuttlefish" + + expected = "projects/{project}".format(project=project,) + actual = WebRiskServiceV1Beta1Client.common_project_path(project) + assert expected == actual + + +def test_parse_common_project_path(): + expected = { + "project": "mussel", + } + path = WebRiskServiceV1Beta1Client.common_project_path(**expected) + + # Check that the path construction is reversible. + actual = WebRiskServiceV1Beta1Client.parse_common_project_path(path) + assert expected == actual + + +def test_common_location_path(): + project = "winkle" + location = "nautilus" + + expected = "projects/{project}/locations/{location}".format( + project=project, location=location, ) - assert transport.grpc_channel == mock_grpc_channel + actual = WebRiskServiceV1Beta1Client.common_location_path(project, location) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers.create_channel", autospec=True) -def test_web_risk_service_v1_beta1_grpc_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel +def test_parse_common_location_path(): + expected = { + "project": "scallop", + "location": "abalone", + } + path = WebRiskServiceV1Beta1Client.common_location_path(**expected) - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.WebRiskServiceV1Beta1GrpcTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, - ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, - ) - assert transport.grpc_channel == mock_grpc_channel + # Check that the path construction is reversible. + actual = WebRiskServiceV1Beta1Client.parse_common_location_path(path) + assert expected == actual -@pytest.mark.parametrize( - "api_mtls_endpoint", ["mtls.squid.clam.whelk", "mtls.squid.clam.whelk:443"] -) -@mock.patch("google.api_core.grpc_helpers_async.create_channel", autospec=True) -def test_web_risk_service_v1_beta1_grpc_asyncio_transport_channel_mtls_with_adc( - grpc_create_channel, api_mtls_endpoint -): - # Check that if channel and client_cert_source are None, but api_mtls_endpoint - # is provided, then a mTLS channel will be created with SSL ADC. - mock_grpc_channel = mock.Mock() - grpc_create_channel.return_value = mock_grpc_channel +def test_client_withDEFAULT_CLIENT_INFO(): + client_info = gapic_v1.client_info.ClientInfo() - # Mock google.auth.transport.grpc.SslCredentials class. - mock_ssl_cred = mock.Mock() - with mock.patch.multiple( - "google.auth.transport.grpc.SslCredentials", - __init__=mock.Mock(return_value=None), - ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), - ): - mock_cred = mock.Mock() - transport = transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport( - host="squid.clam.whelk", - credentials=mock_cred, - api_mtls_endpoint=api_mtls_endpoint, - client_cert_source=None, + with mock.patch.object( + transports.WebRiskServiceV1Beta1Transport, "_prep_wrapped_messages" + ) as prep: + client = WebRiskServiceV1Beta1Client( + credentials=credentials.AnonymousCredentials(), client_info=client_info, ) - grpc_create_channel.assert_called_once_with( - "mtls.squid.clam.whelk:443", - credentials=mock_cred, - credentials_file=None, - scopes=("https://www.googleapis.com/auth/cloud-platform",), - ssl_credentials=mock_ssl_cred, + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.WebRiskServiceV1Beta1Transport, "_prep_wrapped_messages" + ) as prep: + transport_class = WebRiskServiceV1Beta1Client.get_transport_class() + transport = transport_class( + credentials=credentials.AnonymousCredentials(), client_info=client_info, ) - assert transport.grpc_channel == mock_grpc_channel + prep.assert_called_once_with(client_info) diff --git a/webrisk-v1-py.tar.gz b/webrisk-v1-py.tar.gz new file mode 100644 index 0000000..e69de29