summaryrefslogtreecommitdiff
path: root/swiftclient/client.py
diff options
context:
space:
mode:
authorJohn Dickinson <me@not.mn>2014-03-28 15:45:37 -0700
committerJohn Dickinson <me@not.mn>2014-03-30 10:29:07 -0700
commit4f3d6e7f3af0c518d7a55b930947a6fbd5b0a5ae (patch)
tree5dea2ec8b98b9629bd4e023ddab51c49015743d3 /swiftclient/client.py
parentcdf6f84c360088d39af1b8e1745c102fc44ac362 (diff)
downloadpython-swiftclient-4f3d6e7f3af0c518d7a55b930947a6fbd5b0a5ae.tar.gz
set user-agent header
Change-Id: Ia67807667b4b5177d83cce9fcf16d98dc5024fbc
Diffstat (limited to 'swiftclient/client.py')
-rw-r--r--swiftclient/client.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/swiftclient/client.py b/swiftclient/client.py
index ad77817..c5b9a24 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -29,6 +29,7 @@ from urllib import quote as _quote
from urlparse import urlparse, urlunparse
from time import sleep, time
+from swiftclient import version as swiftclient_version
from swiftclient.exceptions import ClientException, InvalidHeadersException
from swiftclient.utils import LengthWrapper
@@ -133,7 +134,7 @@ except ImportError:
class HTTPConnection:
def __init__(self, url, proxy=None, cacert=None, insecure=False,
- ssl_compression=False):
+ ssl_compression=False, default_user_agent=None):
"""
Make an HTTPConnection or HTTPSConnection
@@ -147,6 +148,12 @@ class HTTPConnection:
:param ssl_compression: SSL compression should be disabled by default
and this setting is not usable as of now. The
parameter is kept for backward compatibility.
+ :param default_user_agent: Set the User-Agent header on every request.
+ If set to None (default), the user agent
+ will be "python-swiftclient-<version>". This
+ may be overridden on a per-request basis by
+ explicitly setting the user-agent header on
+ a call to request().
:raises ClientException: Unable to handle protocol scheme
"""
self.url = url
@@ -171,6 +178,10 @@ class HTTPConnection:
)
}
self.requests_args['stream'] = True
+ if default_user_agent is None:
+ default_user_agent = \
+ 'python-swiftclient-%s' % swiftclient_version.version_string
+ self.default_user_agent = default_user_agent
def _request(self, *arg, **kwarg):
""" Final wrapper before requests call, to be patched in tests """
@@ -178,8 +189,11 @@ class HTTPConnection:
def request(self, method, full_path, data=None, headers={}, files=None):
""" Encode url and header, then call requests.request """
- headers = dict((encode_utf8(x), encode_utf8(y)) for x, y in
+ headers = dict((encode_utf8(x.lower()), encode_utf8(y)) for x, y in
headers.items())
+ # set a default User-Agent header if it wasn't passed in
+ if 'user-agent' not in headers:
+ headers['user-agent'] = self.default_user_agent
url = encode_utf8("%s://%s%s" % (
self.parsed_url.scheme,
self.parsed_url.netloc,