Repository: googleapis/google-api-python-client

Issue:
Modify User Agent on Discovery Service Class for Python
Hello,

We are using the discovery service in our cli tool and would like to modify the user agent string as a way for concord to collect data from the api calls made by our tool.

I tried this bit of code L53 to L60:

https://github.com/GoogleCloudPlatform/django-cloud-deploy/blob/master/django_cloud_deploy/cloudlib/project.py#L53

but when making print statements of the http request it shows the following L104 to 117:

---Creating GCP Project Call1-----
------Printing info for http call-------
headers: {'accept': 'application/json', 'accept-encoding': 'gzip, deflate', 'user-agent': 'google-api-python-client/1.7.5 (gzip)', 'content-type': 'application/json', 'content-length': '108'}
body: {"name": "Django Project", "projectId": "doesntmatter", "parent": {"id": "blabla", "type": "folder"}}
uri: https://cloudresourcemanager.googleapis.com/v1/projects?alt=json
---Finished GCP Project Call-----

I modified the http.py execute method within the google-api-python-client for the print statements.


Discussion:
after digging around, the following stack trade produces the following:  File "/Users/epad/Projects/django-cloud-deploy/venv/lib/python3.7/site-packages/googleapiclient/discovery.py", line 799, in method
    actual_path_params, actual_query_params, body_value)
  File "/Users/epad/Projects/django-cloud-deploy/venv/lib/python3.7/site-packages/googleapiclient/model.py", line 145, in request

looking at l799, it seems headers dont seem to be able to be customized, is this expected behavior?
I think the order may matter here -- could you try creating auth_http and then setting user_agent?

```
auth_http = google_auth_httplib2.AuthorizedHttp(credentials)
auth_http = http.set_user_agent(auth_http, 'django-cloud-deploy')
```
You are right that order matters, as it seems like it does what its suppose to, which is replace the new_request method, the problem with that it seems it isnt with the same API that request is used throught the package.

After further discussion, we are using the AuthorizedHttp Class, which has a different signature: 

    def request(self, uri, method='GET', body=None, headers=None,
                **kwargs):
https://github.com/GoogleCloudPlatform/google-auth-library-python-httplib2/blob/e7cd722281d1d897fa9ae6e3b6b78ae142778e6e/google_auth_httplib2.py#L178

vs the Http class:

    def request(
        self,
        uri,
        method="GET",
        body=None,
        headers=None,
        redirections=DEFAULT_MAX_REDIRECTS,
        connection_type=None)
https://github.com/httplib2/httplib2/blob/ece4af0a99d0429b29eaf0b34a06831d5ae11516/python3/httplib2/__init__.py#L1695

which is what new_request expects (it does say it in the doc)




Following questions are what im thinking:

1. Do i need to use authorizedHttp (Will test this out on my own) Answer: Yes, since i cannot pass http and credentials to the disocvery.build, they are explicitly exclusive.
2. Do we want to modify the set_user_agent string to check what type of http class was passed in? not sure if that is clean though.
3. Is there anywhere else where we can inject the header? Its super ugly, but i think i played around with: 


Final note, alternatively i understand the code enough to make a copy of set_user_agent function and handle it for AuthorizedHttp. 

Resolving patch:
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -1720,8 +1720,8 @@ def new_request(uri, method='GET', body=None, headers=None,
       headers['user-agent'] = user_agent + ' ' + headers['user-agent']
     else:
       headers['user-agent'] = user_agent
-    resp, content = request_orig(uri, method, body, headers,
-                        redirections, connection_type)
+    resp, content = request_orig(uri, method=method, body=body, headers=headers,
+                        redirections=redirections, connection_type=connection_type)
     return resp, content
 
   http.request = new_request
@@ -1761,8 +1761,8 @@ def new_request(uri, method='GET', body=None, headers=None,
             'OAuth 1.0 request made with Credentials after tunnel_patch.')
       headers['x-http-method-override'] = "PATCH"
       method = 'POST'
-    resp, content = request_orig(uri, method, body, headers,
-                        redirections, connection_type)
+    resp, content = request_orig(uri, method=method, body=body, headers=headers,
+                        redirections=redirections, connection_type=connection_type)
     return resp, content
 
   http.request = new_request


Test patch: