Skip to content

Commit 90cd12d

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

File tree

8 files changed

+33
-8
lines changed

8 files changed

+33
-8
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

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
# NOTE: Maintainers, please do not require google-api-core>=2.x.x
4343
# Until this issue is closed
4444
# https://github.com/googleapis/google-cloud-python/issues/10566
45-
"google-api-core >= 1.31.5, <3.0.0.dev0,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0",
45+
"google-api-core[grpc] @ git+https://github.com/googleapis/python-api-core.git@add-api-version-header",
46+
#"google-api-core >= 1.31.5, <3.0.0.dev0,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0",
4647
"uritemplate>=3.0.1,<5",
4748
]
4849

testing/constraints-3.10.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
httplib2==0.19.0
33
google-auth==1.32.0
44
google-auth-httplib2==0.2.0
5-
google-api-core
65
uritemplate==3.0.1

testing/constraints-3.7.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
httplib2==0.19.0
1010
google-auth==1.32.0
1111
google-auth-httplib2==0.2.0
12-
google-api-core==1.31.5
1312
uritemplate==3.0.1

testing/constraints-3.8.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
httplib2==0.19.0
33
google-auth
44
google-auth-httplib2==0.2.0
5-
google-api-core==1.31.5
65
uritemplate==3.0.1

testing/constraints-3.9.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
httplib2==0.19.0
44
google-auth
55
google-auth-httplib2==0.2.0
6-
google-api-core==2.15.0
76
uritemplate==3.0.1

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 cloud 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)