Skip to content

Commit a5be257

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

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from googleapiclient import version as googleapiclient_version
3232
from googleapiclient.errors import HttpError
33+
from google.api_core.gapic_v1.version_header import API_VERSION_METADATA_KEY
3334

3435
_LIBRARY_VERSION = googleapiclient_version.__version__
3536
_PY_VERSION = platform.python_version()
@@ -121,15 +122,18 @@ def _log_request(self, headers, path_params, query, body):
121122
LOGGER.info("query: %s", query)
122123
LOGGER.info("--request-end--")
123124

124-
def request(self, headers, path_params, query_params, body_value):
125+
def request(self, headers, path_params, query_params, body_value, api_version=None):
125126
"""Updates outgoing requests with a serialized body.
126127
127128
Args:
128129
headers: dict, request headers
129130
path_params: dict, parameters that appear in the request path
130131
query_params: dict, parameters that appear in the query
131132
body_value: object, the request body as a Python object, which must be
132-
serializable by json.
133+
serializable by json.
134+
api_version: str, The precise API version represented by this request,
135+
which will result in an API Version header being sent along with the
136+
HTTP request.
133137
Returns:
134138
A tuple of (headers, path_params, query, body)
135139
@@ -154,6 +158,8 @@ def request(self, headers, path_params, query_params, body_value):
154158
_LIBRARY_VERSION,
155159
_PY_VERSION,
156160
)
161+
if api_version:
162+
headers[API_VERSION_METADATA_KEY] = api_version
157163

158164
if body_value is not None:
159165
headers["content-type"] = self.content_type

tests/test_json_model.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from googleapiclient.errors import HttpError
3535
import googleapiclient.model
3636
from googleapiclient.model import JsonModel
37+
from google.api_core.gapic_v1.version_header import API_VERSION_METADATA_KEY
3738

3839
_LIBRARY_VERSION = googleapiclient_version.__version__
3940
CSV_TEXT_MOCK = "column1,column2,column3\nstring1,1.2,string2"
@@ -171,6 +172,25 @@ def test_x_goog_api_client(self):
171172
+ " gl-python/"
172173
+ platform.python_version(),
173174
)
175+
176+
def test_x_goog_api_version(self):
177+
model = JsonModel(data_wrapper=False)
178+
179+
# test header composition for clients that wrap discovery
180+
headers = {}
181+
path_params = {}
182+
query_params = {}
183+
body = {}
184+
api_version = "20240401"
185+
186+
headers, _, _, body = model.request(
187+
headers, path_params, query_params, body, api_version
188+
)
189+
190+
self.assertEqual(
191+
headers[API_VERSION_METADATA_KEY],
192+
api_version,
193+
)
174194

175195
def test_bad_response(self):
176196
model = JsonModel(data_wrapper=False)

0 commit comments

Comments
 (0)