Skip to content

Commit

Permalink
Merge branch 'master' into docs/development
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 authored May 24, 2021
2 parents d305938 + 99286e0 commit 4c5f67c
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 150 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## [0.47.0](https://www.github.com/googleapis/gapic-generator-python/compare/v0.46.3...v0.47.0) (2021-05-13)


### Features

* support protobuf method deprecation option [gapic-generator-python] ([#875](https://www.github.com/googleapis/gapic-generator-python/issues/875)) ([5a5a839](https://www.github.com/googleapis/gapic-generator-python/commit/5a5a839b99d78ec5a5c52452e57c289b55ad1db5))

### [0.46.3](https://www.github.com/googleapis/gapic-generator-python/compare/v0.46.2...v0.46.3) (2021-05-12)


### Bug Fixes

* consistently use _pb2 identifier ([#883](https://www.github.com/googleapis/gapic-generator-python/issues/883)) ([d789c84](https://www.github.com/googleapis/gapic-generator-python/commit/d789c84d0d686bdb2d88179041b4c04cc32a3e66))

### [0.46.2](https://www.github.com/googleapis/gapic-generator-python/compare/v0.46.1...v0.46.2) (2021-05-12)


### Bug Fixes

* fix incorrectly referenced exceptions, add missing port to tests ([#873](https://www.github.com/googleapis/gapic-generator-python/issues/873)) ([40078c4](https://www.github.com/googleapis/gapic-generator-python/commit/40078c46b21a0dfa489d4cd80ed7d95bb542f3c3)), closes [#872](https://www.github.com/googleapis/gapic-generator-python/issues/872)

### [0.46.1](https://www.github.com/googleapis/gapic-generator-python/compare/v0.46.0...v0.46.1) (2021-05-07)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
{{ method.client_output.meta.doc|rst(width=72, indent=16, source_format='rst') }}
{% endif %}
"""
{% if method.is_deprecated %}
warnings.warn("{{ method.name|snake_case }} is deprecated", warnings.DeprecationWarning)
{% endif %}
{% if not method.client_streaming %}
# Create or coerce a protobuf request object.
{% if method.flattened_fields %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class {{ service.name }}Transport(metaclass=abc.ABCMeta):
{% if method.retry.backoff_multiplier %}multiplier={{ method.retry.backoff_multiplier }},{% endif %}
predicate=retries.if_exception_type(
{% for ex in method.retry.retryable_exceptions|sort(attribute='__name__') %}
exceptions.{{ ex.__name__ }},
core_exceptions.{{ ex.__name__ }},
{% endfor %}
),
deadline={{ method.timeout }},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ def test_{{ method.name|snake_case }}_flattened():
assert len(call.mock_calls) == 1
_, args, _ = call.mock_calls[0]
{% for key, field in method.flattened_fields.items() %}{% if not field.oneof or field.proto3_optional %}
{% if field.ident|string() == 'timestamp.Timestamp' %}
{% if field.ident|string() == 'timestamp_pb2.Timestamp' %}
assert TimestampRule().to_proto(args[0].{{ key }}) == {{ field.mock_value }}
{% elif field.ident|string() == 'duration.Duration' %}
{% elif field.ident|string() == 'duration_pb2.Duration' %}
assert DurationRule().to_proto(args[0].{{ key }}) == {{ field.mock_value }}
{% else %}
assert args[0].{{ key }} == {{ field.mock_value }}
Expand Down
8 changes: 4 additions & 4 deletions gapic/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def get_response(
sample_templates, client_templates = utils.partition(
lambda fname: os.path.basename(
fname) == samplegen.DEFAULT_TEMPLATE_NAME,
self._env.loader.list_templates(),
self._env.loader.list_templates(), # type: ignore
)

# Iterate over each template and add the appropriate output files
Expand All @@ -113,7 +113,7 @@ def get_response(
sample_output = self._generate_samples_and_manifest(
api_schema, self._env.get_template(sample_templates[0]),
opts=opts,
)
)
output_files.update(sample_output)

# Return the CodeGeneratorResponse output.
Expand Down Expand Up @@ -286,10 +286,10 @@ def _render_template(
for service in api_schema.services.values():
if (
(skip_subpackages
and service.meta.address.subpackage != api_schema.subpackage_view)
and service.meta.address.subpackage != api_schema.subpackage_view)
or
('transport' in template_name
and not self._is_desired_transport(template_name, opts))
and not self._is_desired_transport(template_name, opts))
or
# TODO(yon-mg) - remove when rest async implementation resolved
# temporarily stop async client gen while rest async is unkown
Expand Down
10 changes: 5 additions & 5 deletions gapic/schema/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ def __str__(self) -> str:
if self.module:
module_name = self.module

# If collisions are registered and conflict with our module,
# use the module alias instead.
if self.module_alias:
module_name = self.module_alias

# This module is from a different proto package
# Most commonly happens for a common proto
# https://pypi.org/project/googleapis-common-protos/
if not self.proto_package.startswith(self.api_naming.proto_package):
module_name = f'{self.module}_pb2'

# If collisions are registered and conflict with our module,
# use the module alias instead.
if self.module_alias:
module_name = self.module_alias

# Return the dot-separated Python identifier.
return '.'.join((module_name,) + self.parent + (self.name,))

Expand Down
6 changes: 6 additions & 0 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,14 @@ def _client_output(self, enable_asyncio: bool):
# Return the usual output.
return self.output

@property
def is_deprecated(self) -> bool:
"""Returns true if the method is deprecated, false otherwise."""
return descriptor_pb2.MethodOptions.HasField(self.options, 'deprecated')

# TODO(yon-mg): remove or rewrite: don't think it performs as intended
# e.g. doesn't work with basic case of gRPC transcoding

@property
def field_headers(self) -> Sequence[str]:
"""Return the field headers defined for this method."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class {{ service.async_client_name }}:
{% if method.retry.backoff_multiplier %}multiplier={{ method.retry.backoff_multiplier }},{% endif %}
predicate=retries.if_exception_type(
{% for ex in method.retry.retryable_exceptions|sort(attribute="__name__") %}
exceptions.{{ ex.__name__ }},
core_exceptions.{{ ex.__name__ }},
{% endfor %}
),
deadline={{ method.timeout }},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
{{ method.client_output.meta.doc|rst(width=72, indent=16, source_format="rst") }}
{% endif %}
"""
{% if method.is_deprecated %}
warnings.warn("{{ method.name|snake_case }} is deprecated", warnings.DeprecationWarning)
{% endif %}
{% if not method.client_streaming %}
# Create or coerce a protobuf request object.
{% if method.flattened_fields %}
Expand Down Expand Up @@ -476,9 +479,9 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
metadata: Sequence[Tuple[str, str]] = (),
) -> policy_pb2.Policy:
r"""Sets the IAM access control policy on the specified function.

Replaces any existing policy.

Args:
request (:class:`~.iam_policy_pb2.SetIamPolicyRequest`):
The request object. Request message for `SetIamPolicy`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import abc
from typing import Awaitable, Callable, Dict, Optional, Sequence, Union
import packaging.version
import pkg_resources
{% if 'rest' in opts.transport %}
from requests import __version__ as requests_version
{% endif %}

import google.auth # type: ignore
import google.api_core # type: ignore
Expand Down Expand Up @@ -33,6 +36,12 @@ try:
gapic_version=pkg_resources.get_distribution(
'{{ api.naming.warehouse_package_name }}',
).version,
{% if 'grpc' not in opts.transport %}
grpc_version=None,
{% endif %}
{% if 'rest' in opts.transport %}
rest_version=requests_version,
{% endif %}
)
except pkg_resources.DistributionNotFound:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo()
Expand All @@ -46,8 +55,6 @@ except AttributeError:
except pkg_resources.DistributionNotFound: # pragma: NO COVER
_GOOGLE_AUTH_VERSION = None

_API_CORE_VERSION = google.api_core.__version__


class {{ service.name }}Transport(abc.ABC):
"""Abstract transport class for {{ service.name }}."""
Expand Down Expand Up @@ -121,10 +128,9 @@ class {{ service.name }}Transport(abc.ABC):
self._credentials = credentials


# TODO(busunkim): These two class methods are in the base transport
# TODO(busunkim): This method is in the base transport
# to avoid duplicating code across the transport classes. These functions
# should be deleted once the minimum required versions of google-api-core
# and google-auth are increased.
# should be deleted once the minimum required versions of google-auth is increased.

# TODO: Remove this function once google-auth >= 1.25.0 is required
@classmethod
Expand All @@ -143,25 +149,6 @@ class {{ service.name }}Transport(abc.ABC):

return scopes_kwargs

# TODO: Remove this function once google-api-core >= 1.26.0 is required
@classmethod
def _get_self_signed_jwt_kwargs(cls, host: str, scopes: Optional[Sequence[str]]) -> Dict[str, Union[Optional[Sequence[str]], str]]:
"""Returns kwargs to pass to grpc_helpers.create_channel depending on the google-api-core version"""

self_signed_jwt_kwargs: Dict[str, Union[Optional[Sequence[str]], str]] = {}

if _API_CORE_VERSION and (
packaging.version.parse(_API_CORE_VERSION)
>= packaging.version.parse("1.26.0")
):
self_signed_jwt_kwargs["default_scopes"] = cls.AUTH_SCOPES
self_signed_jwt_kwargs["scopes"] = scopes
self_signed_jwt_kwargs["default_host"] = cls.DEFAULT_HOST
else:
self_signed_jwt_kwargs["scopes"] = scopes or cls.AUTH_SCOPES

return self_signed_jwt_kwargs


def _prep_wrapped_messages(self, client_info):
# Precompute the wrapped methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,14 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
and ``credentials_file`` are passed.
"""

self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)

return grpc_helpers.create_channel(
host,
credentials=credentials,
credentials_file=credentials_file,
quota_project_id=quota_project_id,
**self_signed_jwt_kwargs,
default_scopes=cls.AUTH_SCOPES,
scopes=scopes,
default_host=cls.DEFAULT_HOST,
**kwargs
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
aio.Channel: A gRPC AsyncIO channel object.
"""

self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)

return grpc_helpers_async.create_channel(
host,
credentials=credentials,
credentials_file=credentials_file,
quota_project_id=quota_project_id,
**self_signed_jwt_kwargs,
default_scopes=cls.AUTH_SCOPES,
scopes=scopes,
default_host=cls.DEFAULT_HOST,
**kwargs
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ from typing import Callable, Dict, Optional, Sequence, Tuple
from google.api_core import operations_v1
{% endif %}
from google.api_core import gapic_v1 # type: ignore
from google.auth import credentials as ga_credentials # type: ignore
from google.auth.transport.grpc import SslCredentials # type: ignore
from google.api_core import exceptions as core_exceptions # type: ignore
from google.auth import credentials as ga_credentials # type: ignore
from google.auth.transport.grpc import SslCredentials # type: ignore

import grpc # type: ignore

Expand Down Expand Up @@ -109,13 +110,13 @@ class {{ service.name }}RestTransport({{ service.name }}Transport):
if self._operations_client is None:
from google.api_core import grpc_helpers

self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(self._host, self._scopes)

self._operations_client = operations_v1.OperationsClient(
grpc_helpers.create_channel(
self._host,
credentials=self._credentials,
**self_signed_jwt_kwargs,
default_scopes=cls.AUTH_SCOPES,
scopes=self._scopes,
default_host=cls.DEFAULT_HOST,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
Expand Down Expand Up @@ -207,15 +208,20 @@ class {{ service.name }}RestTransport({{ service.name }}Transport):
url += '?{}'.format('&'.join(query_params)).replace(' ', '+')

# Send the request
headers = dict(metadata)
headers['Content-Type'] = 'application/json'
response = self._session.{{ method.http_opt['verb'] }}(
url,
headers=headers,
{% if 'body' in method.http_opt %}
data=body,
{% endif %}
)

# Raise requests.exceptions.HTTPError if the status code is >= 400
response.raise_for_status()
# In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception
# subclass.
if response.status_code >= 400:
raise core_exceptions.from_http_response(response)
{% if not method.void %}

# Return the response
Expand Down
2 changes: 1 addition & 1 deletion gapic/templates/setup.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ setuptools.setup(
platforms='Posix; MacOS X; Windows',
include_package_data=True,
install_requires=(
'google-api-core[grpc] >= 1.22.2, < 2.0.0dev',
'google-api-core[grpc] >= 1.27.0, < 2.0.0dev',
'libcst >= 0.2.5',
'proto-plus >= 1.15.0',
'packaging >= 14.3',
Expand Down
Loading

0 comments on commit 4c5f67c

Please sign in to comment.