Skip to content

Commit

Permalink
Changed name ClientAPI -> FednClient
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminastrand committed Nov 6, 2024
1 parent d3b6998 commit 9b0e82a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
1 change: 1 addition & 0 deletions fedn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions fedn/network/clients/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
26 changes: 13 additions & 13 deletions fedn/network/clients/client_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b0e82a

Please sign in to comment.