@@ -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
0 commit comments