Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KatHellg committed Jan 12, 2025
1 parent a430156 commit f236c89
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 237 deletions.
32 changes: 5 additions & 27 deletions fedn/cli/client_cmd.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import uuid

import click
import requests

from fedn.cli.main import main
from fedn.cli.shared import CONTROLLER_DEFAULTS, apply_config, get_api_url, get_token, print_response
from fedn.cli.shared import CONTROLLER_DEFAULTS, apply_config, get_response, print_response
from fedn.common.exceptions import InvalidClientConfig
from fedn.network.clients.client import Client
from fedn.network.clients.client_v2 import Client as ClientV2
Expand Down Expand Up @@ -49,22 +48,13 @@ def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, n_
- result: list of clients
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="clients")
headers = {}

if n_max:
headers["X-Limit"] = n_max

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "clients", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint="clients", token=token, headers=headers, usr_api=False, usr_token=False)
print_response(response, "clients", None)


@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
Expand All @@ -80,20 +70,8 @@ def get_client(ctx, protocol: str, host: str, port: str, token: str = None, id:
- result: client with given id
"""
_url = get_api_url(protocol=protocol, host=host, port=port, endpoint="clients")
url = f"{_url}{id}"
headers = {}

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "client", id)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint=f"clients/{id}", token=token, headers={}, usr_api=False, usr_token=False)
print_response(response, "client", id)


@client_cmd.command("start-v1")
Expand Down
32 changes: 5 additions & 27 deletions fedn/cli/combiner_cmd.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import uuid

import click
import requests

from .main import main
from .shared import CONTROLLER_DEFAULTS, apply_config, get_api_url, get_token, print_response
from .shared import CONTROLLER_DEFAULTS, apply_config, get_response, print_response


@main.group("combiner")
Expand Down Expand Up @@ -77,22 +76,13 @@ def list_combiners(ctx, protocol: str, host: str, port: str, token: str = None,
- result: list of combiners
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="combiners")
headers = {}

if n_max:
headers["X-Limit"] = n_max

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "combiners", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint="combiners", token=token, headers=headers, usr_api=False, usr_token=False)
print_response(response, "combiners", None)


@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
Expand All @@ -108,17 +98,5 @@ def get_combiner(ctx, protocol: str, host: str, port: str, token: str = None, id
- result: combiner with given id
"""
_url = get_api_url(protocol=protocol, host=host, port=port, endpoint="combiners")
url = f"{_url}{id}"
headers = {}

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "combiner", id)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint=f"combiners/{id}", token=token, headers={}, usr_api=False, usr_token=False)
print_response(response, "combiner", id)
44 changes: 10 additions & 34 deletions fedn/cli/model_cmd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import click
import requests

from .main import main
from .shared import CONTROLLER_DEFAULTS, get_api_url, get_token, print_response
from .shared import CONTROLLER_DEFAULTS, get_response, print_response


@main.group("model")
Expand All @@ -16,7 +15,7 @@ def model_cmd(ctx):
@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)")
@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)")
@click.option("-t", "--token", required=False, help="Authentication token")
@click.option("-session_id", "--session_id", required=False, help="models in session with given session id")
@click.option("-s", "--session_id", required=False, help="models in session with given session id")
@click.option("--n_max", required=False, help="Number of items to list")
@model_cmd.command("list")
@click.pass_context
Expand All @@ -27,26 +26,18 @@ def list_models(ctx, protocol: str, host: str, port: str, token: str = None, ses
- result: list of models
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="models")

headers = {}

if n_max:
headers["X-Limit"] = n_max

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

if session_id:
url = f"{url}?session_id={session_id}"

try:
response = requests.get(url, headers=headers)
print_response(response, "models", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(
protocol=protocol, host=host, port=port, endpoint=f"models/?session_id={session_id}", token=token, headers=headers, usr_api=False, usr_token=False
)
else:
response = get_response(protocol=protocol, host=host, port=port, endpoint="models", token=token, headers=headers, usr_api=False, usr_token=False)
print_response(response, "models", None)


@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
Expand All @@ -62,20 +53,5 @@ def get_model(ctx, protocol: str, host: str, port: str, token: str = None, id: s
- result: model with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="models")

headers = {}

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

if id:
url = f"{url}{id}"

try:
response = requests.get(url, headers=headers)
print_response(response, "model", id)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint=f"models/{id}", token=token, headers={}, usr_api=False, usr_token=False)
print_response(response, "model", id)
32 changes: 5 additions & 27 deletions fedn/cli/package_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import tarfile

import click
import requests

from fedn.common.log_config import logger

from .main import main
from .shared import CONTROLLER_DEFAULTS, get_api_url, get_token, print_response
from .shared import CONTROLLER_DEFAULTS, get_response, print_response


@main.group("package")
Expand Down Expand Up @@ -54,22 +53,13 @@ def list_packages(ctx, protocol: str, host: str, port: str, token: str = None, n
- result: list of packages
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="packages")
headers = {}

if n_max:
headers["X-Limit"] = n_max

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "packages", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint="packages", token=token, headers=headers, usr_api=False, usr_token=False)
print_response(response, "packages", None)


@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
Expand All @@ -85,17 +75,5 @@ def get_package(ctx, protocol: str, host: str, port: str, token: str = None, id:
- result: package with given id
"""
_url = get_api_url(protocol=protocol, host=host, port=port, endpoint="packages")
url = f"{_url}{id}"
headers = {}

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "package", id)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint=f"packages/{id}", token=token, headers={}, usr_api=False, usr_token=False)
print_response(response, "package", id)
59 changes: 26 additions & 33 deletions fedn/cli/round_cmd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import click
import requests

from .main import main
from .shared import CONTROLLER_DEFAULTS, get_api_url, get_token, print_response
from .shared import CONTROLLER_DEFAULTS, get_response, print_response


@main.group("round")
Expand All @@ -15,7 +14,7 @@ def round_cmd(ctx):
@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)")
@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)")
@click.option("-session_id", "--session_id", required=False, help="Rounds in session with given session id")
@click.option("-s", "--session_id", required=False, help="Rounds in session with given session id")
@click.option("-t", "--token", required=False, help="Authentication token")
@click.option("--n_max", required=False, help="Number of items to list")
@round_cmd.command("list")
Expand All @@ -27,27 +26,34 @@ def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, ses
- result: list of rounds
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="rounds")

headers = {}

if n_max:
headers["X-Limit"] = n_max

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

if session_id:
url = f"{url}?round_config.session_id={session_id}"

try:
response = requests.get(url, headers=headers)
print_response(response, "rounds", None)

except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(
protocol=protocol,
host=host,
port=port,
endpoint=f"rounds/?round_config.session_id={session_id}",
token=token,
headers=headers,
usr_api=False,
usr_token=False,
)
else:
response = get_response(
protocol=protocol,
host=host,
port=port,
endpoint="rounds",
token=token,
headers=headers,
usr_api=False,
usr_token=False,
)
print_response(response, "rounds", None)


@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
Expand All @@ -63,18 +69,5 @@ def get_round(ctx, protocol: str, host: str, port: str, token: str = None, id: s
- result: round with given id
"""
_url = get_api_url(protocol=protocol, host=host, port=port, endpoint="rounds")
url = f"{_url}{id}"
headers = {}

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "round", id)

except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint=f"rounds/{id}", token=token, headers={}, usr_api=False, usr_token=False)
print_response(response, "round", id)
32 changes: 5 additions & 27 deletions fedn/cli/session_cmd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import click
import requests

from .main import main
from .shared import CONTROLLER_DEFAULTS, get_api_url, get_token, print_response
from .shared import CONTROLLER_DEFAULTS, get_response, print_response


@main.group("session")
Expand All @@ -26,22 +25,13 @@ def list_sessions(ctx, protocol: str, host: str, port: str, token: str = None, n
- result: list of sessions
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="sessions")
headers = {}

if n_max:
headers["X-Limit"] = n_max

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "sessions", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint="sessions", token=token, headers=headers, usr_api=False, usr_token=False)
print_response(response, "sessions", None)


@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
Expand All @@ -57,17 +47,5 @@ def get_session(ctx, protocol: str, host: str, port: str, token: str = None, id:
- result: session with given session id
"""
_url = get_api_url(protocol=protocol, host=host, port=port, endpoint="sessions")
url = f"{_url}{id}"
headers = {}

_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

try:
response = requests.get(url, headers=headers)
print_response(response, "session", id)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
response = get_response(protocol=protocol, host=host, port=port, endpoint=f"sessions/{id}", token=token, headers={}, usr_api=False, usr_token=False)
print_response(response, "session", id)
Loading

0 comments on commit f236c89

Please sign in to comment.