Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07f647c

Browse files
authored
Pass library and Python version in x-goog-api-client (googleapis#734)
1 parent 6562b58 commit 07f647c

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

googleapiclient/model.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626

2727
import json
2828
import logging
29+
import platform
2930

3031
from six.moves.urllib.parse import urlencode
3132

3233
from googleapiclient import __version__
3334
from googleapiclient.errors import HttpError
3435

36+
_PY_VERSION = platform.python_version()
3537

3638
LOGGER = logging.getLogger(__name__)
3739

@@ -144,7 +146,12 @@ def request(self, headers, path_params, query_params, body_value):
144146
headers['user-agent'] += ' '
145147
else:
146148
headers['user-agent'] = ''
147-
headers['user-agent'] += 'google-api-python-client/%s (gzip)' % __version__
149+
headers['user-agent'] += '(gzip)'
150+
if 'x-goog-api-client' in headers:
151+
headers['x-goog-api-client'] += ' '
152+
else:
153+
headers['x-goog-api-client'] = ''
154+
headers['x-goog-api-client'] += 'gdcl/%s gl-python/%s' % (__version__, _PY_VERSION)
148155

149156
if body_value is not None:
150157
headers['content-type'] = self.content_type

tests/test_http.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,26 +485,22 @@ def test_media_io_base_download_custom_request_headers(self):
485485
download = MediaIoBaseDownload(
486486
fd=self.fd, request=self.request, chunksize=3)
487487

488-
self.assertEqual(download._headers, {'Cache-Control':'no-store'})
488+
self.assertEqual(download._headers.get('Cache-Control'), 'no-store')
489489

490490
status, done = download.next_chunk()
491491

492-
result = self.fd.getvalue().decode('utf-8')
492+
result = json.loads(self.fd.getvalue().decode('utf-8'))
493493

494-
# we abuse the internals of the object we're testing, pay no attention
495-
# to the actual bytes= values here; we are just asserting that the
496-
# header we added to the original request is sent up to the server
497-
# on each call to next_chunk
494+
# assert that that the header we added to the original request is
495+
# sent up to the server on each call to next_chunk
498496

499-
self.assertEqual(json.loads(result),
500-
{"Cache-Control": "no-store", "range": "bytes=0-3"})
497+
self.assertEqual(result.get("Cache-Control"), "no-store")
501498

502499
download._fd = self.fd = BytesIO()
503500
status, done = download.next_chunk()
504501

505-
result = self.fd.getvalue().decode('utf-8')
506-
self.assertEqual(json.loads(result),
507-
{"Cache-Control": "no-store", "range": "bytes=51-54"})
502+
result = json.loads(self.fd.getvalue().decode('utf-8'))
503+
self.assertEqual(result.get("Cache-Control"), "no-store")
508504

509505
def test_media_io_base_download_handle_redirects(self):
510506
self.request.http = HttpMockSequence([

tests/test_json_model.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import copy
2727
import json
2828
import os
29+
import platform
2930
import unittest2 as unittest
3031
import httplib2
3132
import googleapiclient.model
@@ -143,8 +144,22 @@ def test_user_agent(self):
143144
headers, path_params, query_params, body)
144145

145146
self.assertEqual(headers['user-agent'],
146-
'my-test-app/1.23.4 google-api-python-client/' + __version__ +
147-
' (gzip)')
147+
'my-test-app/1.23.4 (gzip)')
148+
149+
def test_x_goog_api_client(self):
150+
model = JsonModel(data_wrapper=False)
151+
152+
# test header composition for cloud clients that wrap discovery
153+
headers = {'x-goog-api-client': 'gccl/1.23.4'}
154+
path_params = {}
155+
query_params = {}
156+
body = {}
157+
158+
headers, unused_params, unused_query, body = model.request(
159+
headers, path_params, query_params, body)
160+
161+
self.assertEqual(headers['x-goog-api-client'],
162+
'gccl/1.23.4' + ' gdcl/' + __version__ + ' gl-python/' + platform.python_version())
148163

149164
def test_bad_response(self):
150165
model = JsonModel(data_wrapper=False)

0 commit comments

Comments
 (0)