From 48c1033f129112b547e98447f6cb254f00f4e200 Mon Sep 17 00:00:00 2001 From: crisog Date: Mon, 20 May 2024 17:15:05 -0400 Subject: [PATCH] fix: get internal entrypoints --- pyproject.toml | 2 +- stateless/cli/commands/entrypoints.py | 123 +++++++++++++++++++------- stateless/main.py | 2 +- 3 files changed, 93 insertions(+), 34 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fd7facd..b4756cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "stateless-sdk" -version = "0.0.19" # Keep this in sync with __version__ in stateless/main.py +version = "0.0.20" # Keep this in sync with __version__ in stateless/main.py authors = [{ name = "blockjoe", email = "joe@stateless.solutions" }] description = "A CLI for interacting with the Stateless Gateway" readme = "readme.md" diff --git a/stateless/cli/commands/entrypoints.py b/stateless/cli/commands/entrypoints.py index acd31af..3ac600e 100644 --- a/stateless/cli/commands/entrypoints.py +++ b/stateless/cli/commands/entrypoints.py @@ -40,6 +40,20 @@ def _get_offerings(chain_id: Optional[int] = None, offset=0, limit=10): ) return offerings + @staticmethod + def _get_internal_provider_entrypoints(offset=0, limit=10): + internal_provider_entrypoints = EntrypointsManager.make_paginated_request( + V1Routes.INTERNAL_PROVIDER_ENTRYPOINTS, offset, limit + ) + if not internal_provider_entrypoints: + raise Exit( + secho( + "No internal provider entrypoints found.", + fg="red", + ) + ) + return internal_provider_entrypoints + @staticmethod def _get_regions(): response = make_request_with_api_key("GET", V1Routes.REGIONS) @@ -99,6 +113,65 @@ def _select_entrypoint(prompt_message): return selected_entrypoint + @staticmethod + def _select_internal_entrypoint(prompt_message): + offset = 0 + limit = 10 + selected_internal_entrypoint = None + while selected_internal_entrypoint is None: + response = EntrypointsManager._get_internal_provider_entrypoints( + offset=offset, limit=limit + ) + internal_entrypoints = response["items"] + total = response["total"] + + if ( + not internal_entrypoints and offset == 0 + ): # No internal entrypoints available at all + console.print("No internal entrypoints available.") + return None + + entrypoints = [] + for internal_entrypoint in internal_entrypoints: + item = ( + internal_entrypoint["id"], + str(internal_entrypoint["chain_id"]), + internal_entrypoint["url"], + internal_entrypoint["identity"], + ) + entrypoints.append(item) + + navigation_message = "" + if offset > 0: + entrypoints.insert(0, ("Previous Page", "prev")) + navigation_message += "[bold yellow]Previous Page: Go back to the previous page.[/bold yellow]" + if total > offset + limit: + entrypoints.append(("Next Page", "next")) + navigation_message += "[bold yellow]Next Page: Move to the next page of entrypoints.[/bold yellow]" + + if navigation_message: + console.print(navigation_message) + + questions = [ + inquirer.List( + "entrypoint", + message=prompt_message, + choices=entrypoints, + carousel=True, + ), + ] + answers = inquirer.prompt(questions) + choice = answers["entrypoint"] + + if choice == "next": + offset += limit + elif choice == "prev": + offset = max(0, offset - limit) + else: + selected_internal_entrypoint = choice + + return selected_internal_entrypoint + @entrypoints_app.command("create") def entrypoint_create(config_file: Optional[str] = Option(None, "--config-file", "-c")): @@ -353,37 +426,6 @@ def entrypoint_create_internal( break -@entrypoints_app.command("view-internal") -def entrypoint_get_internal( - entrypoint_id: Optional[str] = Argument( - None, help="The UUID of the entrypoint to view." - ), -): - admin_guard() - entrypoint_id = entrypoint_id or EntrypointsManager._select_entrypoint( - "What's the ID of the entrypoint you want to view?" - ) - response = make_request_with_api_key( - "GET", f"{V1Routes.INTERNAL_PROVIDER_ENTRYPOINTS}/{entrypoint_id}" - ) - json_response = response.json() - - if response.status_code == 200: - items = [ - ( - str(json_response["id"]), - json_response["url"], - json_response["chain"]["name"], - json_response["identity"], - ) - ] - EntrypointsManager._print_table( - items, ["Entrypoint ID", "URL", "Chain", "Identity"] - ) - else: - console.print(f"Error getting entrypoint: {json_response['detail']}") - - @entrypoints_app.command("update-internal") def entrypoint_update_internal( entrypoint_id: Optional[str] = Argument( @@ -397,7 +439,7 @@ def entrypoint_update_internal( ), ): admin_guard() - entrypoint_id = entrypoint_id or EntrypointsManager._select_entrypoint( + entrypoint_id = entrypoint_id or EntrypointsManager._select_internal_entrypoint( "What's the ID of the entrypoint you want to update?" ) if config_file: @@ -421,3 +463,20 @@ def entrypoint_update_internal( console.print("Your internal provider entrypoint has been updated.") else: console.print(f"Error updating entrypoint: {json_response['detail']}") + + +@entrypoints_app.command("list-internal") +def entrypoint_list_internal( + limit: int = Option(10, help="Number of entrypoints per page."), +): + admin_guard() + + internal_entrypoints = [ + (item["id"], str(item["chain_id"]), item["url"], item["identity"]) + for item in EntrypointsManager._get_internal_provider_entrypoints()["items"] + ] + + # Print table of internal entrypoints + EntrypointsManager._print_table( + internal_entrypoints, ["Entrypoint ID", "Chain ID", "URL", "Identity"] + ) diff --git a/stateless/main.py b/stateless/main.py index 7bd1180..3b9dbee 100644 --- a/stateless/main.py +++ b/stateless/main.py @@ -35,7 +35,7 @@ |_____/ \__\__,_|\__\___|_|\___||___/___/ \_____|______|_____| """ # noqa: W291 -__version__ = "0.0.19" # Keep this in sync with pyproject.toml +__version__ = "0.0.20" # Keep this in sync with pyproject.toml def version_callback(value: bool):