Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add x-goog-api-client header to rest clients #888

Merged
merged 6 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,11 @@ class {{ service.name }}RestTransport({{ service.name }}Transport):
url += '?{}'.format('&'.join(query_params)).replace(' ', '+')

# Send the request
headers = dict(metadata)
headers['Content-Type'] = 'application/json'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naive question: this is not x-goog-api-client. Where does the name content-type come from? Is there documentation somewhere describing it?

Copy link
Contributor Author

@vam-google vam-google May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Content-Type header is a part of HTTP standard: https://datatracker.ietf.org/doc/html/rfc2616#section-14.17

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@software-dov See also the internal REGAPIC gRPC transcoding design doc, where we call this out explicitly (relatively recent addition, since we found some RPCs fail without it)

response = self._session.{{ method.http_opt['verb'] }}(
url,
headers=headers,
{% if 'body' in method.http_opt %}
data=body,
{% endif %}
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