Add user agent header wrapper to requests handled by AuthorizedHttp (#721)

* Revert patched google_auth_httplib2 and replace functionality by wrapping original library calls

* Wrap calls to google_auth_httplib2.Request__call__ to include a user-agent header.

* Fix bad dict key assignment syntax

* Add user agent header wrapper to requests handled by AuthorizedHttp
This commit is contained in:
ejochman
2018-04-16 17:34:27 -07:00
committed by Jay Lee
parent bbb486f7b2
commit f8c24bf86a

View File

@ -80,25 +80,32 @@ Go to the following link in your browser:
{address} {address}
""" """
# Override and wrap google_auth_httplib2.Request.__call__ so that the GAM # Override and wrap google_auth_httplib2 request methods so that the GAM
# user-agent string is inserted into HTTP request headers. # user-agent string is inserted into HTTP request headers.
google_auth_httplib2_request_call = google_auth_httplib2.Request.__call__ def _request_with_user_agent(request_method):
def _request_with_user_agent(self, *args, **kwargs): """Inserts the GAM user-agent header kwargs sent to a method."""
"""Inserts the GAM user-agent header in all google_auth_httplib2 requests."""
GAM_USER_AGENT = GAM_INFO GAM_USER_AGENT = GAM_INFO
if kwargs.get('headers') is not None: def wrapped_request_method(self, *args, **kwargs):
if kwargs['headers'].get('user-agent'): if kwargs.get('headers') is not None:
# Save the existing user-agent header and tack on the GAM user-agent. if kwargs['headers'].get('user-agent'):
kwargs['headers']['user-agent'] = '%s %s' % ( if GAM_USER_AGENT not in kwargs['headers']['user-agent']:
GAM_USER_AGENT, kwargs.headers['user-agent']) # Save the existing user-agent header and tack on the GAM user-agent.
kwargs['headers']['user-agent'] = '%s %s' % (
GAM_USER_AGENT, kwargs['headers']['user-agent'])
else:
kwargs['headers']['user-agent'] = GAM_USER_AGENT
else: else:
kwargs['headers']['user-agent'] = GAM_USER_AGENT kwargs['headers'] = {'user-agent': GAM_USER_AGENT}
else:
kwargs['headers'] = {'user-agent': GAM_USER_AGENT}
return google_auth_httplib2_request_call(self, *args, **kwargs)
google_auth_httplib2.Request.__call__ = _request_with_user_agent return request_method(self, *args, **kwargs)
return wrapped_request_method
google_auth_httplib2.Request.__call__ = _request_with_user_agent(
google_auth_httplib2.Request.__call__)
google_auth_httplib2.AuthorizedHttp.request = _request_with_user_agent(
google_auth_httplib2.AuthorizedHttp.request)
def showUsage(): def showUsage():
doGAMVersion(checkForArgs=False) doGAMVersion(checkForArgs=False)