Set user-agent (#660)

This commit is contained in:
Ross Scroggs
2017-12-21 07:06:29 -08:00
committed by Jay Lee
parent 5318a7a9da
commit 14fab0293e
2 changed files with 11 additions and 4 deletions

View File

@@ -1012,7 +1012,7 @@ def buildGAPIServiceObject(api, act_as, showAuthError=True):
GM_Globals[GM_CURRENT_API_USER] = act_as
GM_Globals[GM_CURRENT_API_SCOPES] = API_SCOPE_MAPPING[api]
credentials = getSvcAcctCredentials(GM_Globals[GM_CURRENT_API_SCOPES], act_as)
request = google_auth_httplib2.Request(http)
request = google_auth_httplib2.Request(http, user_agent=GAM_INFO)
try:
credentials.refresh(request)
service._http = google_auth_httplib2.AuthorizedHttp(credentials, http=http, user_agent=GAM_INFO)
@@ -1077,7 +1077,7 @@ def doCheckServiceAccount(users):
for scope in all_scopes:
try:
credentials = getSvcAcctCredentials([scope], user)
request = google_auth_httplib2.Request(httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL]))
request = google_auth_httplib2.Request(httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL]), user_agent=GAM_INFO)
credentials.refresh(request)
result = u'PASS'
except httplib2.ServerNotFoundError as e:

View File

@@ -80,8 +80,9 @@ class Request(transport.Request):
.. automethod:: __call__
"""
def __init__(self, http):
def __init__(self, http, user_agent=None):
self.http = http
self.user_agent = user_agent
def __call__(self, url, method='GET', body=None, headers=None,
timeout=None, **kwargs):
@@ -112,6 +113,8 @@ class Request(transport.Request):
)
try:
if self.user_agent is not None:
headers['user-agent'] = self.user_agent
_LOGGER.debug('Making request: %s %s', method, url)
response, data = self.http.request(
url, method=method, body=body, headers=headers, **kwargs)
@@ -157,6 +160,7 @@ class AuthorizedHttp(object):
http (httplib2.Http): The underlying HTTP object to
use to make requests. If not specified, a
:class:`httplib2.Http` instance will be constructed.
user_agent: the user-agent header
refresh_status_codes (Sequence[int]): Which HTTP status codes
indicate that credentials should be refreshed and the request
should be retried.
@@ -170,11 +174,12 @@ class AuthorizedHttp(object):
self.http = http
self.user_agent = user_agent
self.credentials = credentials
self.user_agent = user_agent
self._refresh_status_codes = refresh_status_codes
self._max_refresh_attempts = max_refresh_attempts
# Request instance used by internal methods (for example,
# credentials.refresh).
self._request = Request(self.http)
self._request = Request(self.http, self.user_agent)
def request(self, uri, method='GET', body=None, headers=None,
**kwargs):
@@ -186,6 +191,8 @@ class AuthorizedHttp(object):
# Make a copy of the headers. They will be modified by the credentials
# and we want to pass the original headers if we recurse.
request_headers = headers.copy() if headers is not None else {}
if self.user_agent is not None:
request_headers['user-agent'] = self.user_agent
if self.user_agent:
if request_headers.get('user-agent'):