Commit a9aa8bea authored by Joao Pedro Lopes's avatar Joao Pedro Lopes
Browse files

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
parent b54a366d
Pipeline #3328344 passed with stages
in 12 minutes and 2 seconds
import pkg_resources
try:
__version__ = pkg_resources.get_distribution("fts3-rest").version
__version__ = pkg_resources.get_distribution("fts3").version
except pkg_resources.DistributionNotFound:
__version__ = "3.x.x"
......@@ -21,6 +21,7 @@ import socket
import sys
from fts3.rest.client import Context
from fts3 import __version__ as CLIENT_VERSION
CONFIG_FILENAMES = ["/etc/fts3/fts3client.cfg", os.path.expanduser("~/.fts3client.cfg")]
......@@ -166,6 +167,7 @@ class Base:
)
def _create_context(self):
user_agent = "fts-rest-cli/" + CLIENT_VERSION
return Context(
self.options.endpoint,
ukey=self.options.ukey,
......@@ -173,4 +175,5 @@ class Base:
verify=self.options.verify,
access_token=self.options.access_token,
capath=self.options.capath,
user_agent=user_agent,
)
......@@ -23,6 +23,7 @@ from urllib.parse import quote
from M2Crypto import X509, RSA, EVP, BIO
from M2Crypto.ASN1 import UTC
from fts3 import __version__ as CLIENT_VERSION
from .exceptions import *
from .request import Request
......@@ -141,6 +142,12 @@ class Context(object):
)
return endpoint_info
def _set_user_agent(self, user_agent=None):
if user_agent is None:
self.user_agent = "fts-python-bindings/" + CLIENT_VERSION
else:
self.user_agent = user_agent
def __init__(
self,
endpoint,
......@@ -153,10 +160,12 @@ class Context(object):
request_class=Request,
connectTimeout=30,
timeout=30,
user_agent=None,
):
self.passwd = None
self.access_method = None
self._set_user_agent(user_agent)
self._set_endpoint(endpoint)
if no_creds:
self.ucert = self.ukey = self.access_token = None
......@@ -196,20 +205,34 @@ class Context(object):
if args:
query = "&".join("%s=%s" % (k, quote(v)) for k, v in args.items())
path += "?" + query
return self._requester.method("GET", "%s/%s" % (self.endpoint, path))
return self._requester.method(
"GET",
"%s/%s" % (self.endpoint, path),
headers={"User-Agent": self.user_agent},
)
def put(self, path, body):
return self._requester.method("PUT", "%s/%s" % (self.endpoint, path), body)
return self._requester.method(
"PUT",
"%s/%s" % (self.endpoint, path),
body=body,
headers={"User-Agent": self.user_agent},
)
def delete(self, path):
return self._requester.method("DELETE", "%s/%s" % (self.endpoint, path))
return self._requester.method(
"DELETE",
"%s/%s" % (self.endpoint, path),
headers={"User-Agent": self.user_agent},
)
def post_json(self, path, body):
if not isinstance(body, str):
body = json.dumps(body)
headers = {"Content-Type": "application/json", "User-Agent": self.user_agent}
return self._requester.method(
"POST",
"%s/%s" % (self.endpoint, path),
body,
headers={"Content-Type": "application/json"},
body=body,
headers=headers,
)
......@@ -87,10 +87,7 @@ class Request:
raise ServerError(str(code))
def method(self, method, url, body=None, headers=None, user=None, passw=None):
_headers = {
"Accept": "application/json",
"User-Agent": "fts-rest-client/3.12.0",
}
_headers = {"Accept": "application/json"}
if headers:
_headers.update(headers)
if self.access_token:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment