Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
feat!: move to webrisk API to python microgenerator (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhenliu-gg5 committed Jul 14, 2020
1 parent c06d191 commit bbd2adf
Show file tree
Hide file tree
Showing 70 changed files with 7,347 additions and 5,997 deletions.
3 changes: 2 additions & 1 deletion .coveragerc
Expand Up @@ -32,4 +32,5 @@ omit =
*/gapic/*.py
*/proto/*.py
*/core/*.py
*/site-packages/*.py
*/site-packages/*.py
google/cloud/webrisk/__init__.py
122 changes: 122 additions & 0 deletions UPGRADING.MD
@@ -0,0 +1,122 @@
# 2.0.0 Migration Guide

The 2.0 release of the `google-cloud-webrisk` client is a significant upgrade based on a [next-gen code generator](https://github.com/googleapis/gapic-generator-python), and includes substantial interface changes. Existing code written for earlier versions of this library will likely require updates to use this version. This document describes the changes that have been made, and what you need to do to update your usage.

If you experience issues or have questions, please file an [issue](https://github.com/googleapis/python-webrisk/issues).

## Supported Python Versions

> **WARNING**: Breaking change
The 2.0.0 release requires Python 3.6+.


## Method Calls

> **WARNING**: Breaking change
Methods expect request objects. We provide a script that will convert most common use cases.

* Install the library

```py
python3 -m pip install google-cloud-webrisk
```

* The scripts `fixup_webrisk_v1_keywords.py` and `fixup_webrisk_v1beta1_keywords.py` shipped with the library. It expects
an input directory (with the code to convert) and an empty destination directory.

```sh
$ fixup_webrisk_v1_keywords.py --input-directory .samples/ --output-directory samples/
```

**Before:**
```py
from google.cloud import webrisk_v1

client = webrisk_v1.WebRiskServiceClient()
# TODO: Initialize `threat_types`:
threat_types = []

response = client.search_hashes(threat_types)
```


**After:**
```py
from google.cloud import webrisk_v1

client = webrisk_v1.WebRiskServiceClient()

response = client.search_hashes(request={"threat_types": "[]"})

```

### More Details

In `google-cloud-webrisk<2.0.0`, parameters required by the API were positional parameters and optional parameters were keyword parameters.

**Before:**
```py
def search_hashes(
self,
threat_types,
hash_prefix=None,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
):
```

In the 2.0.0 release, all methods have a single positional parameter `request`. Method docstrings indicate whether a parameter is required or optional.

Some methods have additional keyword only parameters. The available parameters depend on the [`google.api.method_signature` annotation](https://github.com/googleapis/googleapis/blob/master/google/cloud/webrisk/v1/webrisk.proto#L74) specified by the API producer.


**After:**
```py
def search_hashes(
self,
request: webrisk.SearchHashesRequest = None,
*,
hash_prefix: bytes = None,
threat_types: Sequence[webrisk.ThreatType] = None,
retry: retries.Retry = gapic_v1.method.DEFAULT,
timeout: float = None,
metadata: Sequence[Tuple[str, str]] = (),
) -> webrisk.SearchHashesResponse:
```

> **NOTE:** The `request` parameter and flattened keyword parameters for the API are mutually exclusive.
> Passing both will result in an error.

Both of these calls are valid:

```py
response = client.search_hashes(
request={
"hash_prefix": hash_prefix,
"threat_types": threat_types,
}
)
```

```py
response = client.search_hashes(
hash_prefix=hash_prefix,
threat_types=threat_types,
)
```

This call is invalid because it mixes `request` with a keyword argument `threat_types`. Executing this code
will result in an error.

```py
response = client.synthesize_speech(
request={
"hash_prefix": hash_prefix,
},
threat_types=threat_types
)
```
6 changes: 0 additions & 6 deletions docs/gapic/v1/api.rst

This file was deleted.

5 changes: 0 additions & 5 deletions docs/gapic/v1/types.rst

This file was deleted.

6 changes: 0 additions & 6 deletions docs/gapic/v1beta1/api.rst

This file was deleted.

5 changes: 0 additions & 5 deletions docs/gapic/v1beta1/types.rst

This file was deleted.

8 changes: 4 additions & 4 deletions docs/index.rst
Expand Up @@ -7,17 +7,17 @@ v1
.. toctree::
:maxdepth: 2

gapic/v1/api
gapic/v1/types
webrisk_v1/services
webrisk_v1/types


v1beta1
-------------
.. toctree::
:maxdepth: 2

gapic/v1beta1/api
gapic/v1beta1/types
webrisk_v1beta1/services
webrisk_v1beta1/types
changelog

Changelog
Expand Down
6 changes: 6 additions & 0 deletions docs/webrisk_v1/services.rst
@@ -0,0 +1,6 @@
Services for Google Cloud Webrisk v1 API
========================================

.. automodule:: google.cloud.webrisk_v1.services.web_risk_service
:members:
:inherited-members:
5 changes: 5 additions & 0 deletions docs/webrisk_v1/types.rst
@@ -0,0 +1,5 @@
Types for Google Cloud Webrisk v1 API
=====================================

.. automodule:: google.cloud.webrisk_v1.types
:members:
6 changes: 6 additions & 0 deletions docs/webrisk_v1beta1/services.rst
@@ -0,0 +1,6 @@
Services for Google Cloud Webrisk v1beta1 API
=============================================

.. automodule:: google.cloud.webrisk_v1beta1.services.web_risk_service_v1_beta1
:members:
:inherited-members:
5 changes: 5 additions & 0 deletions docs/webrisk_v1beta1/types.rst
@@ -0,0 +1,5 @@
Types for Google Cloud Webrisk v1beta1 API
==========================================

.. automodule:: google.cloud.webrisk_v1beta1.types
:members:
58 changes: 58 additions & 0 deletions google/cloud/webrisk/__init__.py
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from google.cloud.webrisk_v1.services.web_risk_service.async_client import (
WebRiskServiceAsyncClient,
)
from google.cloud.webrisk_v1.services.web_risk_service.client import (
WebRiskServiceClient,
)
from google.cloud.webrisk_v1.types.webrisk import CompressionType
from google.cloud.webrisk_v1.types.webrisk import ComputeThreatListDiffRequest
from google.cloud.webrisk_v1.types.webrisk import ComputeThreatListDiffResponse
from google.cloud.webrisk_v1.types.webrisk import CreateSubmissionRequest
from google.cloud.webrisk_v1.types.webrisk import RawHashes
from google.cloud.webrisk_v1.types.webrisk import RawIndices
from google.cloud.webrisk_v1.types.webrisk import RiceDeltaEncoding
from google.cloud.webrisk_v1.types.webrisk import SearchHashesRequest
from google.cloud.webrisk_v1.types.webrisk import SearchHashesResponse
from google.cloud.webrisk_v1.types.webrisk import SearchUrisRequest
from google.cloud.webrisk_v1.types.webrisk import SearchUrisResponse
from google.cloud.webrisk_v1.types.webrisk import Submission
from google.cloud.webrisk_v1.types.webrisk import ThreatEntryAdditions
from google.cloud.webrisk_v1.types.webrisk import ThreatEntryRemovals
from google.cloud.webrisk_v1.types.webrisk import ThreatType

__all__ = (
"CompressionType",
"ComputeThreatListDiffRequest",
"ComputeThreatListDiffResponse",
"CreateSubmissionRequest",
"RawHashes",
"RawIndices",
"RiceDeltaEncoding",
"SearchHashesRequest",
"SearchHashesResponse",
"SearchUrisRequest",
"SearchUrisResponse",
"Submission",
"ThreatEntryAdditions",
"ThreatEntryRemovals",
"ThreatType",
"WebRiskServiceAsyncClient",
"WebRiskServiceClient",
)
2 changes: 2 additions & 0 deletions google/cloud/webrisk/py.typed
@@ -0,0 +1,2 @@
# Marker file for PEP 561.
# The google-cloud-webrisk package uses inline types.
60 changes: 34 additions & 26 deletions google/cloud/webrisk_v1/__init__.py
@@ -1,45 +1,53 @@
# -*- coding: utf-8 -*-
#

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


from __future__ import absolute_import
import sys
import warnings

from google.cloud.webrisk_v1 import types
from google.cloud.webrisk_v1.gapic import enums
from google.cloud.webrisk_v1.gapic import web_risk_service_client


if sys.version_info[:2] == (2, 7):
message = (
"A future version of this library will drop support for Python 2.7. "
"More details about Python 2 support for Google Cloud Client Libraries "
"can be found at https://cloud.google.com/python/docs/python2-sunset/"
)
warnings.warn(message, DeprecationWarning)


class WebRiskServiceClient(web_risk_service_client.WebRiskServiceClient):
__doc__ = web_risk_service_client.WebRiskServiceClient.__doc__
enums = enums
from .services.web_risk_service import WebRiskServiceClient
from .types.webrisk import CompressionType
from .types.webrisk import ComputeThreatListDiffRequest
from .types.webrisk import ComputeThreatListDiffResponse
from .types.webrisk import CreateSubmissionRequest
from .types.webrisk import RawHashes
from .types.webrisk import RawIndices
from .types.webrisk import RiceDeltaEncoding
from .types.webrisk import SearchHashesRequest
from .types.webrisk import SearchHashesResponse
from .types.webrisk import SearchUrisRequest
from .types.webrisk import SearchUrisResponse
from .types.webrisk import Submission
from .types.webrisk import ThreatEntryAdditions
from .types.webrisk import ThreatEntryRemovals
from .types.webrisk import ThreatType


__all__ = (
"enums",
"types",
"CompressionType",
"ComputeThreatListDiffRequest",
"ComputeThreatListDiffResponse",
"CreateSubmissionRequest",
"RawHashes",
"RawIndices",
"RiceDeltaEncoding",
"SearchHashesRequest",
"SearchHashesResponse",
"SearchUrisRequest",
"SearchUrisResponse",
"Submission",
"ThreatEntryAdditions",
"ThreatEntryRemovals",
"ThreatType",
"WebRiskServiceClient",
)

0 comments on commit bbd2adf

Please sign in to comment.