Skip to content

roboherd.util

parse_connection_string(connection_string)

Parse a connection string into a dictionary of connection parameters.

>>> parse_connection_string("ws://user:pass@host/ws")
{'host': 'host',
    'port': 80,
    'username': 'user',
    'password': 'pass',
    'websocket_path': '/ws'}

>>> parse_connection_string("wss://user:pass@host/ws")
{'host': 'host',
    'port': 443,
    'username': 'user',
    'password': 'pass',
    'websocket_path': '/ws'}
Source code in roboherd/util.py
def parse_connection_string(connection_string: str) -> dict:
    """
    Parse a connection string into a dictionary of connection parameters.

    ```pycon
    >>> parse_connection_string("ws://user:pass@host/ws")
    {'host': 'host',
        'port': 80,
        'username': 'user',
        'password': 'pass',
        'websocket_path': '/ws'}

    >>> parse_connection_string("wss://user:pass@host/ws")
    {'host': 'host',
        'port': 443,
        'username': 'user',
        'password': 'pass',
        'websocket_path': '/ws'}

    ```
    """

    parsed = urlparse(connection_string)

    default_port = 80 if parsed.scheme == "ws" else 443

    return {
        "host": parsed.hostname,
        "port": parsed.port or default_port,
        "username": parsed.username,
        "password": parsed.password,
        "websocket_path": parsed.path,
    }