caosdb.connection.utils module

Utility functions for the connection module.

auth_token_to_cookie.

Urlencode an auth token string and format it as a cookie.

Parameters

auth_token (str) – The plain auth token string.

Raises

TypeError – If the auth_token was None

Returns

A cookie

Return type

str

caosdb.connection.utils.check_python_ssl_version(hexversion)

Check the python version.

If version < 2.7.9 or 3.0 <= version < 3.2 the ssl library does not actually verify the ssl certificates send by the server. That is evil and these versions shall not be used.

Parameters

hexversion (int) – A python version.

Raises

Exception – If the version does not fully support ssl encryption.

caosdb.connection.utils.make_uri_path(segments=None, query=None)

Url-encode all segments, concat them with slashes and append the query.

Examples

>>> make_uri_path(['a','b'])
'a/b'
>>> make_uri_path(['a','ö'])
'a/%C3%B6'
>>> make_uri_path(['a','b'], {'key': 'val'})
'a/b?key=val'
Parameters
  • segments (list of str) – The segments of the path

  • query (dict) – A dict of str keys with None or str values.

Returns

A relative uri path (no host information, possibly no root path).

Return type

str

caosdb.connection.utils.parse_auth_token(cookie)

parse_auth_token.

Parse an auth token from a cookie.

Parameters

cookie (str) – A cookie with an urlencoded authtoken.

Returns

An auth token string.

Return type

str

caosdb.connection.utils.parse_url(url)
caosdb.connection.utils.quote(string)
caosdb.connection.utils.unquote(string)

unquote.

Decode an urlencoded string into a plain text string.

caosdb.connection.utils.urlencode(query)

Convert a dict of into a url-encoded (unicode) string.

This is basically a python2/python3 compatibility wrapper for the respective functions in the urllib module with some slight modifications.

1) A None is translated to an empty string. >>> urlencode({‘key’: None}) ‘key=’

2) Unicode strings are allowed for python2. >>> urlencode({‘kèy’: ‘välüe’}) ‘k%C3%A8y=v%C3%A4l%C3%BCe’

3) All other parameters which can be passed to the respective functions are not implemented here and the default parameters will be used. >>> urlencode({‘key’: [‘val1’, ‘val2’]}, doseq=True) Traceback (most recent call last):

TypeError: urlencode() got an unexpected keyword argument ‘doseq’

Otherwise, this functions works exactly as its counterparts in the urllib modules when they are called with only the query parameter.

Parameters

query (dict) – A dict of str or unicode keys with None, unicode or str values.

Returns

A series of key=value pairs separated by &.

Return type

str

Raises

AttributeError – If a key or a value does not have a ‘encode’ method.