1 3
import platform
2

3 3
import mopidy
4

5
"Helpers for configuring HTTP clients used in Mopidy extensions."
6

7

8 3
def format_proxy(proxy_config, auth=True):
9
    """Convert a Mopidy proxy config to the commonly used proxy string format.
10

11
    Outputs ``scheme://host:port``, ``scheme://user:pass@host:port`` or
12
    :class:`None` depending on the proxy config provided.
13

14
    You can also opt out of getting the basic auth by setting ``auth`` to
15
    :class:`False`.
16

17
    .. versionadded:: 1.1
18
    """
19 3
    if not proxy_config.get("hostname"):
20 3
        return None
21

22 3
    scheme = proxy_config.get("scheme") or "http"
23 3
    username = proxy_config.get("username")
24 3
    password = proxy_config.get("password")
25 3
    hostname = proxy_config["hostname"]
26 3
    port = proxy_config.get("port")
27 3
    if not port or port < 0:
28 3
        port = 80
29

30 3
    if username and password and auth:
31 3
        return f"{scheme}://{username}:{password}@{hostname}:{port}"
32
    else:
33 3
        return f"{scheme}://{hostname}:{port}"
34

35

36 3
def format_user_agent(name=None):
37
    """Construct a User-Agent suitable for use in client code.
38

39
    This will identify use by the provided ``name`` (which should be on the
40
    format ``dist_name/version``), Mopidy version and Python version.
41

42
    .. versionadded:: 1.1
43
    """
44 3
    parts = [
45
        f"Mopidy/{mopidy.__version__}",
46
        f"{platform.python_implementation()}/{platform.python_version()}",
47
    ]
48 3
    if name:
49 3
        parts.insert(0, name)
50 3
    return " ".join(parts)

Read our documentation on viewing source code .

Loading