Skip to content

Commit 2fa4d76

Browse files
committed
FTS-1735: Use fts3 package version to set the HTTP User-Agent header with client version
- In addition the User-Agent header will also distinguish if the request was made using the CLI or the python bindings
1 parent 761037f commit 2fa4d76

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/fts3/cli/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import sys
2424

2525
from fts3.rest.client import Context
26-
26+
from fts3 import __version__ as CLIENT_VERSION
2727

2828
CONFIG_FILENAMES = [
2929
'/etc/fts3/fts3client.cfg',
@@ -138,7 +138,8 @@ def run(self):
138138
raise NotImplementedError('Run method not implemented in %s' % type(self).__name__)
139139

140140
def _create_context(self):
141+
user_agent = "fts-rest-cli/" + CLIENT_VERSION
141142
return Context(
142143
self.options.endpoint, ukey=self.options.ukey, ucert=self.options.ucert, verify=self.options.verify,
143-
access_token=self.options.access_token, capath=self.options.capath
144+
access_token=self.options.access_token, capath=self.options.capath, user_agent=user_agent
144145
)

src/fts3/rest/client/context.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
from datetime import datetime
1919
from M2Crypto import X509, RSA, EVP, BIO
20+
from fts3 import __version__ as CLIENT_VERSION
21+
2022
try:
2123
from M2Crypto.ASN1 import UTC
2224
except:
@@ -145,11 +147,18 @@ def _validate_endpoint(self):
145147
raise BadEndpoint("%s (%s)" % (self.endpoint, str(e))), None, sys.exc_info()[2]
146148
return endpoint_info
147149

150+
def _set_user_agent(self, user_agent=None):
151+
if user_agent is None:
152+
self.user_agent = 'fts-python-bindings/' + CLIENT_VERSION
153+
else:
154+
self.user_agent = user_agent
155+
148156
def __init__(self, endpoint, ucert=None, ukey=None, verify=True, access_token=None, no_creds=False, capath=None,
149-
request_class=PycurlRequest, connectTimeout=30, timeout=30):
157+
request_class=PycurlRequest, connectTimeout=30, timeout=30, user_agent=None):
150158
self.passwd = None
151159
self.access_method = None
152160

161+
self._set_user_agent(user_agent)
153162
self._set_endpoint(endpoint)
154163
if no_creds:
155164
self.ucert = self.ukey = self.access_token = None
@@ -180,21 +189,24 @@ def get(self, path, args=None):
180189
query = '&'.join(map(lambda (k, v): "%s=%s" % (k, urllib.quote(v)), args.iteritems()))
181190
path += '?' + query
182191
return self._requester.method('GET',
183-
"%s/%s" % (self.endpoint, path))
192+
"%s/%s" % (self.endpoint, path),
193+
headers = {'User-Agent': self.user_agent})
184194

185195
def put(self, path, body):
186196
return self._requester.method('PUT',
187197
"%s/%s" % (self.endpoint, path),
188-
body)
198+
body = body, headers = {'User-Agent': self.user_agent})
189199

190200
def delete(self, path):
191201
return self._requester.method('DELETE',
192-
"%s/%s" % (self.endpoint, path))
202+
"%s/%s" % (self.endpoint, path),
203+
headers = {'User-Agent': self.user_agent})
193204

194205
def post_json(self, path, body):
195206
if not isinstance(body, str) and not isinstance(body, unicode):
196207
body = json.dumps(body)
208+
headers = {'Content-Type': 'application/json', 'User-Agent': self.user_agent}
197209
return self._requester.method('POST',
198210
"%s/%s" % (self.endpoint, path),
199-
body,
200-
headers={'Content-Type': 'application/json'})
211+
body = body,
212+
headers = headers)

src/fts3/rest/client/pycurlRequest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def method(self, method, url, body=None, headers=None):
129129
elif method == 'PUT':
130130
self.curl_handle.setopt(pycurl.UPLOAD, True)
131131

132-
_headers = {'Accept': 'application/json', "User-Agent": "fts-rest-client/3.11.0"}
132+
_headers = {'Accept': 'application/json'}
133133
if headers:
134134
_headers.update(headers)
135135
if self.access_token:

0 commit comments

Comments
 (0)