Skip to content

Commit 8644c91

Browse files
committed
feat: add support for reading apiVersion in discovery artifacts
1 parent 611e168 commit 8644c91

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

googleapiclient/discovery.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,11 @@ def method(self, **kwargs):
11701170
elif "response" not in methodDesc:
11711171
model = RawModel()
11721172

1173+
api_version = methodDesc.get("apiVersion", None)
1174+
11731175
headers = {}
11741176
headers, params, query, body = model.request(
1175-
headers, actual_path_params, actual_query_params, body_value
1177+
headers, actual_path_params, actual_query_params, body_value, api_version
11761178
)
11771179

11781180
expanded_url = uritemplate.expand(pathUrl, params)

googleapiclient/model.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@
2727
import logging
2828
import platform
2929
import urllib
30+
import warnings
3031

3132
from googleapiclient import version as googleapiclient_version
3233
from googleapiclient.errors import HttpError
34+
try:
35+
from google.api_core.version_header import API_VERSION_METADATA_KEY
36+
HAS_API_VERSION = True
37+
except ImportError:
38+
HAS_API_VERSION = False
3339

3440
_LIBRARY_VERSION = googleapiclient_version.__version__
3541
_PY_VERSION = platform.python_version()
@@ -121,15 +127,18 @@ def _log_request(self, headers, path_params, query, body):
121127
LOGGER.info("query: %s", query)
122128
LOGGER.info("--request-end--")
123129

124-
def request(self, headers, path_params, query_params, body_value):
130+
def request(self, headers, path_params, query_params, body_value, api_version=None):
125131
"""Updates outgoing requests with a serialized body.
126132
127133
Args:
128134
headers: dict, request headers
129135
path_params: dict, parameters that appear in the request path
130136
query_params: dict, parameters that appear in the query
131137
body_value: object, the request body as a Python object, which must be
132-
serializable by json.
138+
serializable by json.
139+
api_version: str, The precise API version represented by this request,
140+
which will result in an API Version header being sent along with the
141+
HTTP request.
133142
Returns:
134143
A tuple of (headers, path_params, query, body)
135144
@@ -155,6 +164,15 @@ def request(self, headers, path_params, query_params, body_value):
155164
_PY_VERSION,
156165
)
157166

167+
if api_version and HAS_API_VERSION:
168+
headers[API_VERSION_METADATA_KEY] = api_version
169+
elif api_version:
170+
warnings.warn(
171+
"The `api_version` argument is ignored as a newer version of "
172+
"`google-api-core` is required to use this feature."
173+
"Please upgrade `google-api-core` to 2.19.0 or newer."
174+
)
175+
158176
if body_value is not None:
159177
headers["content-type"] = self.content_type
160178
body_value = self.serialize(body_value)

tests/test_json_model.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
import googleapiclient.model
3636
from googleapiclient.model import JsonModel
3737

38+
try:
39+
from google.api_core.version_header import API_VERSION_METADATA_KEY
40+
HAS_API_VERSION = True
41+
except ImportError:
42+
HAS_API_VERSION = False
43+
3844
_LIBRARY_VERSION = googleapiclient_version.__version__
3945
CSV_TEXT_MOCK = "column1,column2,column3\nstring1,1.2,string2"
4046

@@ -172,6 +178,26 @@ def test_x_goog_api_client(self):
172178
+ platform.python_version(),
173179
)
174180

181+
@unittest.skipIf(not HAS_API_VERSION, "Skip this test when an older version of google-api-core is used")
182+
def test_x_goog_api_version(self):
183+
model = JsonModel(data_wrapper=False)
184+
185+
# test header composition for clients that wrap discovery
186+
headers = {}
187+
path_params = {}
188+
query_params = {}
189+
body = {}
190+
api_version = "20240401"
191+
192+
headers, _, _, body = model.request(
193+
headers, path_params, query_params, body, api_version
194+
)
195+
196+
self.assertEqual(
197+
headers[API_VERSION_METADATA_KEY],
198+
api_version,
199+
)
200+
175201
def test_bad_response(self):
176202
model = JsonModel(data_wrapper=False)
177203
resp = httplib2.Response({"status": "401"})

0 commit comments

Comments
 (0)