fedn.network.api package

API module for the FEDn network. Includes a REST-API server to interact with the controller and statestore.

Submodules

fedn.network.api.client module

class fedn.network.api.client.APIClient(host, port=None, secure=False, verify=False, token=None, auth_scheme=None)[source]

Bases: object

An API client for interacting with the statestore and controller.

Parameters:
  • host (str) – The host of the api server.

  • port (int) – The port of the api server.

  • secure (bool) – Whether to use https.

  • verify (bool) – Whether to verify the server certificate.

download_model(id: str, path: str)[source]

Download the model with id id.

Parameters:
  • id (str) – The id (or model property) of the model to download.

  • path (str) – The path to download the model to.

Returns:

Message with success or failure.

Return type:

dict

download_package(path: str)[source]

Download the compute package.

Parameters:

path (str) – The path to download the compute package to.

Returns:

Message with success or failure.

Return type:

dict

get_active_clients(combiner_id: str | None = None, n_max: int | None = None)[source]

Get active clients from the statestore.

Parameters:
  • combiner_id (str) – The combiner id to get active clients for.

  • n_max (int) – The maximum number of clients to get (If none all will be fetched).

Returns:

Active clients.

Return type:

dict

get_active_model()[source]

Get the latest model from the statestore.

Returns:

The latest model.

Return type:

dict

get_active_package()[source]

Get the (active) compute package from the statestore.

Returns:

Package.

Return type:

dict

get_client(id: str)[source]

Get a client from the statestore.

Parameters:

id (str) – The client id to get.

Returns:

Client.

Return type:

dict

get_client_config(checksum=True)[source]

Get client config from controller. Optionally include the checksum. The config is used for clients to connect to the controller and ask for combiner assignment.

Parameters:

checksum (bool) – Whether to include the checksum of the package.

Returns:

The client configuration.

Return type:

dict

get_clients(n_max: int | None = None)[source]

Get clients from the statestore.

Parameters:

n_max (int) – The maximum number of clients to get (If none all will be fetched).

return: Clients. rtype: dict

get_clients_count()[source]

Get the number of clients in the statestore.

Returns:

The number of clients.

Return type:

dict

get_combiner(id: str)[source]

Get a combiner from the statestore.

Parameters:

id (str) – The combiner id to get.

Returns:

Combiner.

Return type:

dict

get_combiners(n_max: int | None = None)[source]

Get combiners in the network.

Parameters:

n_max (int) – The maximum number of combiners to get (If none all will be fetched).

Returns:

Combiners.

Return type:

dict

get_combiners_count()[source]

Get the number of combiners in the statestore.

Returns:

The number of combiners.

Return type:

dict

get_controller_status()[source]

Get the status of the controller.

Returns:

The status of the controller.

Return type:

dict

get_model(id: str)[source]

Get a model from the statestore.

Parameters:

id (str) – The id (or model property) of the model to get.

Returns:

Model.

Return type:

dict

get_model_trail(id: str | None = None, include_self: bool = True, reverse: bool = True, n_max: int | None = None)[source]

Get the model trail.

Parameters:
  • id (str) – The id (or model property) of the model to start the trail from. (optional)

  • n_max (int) – The maximum number of models to get (If none all will be fetched).

Returns:

Models.

Return type:

dict

get_models(session_id: str | None = None, n_max: int | None = None)[source]

Get models from the statestore.

Parameters:
  • session_id (str) – The session id to get models for. (optional)

  • n_max (int) – The maximum number of models to get (If none all will be fetched).

Returns:

Models.

Return type:

dict

get_models_count()[source]

Get the number of models in the statestore.

Returns:

The number of models.

Return type:

dict

get_package(id: str)[source]

Get a compute package from the statestore.

Parameters:

id (str) – The id of the compute package to get.

Returns:

Package.

Return type:

dict

get_package_checksum()[source]

Get the checksum of the compute package.

Returns:

The checksum.

Return type:

dict

get_packages(n_max: int | None = None)[source]

Get compute packages from the statestore.

Parameters:

n_max (int) – The maximum number of packages to get (If none all will be fetched).

Returns:

Packages.

Return type:

dict

get_packages_count()[source]

Get the number of compute packages in the statestore.

Returns:

The number of packages.

Return type:

dict

get_round(id: str)[source]

Get a round from the statestore.

Parameters:

round_id (str) – The round id to get.

Returns:

Round (config and metrics).

Return type:

dict

get_rounds(n_max: int | None = None)[source]

Get all rounds from the statestore.

Parameters:

n_max (int) – The maximum number of rounds to get (If none all will be fetched).

Returns:

Rounds.

Return type:

dict

get_rounds_count()[source]

Get the number of rounds in the statestore.

Returns:

The number of rounds.

Return type:

dict

get_session(id: str)[source]

Get a session from the statestore.

Parameters:

id (str) – The session id to get.

Returns:

Session.

Return type:

dict

get_session_status(id: str)[source]

Get the status of a session.

Parameters:

id (str) – The id of the session to get.

Returns:

The status of the session.

Return type:

str

get_sessions(n_max: int | None = None)[source]

Get sessions from the statestore.

Parameters:

n_max (int) – The maximum number of sessions to get (If none all will be fetched).

Returns:

Sessions.

Return type:

dict

get_sessions_count()[source]

Get the number of sessions in the statestore.

Returns:

The number of sessions.

Return type:

dict

get_status(id: str)[source]

Get a status object (event) from the statestore.

Parameters:

id (str) – The id of the status to get.

Returns:

Status.

Return type:

dict

get_statuses(session_id: str | None = None, event_type: str | None = None, sender_name: str | None = None, sender_role: str | None = None, n_max: int | None = None)[source]

Get statuses from the statestore. Filter by input parameters

Parameters:
  • session_id (str) – The session id to get statuses for.

  • event_type (str) – The event type to get.

  • sender_name (str) – The sender name to get.

  • sender_role (str) – The sender role to get.

  • n_max (int) – The maximum number of statuses to get (If none all will be fetched).

Returns:

Statuses

get_statuses_count()[source]

Get the number of statuses in the statestore.

Returns:

The number of statuses.

Return type:

dict

get_validation(id: str)[source]

Get a validation from the statestore.

Parameters:

id (str) – The id of the validation to get.

Returns:

Validation.

Return type:

dict

get_validations(session_id: str | None = None, model_id: str | None = None, correlation_id: str | None = None, sender_name: str | None = None, sender_role: str | None = None, receiver_name: str | None = None, receiver_role: str | None = None, n_max: int | None = None)[source]

Get validations from the statestore. Filter by input parameters.

Parameters:
  • session_id (str) – The session id to get validations for.

  • model_id (str) – The model id to get validations for.

  • correlation_id (str) – The correlation id to get validations for.

  • sender_name (str) – The sender name to get validations for.

  • sender_role (str) – The sender role to get validations for.

  • receiver_name (str) – The receiver name to get validations for.

  • receiver_role (str) – The receiver role to get validations for.

  • n_max (int) – The maximum number of validations to get (If none all will be fetched).

Returns:

Validations.

Return type:

dict

get_validations_count()[source]

Get the number of validations in the statestore.

Returns:

The number of validations.

Return type:

dict

session_is_finished(id: str)[source]

Check if a session with id has finished.

Parameters:

id (str) – The id of the session to get.

Returns:

True if session is finished, otherwise false.

Return type:

bool

set_active_model(path)[source]

Set the initial model in the statestore and upload to model repository.

Parameters:

path (str) – The file path of the initial model to set.

Returns:

A dict with success or failure message.

Return type:

dict

set_active_package(path: str, helper: str, name: str | None = None, description: str | None = None)[source]

Set the compute package in the statestore.

Parameters:
  • path (str) – The file path of the compute package to set.

  • helper (str) – The helper type to use.

Returns:

A dict with success or failure message.

Return type:

dict

start_session(id: str | None = None, aggregator: str = 'fedavg', aggregator_kwargs: dict | None = None, model_id: str | None = None, round_timeout: int = 180, rounds: int = 5, round_buffer_size: int = -1, delete_models: bool = True, validate: bool = True, helper: str = '', min_clients: int = 1, requested_clients: int = 8, server_functions: ServerFunctionsBase | None = None)[source]

Start a new session.

Parameters:
  • id (str) – The session id to start.

  • aggregator (str) – The aggregator plugin to use.

  • model_id (str) – The id of the initial model.

  • round_timeout (int) – The round timeout to use in seconds.

  • rounds (int) – The number of rounds to perform.

  • round_buffer_size (int) – The round buffer size to use.

  • delete_models (bool) – Whether to delete models after each round at combiner (save storage).

  • validate (bool) – Whether to validate the model after each round.

  • helper (str) – The helper type to use.

  • min_clients (int) – The minimum number of clients required.

  • requested_clients (int) – The requested number of clients.

Returns:

A dict with success or failure message and session config.

Return type:

dict

fedn.network.api.interface module

fedn.network.api.network module

fedn.network.api.tests module

class fedn.network.api.tests.NetworkAPITests(methodName='runTest')[source]

Bases: TestCase

Unittests for the Network API.

setUp(mock_mongo, mock_control)[source]

Hook method for setting up the test fixture before exercising it.

test_add_client()[source]

Test add_client endpoint.

test_add_combiner()[source]

Test add_combiner endpoint.

test_get_active_clients()[source]

Test get_active_clients endpoint.

test_get_client_config()[source]

Test get_client_config endpoint.

test_get_combiner()[source]

Test get_combiner endpoint.

test_get_controller_status()[source]

Test get_controller_status endpoint.

test_get_events()[source]

Test get_events endpoint.

test_get_initial_model()[source]

Test get_initial_model endpoint.

test_get_latest_model()[source]

Test get_latest_model endpoint.

test_get_model_trail()[source]

Test get_model_trail endpoint.

test_get_package()[source]

Test get_package endpoint.

test_get_round()[source]

Test get_round endpoint.

test_get_status()[source]

Test get_status endpoint.

test_list_clients()[source]

Test list_clients endpoint.

test_list_combiners()[source]

Test list_combiners endpoint.

test_list_compute_packages()[source]

Test list_compute_packages endpoint.

test_list_models()[source]

Test list_models endpoint.

test_list_rounds()[source]

Test list_rounds endpoint.

test_list_sessions()[source]

Test list_sessions endpoint.

test_set_initial_model()[source]

Test set_initial_model endpoint.

test_start_session()[source]

Test start_session endpoint.