Skip to content

Commit

Permalink
Fix/SK-1127 | Add session_id flag to CLI resources (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
KatHellg authored Nov 20, 2024
1 parent 01d01c6 commit 63c56f9
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 36 deletions.
36 changes: 33 additions & 3 deletions fedn/cli/client_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,42 @@ def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, n_
if _token:
headers["Authorization"] = _token

click.echo(f"\nListing clients: {url}\n")
click.echo(f"Headers: {headers}")

try:
response = requests.get(url, headers=headers)
print_response(response, "clients")
print_response(response, "clients", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")

@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("-t", "--token", required=False, help="Authentication token")
@click.option("-id", "--id", required=True, help="Client ID")
@client_cmd.command("get")
@click.pass_context
def get_client(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None):
"""Return:
------
- result: client with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="clients")
headers = {}


_token = get_token(token)

if _token:
headers["Authorization"] = _token

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


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}")

Expand Down
37 changes: 34 additions & 3 deletions fedn/cli/combiner_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,42 @@ def list_combiners(ctx, protocol: str, host: str, port: str, token: str = None,
if _token:
headers["Authorization"] = _token

click.echo(f"\nListing combiners: {url}\n")
click.echo(f"Headers: {headers}")

try:
response = requests.get(url, headers=headers)
print_response(response, "combiners")
print_response(response, "combiners", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")


@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("-t", "--token", required=False, help="Authentication token")
@click.option("-id", "--id", required=True, help="Combiner ID")
@combiner_cmd.command("get")
@click.pass_context
def get_combiner(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None):
"""Return:
------
- result: combiner with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="combiners")
headers = {}


_token = get_token(token)

if _token:
headers["Authorization"] = _token

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


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}")
47 changes: 43 additions & 4 deletions fedn/cli/model_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ 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("--n_max", required=False, help="Number of items to list")
@model_cmd.command("list")
@click.pass_context
def list_models(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None):
def list_models(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None):
"""Return:
------
- count: number of models
- result: list of models
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="models")


headers = {}

if n_max:
Expand All @@ -38,11 +41,47 @@ def list_models(ctx, protocol: str, host: str, port: str, token: str = None, n_m
if _token:
headers["Authorization"] = _token

click.echo(f"\nListing models: {url}\n")
click.echo(f"Headers: {headers}")
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}")


@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("-t", "--token", required=False, help="Authentication token")
@click.option("-id", "--id", required=True, help="Model ID")
@model_cmd.command("get")
@click.pass_context
def get_model(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None):
"""Return:
------
- result: model with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="models")


headers = {}


_token = get_token(token)

if _token:
headers["Authorization"] = _token

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


try:
response = requests.get(url, headers=headers)
print_response(response, "models")
print_response(response, "model", id)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")
37 changes: 34 additions & 3 deletions fedn/cli/package_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,42 @@ def list_packages(ctx, protocol: str, host: str, port: str, token: str = None, n
if _token:
headers["Authorization"] = _token

click.echo(f"\nListing packages: {url}\n")
click.echo(f"Headers: {headers}")

try:
response = requests.get(url, headers=headers)
print_response(response, "packages")
print_response(response, "packages", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")


@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("-t", "--token", required=False, help="Authentication token")
@click.option("-id", "--id", required=True, help="Package ID")
@package_cmd.command("get")
@click.pass_context
def get_package(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None):
"""Return:
------
- result: package with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="packages")
headers = {}


_token = get_token(token)

if _token:
headers["Authorization"] = _token

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


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}")
47 changes: 43 additions & 4 deletions fedn/cli/round_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ 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("-t", "--token", required=False, help="Authentication token")
@click.option("--n_max", required=False, help="Number of items to list")
@round_cmd.command("list")
@click.pass_context
def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None):
def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None):
"""Return:
------
- count: number of rounds
- result: list of rounds
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="rounds")

headers = {}

if n_max:
Expand All @@ -38,11 +40,48 @@ def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, n_m
if _token:
headers["Authorization"] = _token

click.echo(f"\nListing rounds: {url}\n")
click.echo(f"Headers: {headers}")
if session_id:
url = f"{url}?round_config.session_id={session_id}"


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

except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")


@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("-id", "--id", required=True, help="Round ID")
@click.option("-t", "--token", required=False, help="Authentication token")
@round_cmd.command("get")
@click.pass_context
def get_round(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None):
"""Return:
------
- result: round with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="rounds")

headers = {}


_token = get_token(token)

if _token:
headers["Authorization"] = _token

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


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}")
36 changes: 33 additions & 3 deletions fedn/cli/session_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,41 @@ def list_sessions(ctx, protocol: str, host: str, port: str, token: str = None, n
if _token:
headers["Authorization"] = _token

click.echo(f"\nListing sessions: {url}\n")
click.echo(f"Headers: {headers}")

try:
response = requests.get(url, headers=headers)
print_response(response, "sessions")
print_response(response, "sessions", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")


@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("-t", "--token", required=False, help="Authentication token")
@click.option("-id", "--id", required=True, help="Session ID")
@session_cmd.command("get")
@click.pass_context
def get_session(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None):
"""Return:
------
- result: session with given session id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="sessions")
headers = {}

_token = get_token(token)

if _token:
headers["Authorization"] = _token

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


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}")
26 changes: 18 additions & 8 deletions fedn/cli/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,36 @@ def get_client_package_dir(path: str) -> str:


# Print response from api (list of entities)
def print_response(response, entity_name: str):
def print_response(response, entity_name: str, so):
"""Prints the api response to the cli.
:param response:
type: array
description: list of entities
:param entity_name:
type: string
description: name of entity
:param so:
type: boolean
desriptions: single output format
return: None
"""
if response.status_code == 200:
json_data = response.json()
count, result = json_data.values()
click.echo(f"Found {count} {entity_name}")
click.echo("\n---------------------------------\n")
for obj in result:
click.echo("{")
for k, v in obj.items():
if so:
click.echo(f"Found {entity_name}")
click.echo("\n---------------------------------\n")
for k, v in json_data.items():
click.echo(f"\t{k}: {v}")
click.echo("}")
else:
count, result = json_data.values()
click.echo(f"Found {count} {entity_name}")
click.echo("\n---------------------------------\n")
for obj in result:
print(obj.get("session_id"))
click.echo("{")
for k, v in obj.items():
click.echo(f"\t{k}: {v}")
click.echo("}")
elif response.status_code == 500:
json_data = response.json()
click.echo(f'Error: {json_data["message"]}')
Expand Down
Loading

0 comments on commit 63c56f9

Please sign in to comment.