Skip to content

Commit ed3d0a5

Browse files
author
j.s@google.com
committed
Adding a User-Agent header for logging with the v2 client.
1 parent e045231 commit ed3d0a5

4 files changed

Lines changed: 32 additions & 24 deletions

File tree

src/atom/client.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ class AtomPubClient(object):
2929
host = None
3030
auth_token = None
3131

32-
def __init__(self, http_client=None, host=None, auth_token=None, **kwargs):
32+
def __init__(self, http_client=None, host=None,
33+
auth_token=None, source=None, **kwargs):
3334
"""Creates a new AtomPubClient instance.
3435
3536
Args:
37+
source: The name of your application.
3638
http_client: An object capable of performing HTTP requests through a
3739
request method. This object is used to perform the request
3840
when the AtomPubClient's request method is called. Used to
@@ -49,6 +51,7 @@ def __init__(self, http_client=None, host=None, auth_token=None, **kwargs):
4951
self.host = host
5052
if auth_token is not None:
5153
self.auth_token = auth_token
54+
self.source = source
5255

5356
def request(self, method=None, uri=None, auth_token=None,
5457
http_request=None, **kwargs):
@@ -64,14 +67,9 @@ def request(self, method=None, uri=None, auth_token=None,
6467
auth_token: An authorization token object whose modify_request method
6568
sets the HTTP Authorization header.
6669
"""
67-
if http_request is None:
68-
http_request = atom.http_core.HttpRequest()
69-
# If the http_request didn't specify the target host, use the client's
70-
# default host (if set).
71-
if self.host is not None and http_request.uri.host is None:
72-
http_request.uri.host = self.host
7370
# Modify the request based on the AtomPubClient settings and parameters
7471
# passed in to the request.
72+
http_request = self.modify_request(http_request)
7573
if isinstance(uri, (str, unicode)):
7674
uri = atom.http_core.Uri.parse_uri(uri)
7775
if uri is not None:
@@ -127,12 +125,15 @@ def delete(self, uri=None, auth_token=None, http_request=None, **kwargs):
127125
Delete = delete
128126

129127
def modify_request(self, http_request):
130-
if self.host is not None:
131-
if http_request is None:
132-
http_request = atom.http_core.HttpRequest(
133-
uri=atom.http_core.Uri(host=self.host))
134-
elif http_request.uri.host is None:
135-
http_request.uri.host = self.host
128+
if http_request is None:
129+
http_request = atom.http_core.HttpRequest()
130+
if self.host is not None and http_request.uri.host is None:
131+
http_request.uri.host = self.host
132+
# Set the user agent header for logging purposes.
133+
if self.source:
134+
http_request.headers['User-Agent'] = '%s gdata-py/2.0.0' % self.source
135+
else:
136+
http_request.headers['User-Agent'] = 'gdata-py/2.0.0'
136137
return http_request
137138

138139
ModifyRequest = modify_request

src/atom/http_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import StringIO
3737

3838

39-
USER_AGENT = '%s GData-Python/1.3.2'
39+
USER_AGENT = '%s GData-Python/1.3.3'
4040

4141

4242
class Error(Exception):

src/gdata/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ class GDClient(atom.client.AtomPubClient):
172172
# The gsessionid is used by Google Calendar to prevent redirects.
173173
__gsessionid = None
174174
api_version = None
175+
# Name of the Google Data service when making a ClientLogin request.
175176
auth_service = None
177+
# URL prefixes which should be requested for AuthSub and OAuth.
176178
auth_scopes = None
177179

178180
def request(self, method=None, uri=None, auth_token=None,
@@ -250,7 +252,9 @@ def request(self, method=None, uri=None, auth_token=None,
250252
elif self.__gsessionid is not None:
251253
uri.query['gsessionid'] = self.__gsessionid
252254

253-
http_request = self.modify_request(http_request)
255+
# The AtomPubClient should call this class' modify_request before
256+
# performing the HTTP request.
257+
#http_request = self.modify_request(http_request)
254258

255259
response = atom.client.AtomPubClient.request(self, method=method,
256260
uri=uri, auth_token=auth_token, http_request=http_request, **kwargs)
@@ -417,10 +421,9 @@ def modify_request(self, http_request):
417421
Subclasses may override this method to add their own request
418422
modifications before the request is made.
419423
"""
420-
atom.client.AtomPubClient.modify_request(self, http_request)
424+
http_request = atom.client.AtomPubClient.modify_request(self,
425+
http_request)
421426
if self.api_version is not None:
422-
if http_request is None:
423-
http_request = atom.http_core.HttpRequest()
424427
http_request.headers['GData-Version'] = self.api_version
425428
return http_request
426429

tests/atom_tests/client_test.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def test_simple_request_with_no_client_defaults(self):
4949
self.assert_(response.getheader('Echo-Uri') == '/')
5050
self.assert_(response.getheader('Echo-Scheme') == 'http')
5151
self.assert_(response.getheader('Echo-Method') == 'GET')
52+
self.assertTrue(response.getheader('User-Agent').startswith('gdata-py/'))
5253

5354
def test_auth_request_with_no_client_defaults(self):
5455
client = atom.client.AtomPubClient(atom.mock_http_core.EchoHttpClient())
@@ -144,14 +145,17 @@ def modify_request(self, http_request):
144145
self.assert_(response.getheader('Content-Type') == 'application/xml')
145146

146147
def test_delete(self):
147-
client = atom.client.AtomPubClient(atom.mock_http_core.EchoHttpClient())
148+
client = atom.client.AtomPubClient(atom.mock_http_core.EchoHttpClient(),
149+
source='my new app')
148150
response = client.Delete('http://example.com/simple')
149-
self.assert_(response.getheader('Echo-Host') == 'example.com:None')
150-
self.assert_(response.getheader('Echo-Uri') == '/simple')
151-
self.assert_(response.getheader('Echo-Method') == 'DELETE')
151+
self.assertEqual(response.getheader('Echo-Host'), 'example.com:None')
152+
self.assertEqual(response.getheader('Echo-Uri'), '/simple')
153+
self.assertEqual(response.getheader('Echo-Method'), 'DELETE')
152154
response = client.delete(uri='http://example.com/d')
153-
self.assert_(response.getheader('Echo-Uri') == '/d')
154-
self.assert_(response.getheader('Echo-Method') == 'DELETE')
155+
self.assertEqual(response.getheader('Echo-Uri'), '/d')
156+
self.assertEqual(response.getheader('Echo-Method'), 'DELETE')
157+
self.assertTrue(
158+
response.getheader('User-Agent').startswith('my new app gdata-py/'))
155159

156160
def suite():
157161
return unittest.TestSuite((unittest.makeSuite(AtomPubClientEchoTest, 'test'),

0 commit comments

Comments
 (0)