Skip to content

declearn.communication.utils.build_client

Set up and return a NetworkClient communication endpoint.

Parameters:

Name Type Description Default
protocol str

Name of the communications protocol backend, based on which the Client subclass to instantiate will be retrieved.

required
server_uri str

Public uri of the server to which this client is to connect.

required
name str

Name of this client, reported to the server for logging and messages' addressing purposes.

required
certificate Optional[str]

Path to a certificate (publickey) PEM file, to use SSL/TLS communcations encryption.

None
logger Union[logging.Logger, str, None]

Logger to use, or name of a logger to set up using declearn.utils.get_logger. If None, use type(client)-name.

None
**kwargs Any

Any valid additional keyword parameter may be passed as well. Refer to the target NetworkClient subclass for details.

{}

Returns:

Name Type Description
client NetworkClient

NetworkClient communication endpoint instance.

Source code in declearn/communication/utils/_build.py
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def build_client(
    protocol: str,
    server_uri: str,
    name: str,
    certificate: Optional[str] = None,
    logger: Union[logging.Logger, str, None] = None,
    **kwargs: Any,
) -> NetworkClient:
    """Set up and return a NetworkClient communication endpoint.

    Parameters
    ----------
    protocol: str
        Name of the communications protocol backend, based on which
        the Client subclass to instantiate will be retrieved.
    server_uri: str
        Public uri of the server to which this client is to connect.
    name: str
        Name of this client, reported to the server for logging and
        messages' addressing purposes.
    certificate: str or None, default=None,
        Path to a certificate (publickey) PEM file, to use SSL/TLS
        communcations encryption.
    logger: logging.Logger or str or None, default=None,
        Logger to use, or name of a logger to set up using
        `declearn.utils.get_logger`. If None, use `type(client)-name`.
    **kwargs:
        Any valid additional keyword parameter may be passed as well.
        Refer to the target `NetworkClient` subclass for details.

    Returns
    -------
    client: NetworkClient
        NetworkClient communication endpoint instance.
    """
    protocol = protocol.strip().lower()
    try:
        cls = access_registered(name=protocol, group="NetworkClient")
    except KeyError as exc:  # pragma: no cover
        raise_if_installable(protocol, exc)
        raise KeyError(
            "Failed to retrieve NetworkClient "
            f"class for protocol '{protocol}'."
        ) from exc
    assert issubclass(cls, NetworkClient)  # guaranteed by TypesRegistry
    return cls(
        server_uri=server_uri,
        name=name,
        certificate=certificate,
        logger=logger,
        **kwargs,
    )