From 794a0b136241bdd48afaa65ab52caefff3567c9b Mon Sep 17 00:00:00 2001 From: KatHellg Date: Wed, 13 Nov 2024 14:52:39 +0100 Subject: [PATCH] fixed acc to feedback --- fedn/cli/client_cmd.py | 43 +++++++++++++++++++----- fedn/cli/combiner_cmd.py | 42 +++++++++++++++++++---- fedn/cli/model_cmd.py | 68 +++++++++++++++++++++++--------------- fedn/cli/package_cmd.py | 42 +++++++++++++++++++---- fedn/cli/round_cmd.py | 68 +++++++++++++++++++++++--------------- fedn/cli/session_cmd.py | 43 +++++++++++++++++++----- fedn/cli/shared.py | 3 +- fedn/cli/status_cmd.py | 66 +++++++++++++++++++++--------------- fedn/cli/validation_cmd.py | 62 ++++++++++++++++++++-------------- 9 files changed, 303 insertions(+), 134 deletions(-) diff --git a/fedn/cli/client_cmd.py b/fedn/cli/client_cmd.py index d24c39e0e..00f903926 100644 --- a/fedn/cli/client_cmd.py +++ b/fedn/cli/client_cmd.py @@ -39,11 +39,10 @@ def client_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("-id", "--id", required=False, help="Client ID") @click.option("--n_max", required=False, help="Number of items to list") @client_cmd.command("list") @click.pass_context -def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None, n_max: int = None): +def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None): """Return: ------ - count: number of clients @@ -58,6 +57,38 @@ def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, id _token = get_token(token) + 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", 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=False, 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: + ------ + - count: number of clients + - result: list of clients + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="clients") + headers = {} + + _token = get_token(token) + if _token: headers["Authorization"] = _token @@ -66,18 +97,14 @@ def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, id headers["id"] = id - click.echo(f"\nListing clients: {url}\n") + click.echo(f"\nRetrieving client: {url}\n") click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - if id: - print_response(response, "client", True) - else: - print_response(response, "clients", False) + print_response(response, "client", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") - @client_cmd.command("start") @click.option("-d", "--discoverhost", required=False, help="Hostname for discovery services(reducer).") @click.option("-p", "--discoverport", required=False, help="Port for discovery services (reducer).") diff --git a/fedn/cli/combiner_cmd.py b/fedn/cli/combiner_cmd.py index d04385231..f7e937261 100644 --- a/fedn/cli/combiner_cmd.py +++ b/fedn/cli/combiner_cmd.py @@ -67,11 +67,10 @@ def start_cmd(ctx, discoverhost, discoverport, token, name, host, port, fqdn, se @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=False, help="Combiner ID") @click.option("--n_max", required=False, help="Number of items to list") @combiner_cmd.command("list") @click.pass_context -def list_combiners(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None, n_max: int = None): +def list_combiners(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None): """Return: ------ - count: number of combiners @@ -84,6 +83,38 @@ def list_combiners(ctx, protocol: str, host: str, port: str, token: str = None, if n_max: headers["X-Limit"] = n_max + _token = get_token(token) + + 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", 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=False, 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: @@ -94,13 +125,10 @@ def list_combiners(ctx, protocol: str, host: str, port: str, token: str = None, headers["id"] = id - click.echo(f"\nListing combiners: {url}\n") + click.echo(f"\nRetrieving combiner: {url}\n") click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - if id: - print_response(response, "combiner", True) - else: - print_response(response, "combiners", False) + print_response(response, "combiner", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/model_cmd.py b/fedn/cli/model_cmd.py index c3b2d587b..ebf6e6dca 100644 --- a/fedn/cli/model_cmd.py +++ b/fedn/cli/model_cmd.py @@ -17,12 +17,11 @@ 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("-id", "--id", required=False, help="Model ID") @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, id: str = None, session_id: 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 @@ -30,11 +29,50 @@ def list_models(ctx, protocol: str, host: str, port: str, token: str = None, id: """ 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) + + if _token: + headers["Authorization"] = _token + + if session_id: + url = f"{url}?session_id={session_id}" + headers["session_id"] = session_id + + click.echo(f"\nListing models: {url}\n") + click.echo(f"Headers: {headers}") + 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=False, 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: @@ -44,32 +82,10 @@ def list_models(ctx, protocol: str, host: str, port: str, token: str = None, id: url = f"{url}{id}" headers["id"] = id - - click.echo(f"\nListing models: {url}\n") + click.echo(f"\nRetrieving model: {url}\n") click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - if session_id: - if response.status_code == 200: - json_data = response.json() - count, result = json_data.values() - click.echo(f"Found {count} models") - click.echo("\n---------------------------------\n") - for obj in result: - if obj.get("session_id")==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"]}') - else: - click.echo(f"Error: {response.status_code}") - elif id: - print_response(response, "model", True, session_id) - else: - print_response(response, "models", False, session_id) + print_response(response, "model", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/package_cmd.py b/fedn/cli/package_cmd.py index 7dab52231..30998b828 100644 --- a/fedn/cli/package_cmd.py +++ b/fedn/cli/package_cmd.py @@ -45,11 +45,10 @@ def create_cmd(ctx, path, name): @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=False, help="Package ID") @click.option("--n_max", required=False, help="Number of items to list") @package_cmd.command("list") @click.pass_context -def list_packages(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None, n_max: int = None): +def list_packages(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None): """Return: ------ - count: number of packages @@ -62,6 +61,38 @@ def list_packages(ctx, protocol: str, host: str, port: str, token: str = None, i if n_max: headers["X-Limit"] = n_max + _token = get_token(token) + + 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", 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=False, 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: @@ -72,13 +103,10 @@ def list_packages(ctx, protocol: str, host: str, port: str, token: str = None, i headers["id"] = id - click.echo(f"\nListing packages: {url}\n") + click.echo(f"\nretrieving package: {url}\n") click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - if id: - print_response(response, "package", True) - else: - print_response(response, "packages", False) + print_response(response, "package", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/round_cmd.py b/fedn/cli/round_cmd.py index 68364202c..d60ff66e4 100644 --- a/fedn/cli/round_cmd.py +++ b/fedn/cli/round_cmd.py @@ -16,13 +16,12 @@ 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("-id", "--id", required=False, help="Round ID") @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, id: str = None, session_id: 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 @@ -30,11 +29,49 @@ def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, id: """ 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) + + if _token: + headers["Authorization"] = _token + + if session_id: + url = f"{url}?round_config.session_id={session_id}" + headers["session_id"] = session_id + + click.echo(f"\nListing rounds: {url}\n") + click.echo(f"Headers: {headers}") + 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}") + + +@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=False, 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: @@ -44,33 +81,12 @@ def list_rounds(ctx, protocol: str, host: str, port: str, token: str = None, id: url = f"{url}{id}" headers["id"] = id - click.echo(f"\nListing rounds: {url}\n") + + click.echo(f"\nRetrieving round: {url}\n") click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - if session_id: - if response.status_code == 200: - json_data = response.json() - count, result = json_data.values() - click.echo(f"Found {count} rounds") - click.echo("\n---------------------------------\n") - for obj in result: - if obj.get("round_config").get("session_id")==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"]}') - else: - click.echo(f"Error: {response.status_code}") - elif id: - print_response(response, "round", True, session_id) - else: - print_response(response, "rounds", False, session_id) - + print_response(response, "round", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}") diff --git a/fedn/cli/session_cmd.py b/fedn/cli/session_cmd.py index 28f630880..56184ef2e 100644 --- a/fedn/cli/session_cmd.py +++ b/fedn/cli/session_cmd.py @@ -17,11 +17,10 @@ def session_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("-id", "--id", required=False, help="Session ID") @click.option("--n_max", required=False, help="Number of items to list") @session_cmd.command("list") @click.pass_context -def list_sessions(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None, n_max: int = None): +def list_sessions(ctx, protocol: str, host: str, port: str, token: str = None, n_max: int = None): """Return: ------ - count: number of sessions @@ -36,6 +35,37 @@ def list_sessions(ctx, protocol: str, host: str, port: str, token: str = None, i _token = get_token(token) + 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", 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 @@ -44,13 +74,10 @@ def list_sessions(ctx, protocol: str, host: str, port: str, token: str = None, i headers["id"] = id - click.echo(f"\nListing sessions: {url}\n") + click.echo(f"\nRetrieving session: {url}\n") click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - if id: - print_response(response, "session", True) - else: - print_response(response, "sessions", False) + print_response(response, "session", id) except requests.exceptions.ConnectionError: - click.echo(f"Error: Could not connect to {url}") + click.echo(f"Error: Could not connect to {url}") \ No newline at end of file diff --git a/fedn/cli/shared.py b/fedn/cli/shared.py index 4fb05590d..5876828fb 100644 --- a/fedn/cli/shared.py +++ b/fedn/cli/shared.py @@ -64,7 +64,7 @@ def get_client_package_dir(path: str) -> str: # Print response from api (list of entities) -def print_response(response, entity_name: str, so, session_id): +def print_response(response, entity_name: str, so): """Prints the api response to the cli. :param response: type: array @@ -89,6 +89,7 @@ def print_response(response, entity_name: str, so, session_id): 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}") diff --git a/fedn/cli/status_cmd.py b/fedn/cli/status_cmd.py index 01f7c497a..ea064a857 100644 --- a/fedn/cli/status_cmd.py +++ b/fedn/cli/status_cmd.py @@ -16,12 +16,11 @@ def status_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("-id", "--id", required=False, help="Status ID") @click.option("-session_id", "--session_id", required=False, help="statuses with given session id") @click.option("--n_max", required=False, help="Number of items to list") @status_cmd.command("list") @click.pass_context -def list_statuses(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None, session_id: str = None, n_max: int = None): +def list_statuses(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None): """Return: ------ - count: number of statuses @@ -34,6 +33,42 @@ def list_statuses(ctx, protocol: str, host: str, port: str, token: str = None, i if n_max: headers["X-Limit"] = n_max + _token = get_token(token) + + if _token: + headers["Authorization"] = _token + + if session_id: + url = f"{url}?sessionId={session_id}" + headers["session_id"] = session_id + + + click.echo(f"\nListing statuses: {url}\n") + click.echo(f"Headers: {headers}") + try: + response = requests.get(url, headers=headers) + print_response(response, "statuses", 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=False, help="Status ID") +@status_cmd.command("get") +@click.pass_context +def get_status(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: status with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="statuses") + headers = {} + + _token = get_token(token) if _token: @@ -44,31 +79,10 @@ def list_statuses(ctx, protocol: str, host: str, port: str, token: str = None, i headers["id"] = id - click.echo(f"\nListing statuses: {url}\n") + click.echo(f"\nRetrieving status: {url}\n") click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - if session_id: - if response.status_code == 200: - json_data = response.json() - count, result = json_data.values() - click.echo(f"Found {count} statuses") - click.echo("\n---------------------------------\n") - for obj in result: - if obj.get("session_id")==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"]}') - else: - click.echo(f"Error: {response.status_code}") - elif id: - print_response(response, "status", True) - else: - print_response(response, "statuses", False) + print_response(response, "status", id) except requests.exceptions.ConnectionError: - click.echo(f"Error: Could not connect to {url}") + click.echo(f"Error: Could not connect to {url}") \ No newline at end of file diff --git a/fedn/cli/validation_cmd.py b/fedn/cli/validation_cmd.py index 1dc68c311..10c594398 100644 --- a/fedn/cli/validation_cmd.py +++ b/fedn/cli/validation_cmd.py @@ -17,12 +17,11 @@ def validation_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("-id", "--id", required=False, help="validation ID") @click.option("-session_id", "--session_id", required=False, help="validations in session with given session id") @click.option("--n_max", required=False, help="Number of items to list") @validation_cmd.command("list") @click.pass_context -def list_validations(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None, session_id: str = None, n_max: int = None): +def list_validations(ctx, protocol: str, host: str, port: str, token: str = None, session_id: str = None, n_max: int = None): """Return: ------ - count: number of validations @@ -37,6 +36,40 @@ def list_validations(ctx, protocol: str, host: str, port: str, token: str = None _token = get_token(token) + if _token: + headers["Authorization"] = _token + + if session_id: + url = f"{url}?sessionId={session_id}" + headers["session_id"] = session_id + + click.echo(f"\nListing validations: {url}\n") + click.echo(f"Headers: {headers}") + try: + response = requests.get(url, headers=headers) + print_response(response, "validations", 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=False, help="validation ID") +@validation_cmd.command("get") +@click.pass_context +def get_validation(ctx, protocol: str, host: str, port: str, token: str = None, id: str = None): + """Return: + ------ + - result: validation with given id + + """ + url = get_api_url(protocol=protocol, host=host, port=port, endpoint="validations") + headers = {} + + _token = get_token(token) + if _token: headers["Authorization"] = _token @@ -45,31 +78,10 @@ def list_validations(ctx, protocol: str, host: str, port: str, token: str = None headers["id"] = id - click.echo(f"\nListing validations: {url}\n") + click.echo(f"\nRetrieving validation: {url}\n") click.echo(f"Headers: {headers}") try: response = requests.get(url, headers=headers) - if session_id: - if response.status_code == 200: - json_data = response.json() - count, result = json_data.values() - click.echo(f"Found {count} statuses") - click.echo("\n---------------------------------\n") - for obj in result: - if obj.get("session_id")==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"]}') - else: - click.echo(f"Error: {response.status_code}") - elif id: - print_response(response, "validation", True) - else: - print_response(response, "validations", False) + print_response(response, "validation", id) except requests.exceptions.ConnectionError: click.echo(f"Error: Could not connect to {url}")