From 9b0e82a3b056218c01754ec74900244dc42beaae Mon Sep 17 00:00:00 2001 From: benjaminastrand Date: Wed, 6 Nov 2024 17:31:53 +0100 Subject: [PATCH] Changed name ClientAPI -> FednClient --- fedn/__init__.py | 1 + fedn/network/clients/README.rst | 14 +++++----- fedn/network/clients/client_v2.py | 26 +++++++++---------- .../clients/{client_api.py => fedn_client.py} | 2 +- 4 files changed, 22 insertions(+), 21 deletions(-) rename fedn/network/clients/{client_api.py => fedn_client.py} (99%) diff --git a/fedn/__init__.py b/fedn/__init__.py index 703eab7b2..122c2e2bb 100644 --- a/fedn/__init__.py +++ b/fedn/__init__.py @@ -3,6 +3,7 @@ from os.path import basename, dirname, isfile from fedn.network.api.client import APIClient +from fedn.network.clients.fedn_client import FednClient # flake8: noqa diff --git a/fedn/network/clients/README.rst b/fedn/network/clients/README.rst index 5dbc63ff0..af5e2ac97 100644 --- a/fedn/network/clients/README.rst +++ b/fedn/network/clients/README.rst @@ -26,7 +26,7 @@ Step-by-Step Instructions import argparse - from fedn.network.clients.client_api import ClientAPI, ConnectToApiResult + from fedn.network.clients.fedn_client import FednClient, ConnectToApiResult def get_api_url(api_url: str, api_port: int): @@ -70,15 +70,15 @@ Step-by-Step Instructions def main(api_url: str, api_port: int, token: str = None): - client_api = ClientAPI(train_callback=on_train, validate_callback=on_validate, predict_callback=on_predict) + fedn_client = FednClient(train_callback=on_train, validate_callback=on_validate, predict_callback=on_predict) url = get_api_url(api_url, api_port) name = input("Enter Client Name: ") - client_api.set_name(name) + fedn_client.set_name(name) client_id = str(uuid.uuid4()) - client_api.set_client_id(client_id) + fedn_client.set_client_id(client_id) controller_config = { "name": name, @@ -87,18 +87,18 @@ Step-by-Step Instructions "preferred_combiner": "", } - result, combiner_config = client_api.connect_to_api(url, token, controller_config) + result, combiner_config = fedn_client.connect_to_api(url, token, controller_config) if result != ConnectToApiResult.Assigned: print("Failed to connect to API, exiting.") return - result: bool = client_api.init_grpchandler(config=combiner_config, client_name=client_id, token=token) + result: bool = fedn_client.init_grpchandler(config=combiner_config, client_name=client_id, token=token) if not result: return - client_api.run() + fedn_client.run() if __name__ == "__main__": parser = argparse.ArgumentParser(description="Client Example") diff --git a/fedn/network/clients/client_v2.py b/fedn/network/clients/client_v2.py index fa1bf8e44..6d1f52fb4 100644 --- a/fedn/network/clients/client_v2.py +++ b/fedn/network/clients/client_v2.py @@ -8,7 +8,7 @@ from fedn.common.config import FEDN_CUSTOM_URL_PREFIX from fedn.common.log_config import logger -from fedn.network.clients.client_api import ClientAPI, ConnectToApiResult, GrpcConnectionOptions +from fedn.network.clients.fedn_client import ConnectToApiResult, FednClient, GrpcConnectionOptions from fedn.network.combiner.modelservice import get_tmp_path from fedn.utils.helpers.helpers import get_helper @@ -65,7 +65,7 @@ def __init__( self.fedn_api_url = get_url(self.api_url, self.api_port) - self.client_api: ClientAPI = ClientAPI() + self.fedn_client: FednClient = FednClient() self.helper = None @@ -76,7 +76,7 @@ def _connect_to_api(self) -> Tuple[bool, dict]: if result == ConnectToApiResult.ComputePackageMissing: logger.info("Retrying in 3 seconds") time.sleep(3) - result, response = self.client_api.connect_to_api(self.fedn_api_url, self.token, self.client_obj.to_json()) + result, response = self.fedn_client.connect_to_api(self.fedn_api_url, self.token, self.client_obj.to_json()) if result == ConnectToApiResult.Assigned: return True, response @@ -95,32 +95,32 @@ def start(self): return if self.client_obj.package == "remote": - result = self.client_api.init_remote_compute_package(url=self.fedn_api_url, token=self.token, package_checksum=self.package_checksum) + result = self.fedn_client.init_remote_compute_package(url=self.fedn_api_url, token=self.token, package_checksum=self.package_checksum) if not result: return else: - result = self.client_api.init_local_compute_package() + result = self.fedn_client.init_local_compute_package() if not result: return self.set_helper(combiner_config) - result: bool = self.client_api.init_grpchandler(config=combiner_config, client_name=self.client_obj.client_id, token=self.token) + result: bool = self.fedn_client.init_grpchandler(config=combiner_config, client_name=self.client_obj.client_id, token=self.token) if not result: return logger.info("-----------------------------") - self.client_api.set_train_callback(self.on_train) - self.client_api.set_validate_callback(self.on_validation) + self.fedn_client.set_train_callback(self.on_train) + self.fedn_client.set_validate_callback(self.on_validation) - self.client_api.set_name(self.client_obj.name) - self.client_api.set_client_id(self.client_obj.client_id) + self.fedn_client.set_name(self.client_obj.name) + self.fedn_client.set_client_id(self.client_obj.client_id) - self.client_api.run() + self.fedn_client.run() def set_helper(self, response: GrpcConnectionOptions = None): helper_type = response.get("helper_type", None) @@ -160,7 +160,7 @@ def _process_training_request(self, in_model: BytesIO) -> Tuple[BytesIO, dict]: tic = time.time() - self.client_api.dispatcher.run_cmd("train {} {}".format(inpath, outpath)) + self.fedn_client.dispatcher.run_cmd("train {} {}".format(inpath, outpath)) meta["exec_training"] = time.time() - tic @@ -202,7 +202,7 @@ def _process_validation_request(self, in_model: BytesIO) -> dict: fh.write(in_model.getbuffer()) outpath = get_tmp_path() - self.client_api.dispatcher.run_cmd(f"validate {inpath} {outpath}") + self.fedn_client.dispatcher.run_cmd(f"validate {inpath} {outpath}") with open(outpath, "r") as fh: metrics = json.loads(fh.read()) diff --git a/fedn/network/clients/client_api.py b/fedn/network/clients/fedn_client.py similarity index 99% rename from fedn/network/clients/client_api.py rename to fedn/network/clients/fedn_client.py index 36294ab06..aab3df10e 100644 --- a/fedn/network/clients/client_api.py +++ b/fedn/network/clients/fedn_client.py @@ -53,7 +53,7 @@ def get_compute_package_dir_path(): return result -class ClientAPI: +class FednClient: def __init__(self, train_callback: callable = None, validate_callback: callable = None, predict_callback: callable = None): self.train_callback: callable = train_callback self.validate_callback: callable = validate_callback