Skip to content

Commit

Permalink
feat: allow extra_api_info to be passed to api request (#183)
Browse files Browse the repository at this point in the history
* feat: allow extra_api_info to be passed to api request

* add doc string

Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
  • Loading branch information
Aaron Gabriel Neyer and busunkim96 committed Apr 1, 2022
1 parent 515a3b2 commit 3f8e058
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
16 changes: 15 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,
):
"""A low level method to send a request to the API.
Expand Down Expand Up @@ -317,6 +318,10 @@ def _make_request(
Can also be passed as a tuple (connect_timeout, read_timeout).
See :meth:`requests.Session.request` documentation for details.
:type extra_api_info: string
:param extra_api_info: (optional) Extra api info to be appended to
the X-Goog-API-Client header
:rtype: :class:`requests.Response`
:returns: The HTTP response.
"""
Expand All @@ -327,7 +332,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 +393,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 @@ -446,6 +455,10 @@ def api_request(
Can also be passed as a tuple (connect_timeout, read_timeout).
See :meth:`requests.Session.request` documentation for details.
:type extra_api_info: string
:param extra_api_info: (optional) Extra api info to be appended to
the X-Goog-API-Client header
:raises ~google.cloud.exceptions.GoogleCloudError: if the response code
is not 200 OK.
:raises ValueError: if the response content type is not JSON.
Expand Down Expand Up @@ -474,6 +487,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 @@ -575,6 +575,31 @@ def test_api_request_w_timeout(self):
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

0 comments on commit 3f8e058

Please sign in to comment.