Skip to content

Commit

Permalink
Add support for API version system parameter. (#325)
Browse files Browse the repository at this point in the history
This is a method-specific parameter specified in the discovery doc that
can be passed as a system parameter to the API request (see
https://cloud.google.com/apis/docs/system-parameters).
  • Loading branch information
cglouch authored Sep 5, 2024
1 parent e221bea commit 484c671
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
10 changes: 10 additions & 0 deletions apitools/base/py/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class ApiMethodInfo(messages.Message):
supports_download: (boolean) If True, this method supports
downloading the request via the `alt=media` query
parameter.
api_version_param: API version system parameter for this
method.
"""

relative_path = messages.StringField(1)
Expand All @@ -115,6 +117,7 @@ class ApiMethodInfo(messages.Message):
request_field = messages.StringField(11, default='')
upload_config = messages.MessageField(ApiUploadInfo, 12)
supports_download = messages.BooleanField(13, default=False)
api_version_param = messages.StringField(14)


REQUEST_IS_BODY = '<request>'
Expand Down Expand Up @@ -634,6 +637,12 @@ def __SetBaseHeaders(self, http_request, client):
http_request.headers['accept'] = 'application/json'
http_request.headers['accept-encoding'] = 'gzip, deflate'

def __SetBaseSystemParams(self, http_request, method_config):
"""Fill in the system parameters to always set for the method."""
if method_config.api_version_param:
http_request.headers['X-Goog-Api-Version'] = (
method_config.api_version_param)

def __SetBody(self, http_request, method_config, request, upload):
"""Fill in the body on http_request."""
if not method_config.request_field:
Expand Down Expand Up @@ -672,6 +681,7 @@ def PrepareHttpRequest(self, method_config, request, global_params=None,
http_request = http_wrapper.Request(
http_method=method_config.http_method)
self.__SetBaseHeaders(http_request, self.__client)
self.__SetBaseSystemParams(http_request, method_config)
self.__SetBody(http_request, method_config, request, upload)

url_builder = _UrlBuilder(
Expand Down
18 changes: 14 additions & 4 deletions apitools/base/py/base_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ def fakeMakeRequest(*unused_args, **unused_kwargs):
with self.assertRaises(exceptions.HttpBadRequestError) as err:
service._RunMethod(method_config, request)
http_error = err.exception
self.assertEquals('http://www.google.com', http_error.url)
self.assertEquals('{"field": "abc"}', http_error.content)
self.assertEquals(method_config, http_error.method_config)
self.assertEquals(request, http_error.request)
self.assertEqual('http://www.google.com', http_error.url)
self.assertEqual('{"field": "abc"}', http_error.content)
self.assertEqual(method_config, http_error.method_config)
self.assertEqual(request, http_error.request)

def testQueryEncoding(self):
method_config = base_api.ApiMethodInfo(
Expand Down Expand Up @@ -338,6 +338,16 @@ def testOverwritesTransferUrlBase(self):
expected = 'http://custom.p.googleapis.com/path'
self.assertEqual(observed, expected)

def testApiVersionSystemParameter(self):
method_config = base_api.ApiMethodInfo(
request_type_name='SimpleMessage', api_version_param='2024-01-01')
service = FakeService()
request = SimpleMessage()
http_request = service.PrepareHttpRequest(method_config, request)
self.assertIn('X-Goog-Api-Version', http_request.headers)
self.assertEqual(
'2024-01-01', http_request.headers['X-Goog-Api-Version'])


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions apitools/gen/service_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ def __ComputeMethodInfo(self, method_description, request, response,
method_description.get('mediaUpload'), method_id)
method_info.supports_download = method_description.get(
'supportsMediaDownload', False)
if method_description.get('apiVersion'):
method_info.api_version_param = method_description.get('apiVersion')
self.__all_scopes.update(method_description.get('scopes', ()))
for param, desc in method_description.get('parameters', {}).items():
param = self.__names.CleanName(param)
Expand Down

0 comments on commit 484c671

Please sign in to comment.