linkahead.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

linkahead.connection.utils.make_uri_path(segments: list[str] | None = None, query: dict[str, str | None] | None = None) str

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

linkahead.connection.utils.parse_auth_token(cookie: str | None) str | None

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

linkahead.connection.utils.parse_url(url: str)
linkahead.connection.utils.quote(string: str) str
linkahead.connection.utils.urlencode(query: dict[str, str | None]) str

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.

querydict

A dict of str or unicode keys with None, unicode or str values.

str

A series of key=value pairs separated by &.

AttributeError

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