Skip to content

Commit

Permalink
Set user-agent (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
taers232c authored and jay0lee committed Dec 21, 2017
1 parent 5318a7a commit 14fab02
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/gam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 9 additions & 2 deletions src/google_auth_httplib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -112,6 +113,8 @@ def __call__(self, url, method='GET', body=None, headers=None,
)

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)
Expand Down Expand Up @@ -157,6 +160,7 @@ def __init__(self, credentials, http=None, user_agent=None,
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.
Expand All @@ -170,11 +174,12 @@ def __init__(self, credentials, http=None, user_agent=None,
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):
Expand All @@ -186,6 +191,8 @@ def request(self, uri, method='GET', body=None, headers=None,
# 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'):
Expand Down

0 comments on commit 14fab02

Please sign in to comment.