Skip to content

Commit 4192262

Browse files
feat: add support for reading apiVersion in discovery artifacts (#2380)
* feat: add support for reading apiVersion in discovery artifacts * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 611e168 commit 4192262

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-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: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@
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
3334

35+
try:
36+
from google.api_core.version_header import API_VERSION_METADATA_KEY
37+
38+
HAS_API_VERSION = True
39+
except ImportError:
40+
HAS_API_VERSION = False
41+
3442
_LIBRARY_VERSION = googleapiclient_version.__version__
3543
_PY_VERSION = platform.python_version()
3644

@@ -121,15 +129,18 @@ def _log_request(self, headers, path_params, query, body):
121129
LOGGER.info("query: %s", query)
122130
LOGGER.info("--request-end--")
123131

124-
def request(self, headers, path_params, query_params, body_value):
132+
def request(self, headers, path_params, query_params, body_value, api_version=None):
125133
"""Updates outgoing requests with a serialized body.
126134
127135
Args:
128136
headers: dict, request headers
129137
path_params: dict, parameters that appear in the request path
130138
query_params: dict, parameters that appear in the query
131139
body_value: object, the request body as a Python object, which must be
132-
serializable by json.
140+
serializable by json.
141+
api_version: str, The precise API version represented by this request,
142+
which will result in an API Version header being sent along with the
143+
HTTP request.
133144
Returns:
134145
A tuple of (headers, path_params, query, body)
135146
@@ -155,6 +166,15 @@ def request(self, headers, path_params, query_params, body_value):
155166
_PY_VERSION,
156167
)
157168

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

tests/test_json_model.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
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+
41+
HAS_API_VERSION = True
42+
except ImportError:
43+
HAS_API_VERSION = False
44+
3845
_LIBRARY_VERSION = googleapiclient_version.__version__
3946
CSV_TEXT_MOCK = "column1,column2,column3\nstring1,1.2,string2"
4047

@@ -172,6 +179,29 @@ def test_x_goog_api_client(self):
172179
+ platform.python_version(),
173180
)
174181

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

0 commit comments

Comments
 (0)