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: allow extra_api_info to be passed to api request #183

Merged
merged 4 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion google/cloud/_http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def _make_request(
headers=None,
target_object=None,
timeout=_DEFAULT_TIMEOUT,
extra_api_info=None,
unforced marked this conversation as resolved.
Show resolved Hide resolved
):
"""A low level method to send a request to the API.

Expand Down Expand Up @@ -327,7 +328,10 @@ def _make_request(
if content_type:
headers["Content-Type"] = content_type

headers[CLIENT_INFO_HEADER] = self.user_agent
if extra_api_info:
headers[CLIENT_INFO_HEADER] = f"{self.user_agent} {extra_api_info}"
else:
headers[CLIENT_INFO_HEADER] = self.user_agent
headers["User-Agent"] = self.user_agent

return self._do_request(
Expand Down Expand Up @@ -385,6 +389,7 @@ def api_request(
expect_json=True,
_target_object=None,
timeout=_DEFAULT_TIMEOUT,
extra_api_info=None,
):
"""Make a request over the HTTP transport to the API.

Expand Down Expand Up @@ -474,6 +479,7 @@ def api_request(
headers=headers,
target_object=_target_object,
timeout=timeout,
extra_api_info=extra_api_info,
)

if not 200 <= response.status_code < 300:
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,31 @@ def test_api_request_w_timeout(self):
data=None,
timeout=(2.2, 3.3),
)

def test_api_request_w_extra_api_info(self):
from google.cloud._http import CLIENT_INFO_HEADER

session = make_requests_session([self.EMPTY_JSON_RESPONSE])
client = mock.Mock(_http=session, spec=["_http"])
conn = self._make_mock_one(client)

EXTRA_API_INFO="gccl-invocation-id/testing-id-123"
result = conn.api_request("GET", "/", extra_api_info=EXTRA_API_INFO)

self.assertEqual(result, {})

expected_headers = {
"Accept-Encoding": "gzip",
"User-Agent": conn.user_agent,
CLIENT_INFO_HEADER: f"{conn.user_agent} {EXTRA_API_INFO}",
}
session.request.assert_called_once_with(
method="GET",
url=mock.ANY,
headers=expected_headers,
data=None,
timeout=self._get_default_timeout(),
)

def test_api_request_w_404(self):
from google.cloud import exceptions
Expand Down