Skip to content

Commit 8eaa3e8

Browse files
committed
feat: add support for reading apiVersion in discovery artifacts
1 parent cbed66f commit 8eaa3e8

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-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
@@ -33,6 +33,7 @@
3333

3434
_LIBRARY_VERSION = googleapiclient_version.__version__
3535
_PY_VERSION = platform.python_version()
36+
_API_VERSION_HEADER = "x-goog-api-version"
3637

3738
LOGGER = logging.getLogger(__name__)
3839

@@ -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_HEADER] = api_version
157163

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

tests/test_json_model.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,25 @@ def test_x_goog_api_client(self):
171171
+ " gl-python/"
172172
+ platform.python_version(),
173173
)
174+
175+
def test_x_goog_api_version(self):
176+
model = JsonModel(data_wrapper=False)
177+
178+
# test header composition for cloud clients that wrap discovery
179+
headers = {}
180+
path_params = {}
181+
query_params = {}
182+
body = {}
183+
api_version = "20240401"
184+
185+
headers, _, _, body = model.request(
186+
headers, path_params, query_params, body, api_version
187+
)
188+
189+
self.assertEqual(
190+
headers[googleapiclient.model._API_VERSION_HEADER],
191+
api_version,
192+
)
174193

175194
def test_bad_response(self):
176195
model = JsonModel(data_wrapper=False)

0 commit comments

Comments
 (0)