diff --git a/polyapi/cli.py b/polyapi/cli.py index 66dd234..af5ab46 100644 --- a/polyapi/cli.py +++ b/polyapi/cli.py @@ -2,7 +2,7 @@ from polyapi.utils import print_green -from .config import clear_config, set_api_key_and_url +from .config import initialize_config, set_api_key_and_url from .generate import generate, clear from .function_cli import function_add_or_update, function_execute from .rendered_spec import get_and_update_rendered_spec @@ -43,7 +43,7 @@ def execute_from_cli() -> None: elif command == "setup" and len(args.subcommands) == 2: set_api_key_and_url(args.subcommands[1], args.subcommands[0]) elif command == "setup": - clear_config() + initialize_config(force=True) generate() elif command == "update_rendered_spec": assert len(args.subcommands) == 1 diff --git a/polyapi/config.py b/polyapi/config.py index 5e0e143..19016f5 100644 --- a/polyapi/config.py +++ b/polyapi/config.py @@ -3,6 +3,8 @@ import configparser from typing import Tuple +from polyapi.utils import is_valid_polyapi_url, is_valid_uuid, print_green, print_yellow + # cached values API_KEY = None API_URL = None @@ -55,18 +57,34 @@ def set_api_key_and_url(key: str, url: str): config.write(f) -def initialize_config(): +def initialize_config(force=False): key, url = get_api_key_and_url() - if not key or not url: + if force or (not key or not url): + url = url or "https://na1.polyapi.io" print("Please setup your connection to PolyAPI.") - url = input("? Poly API Base URL (https://na1.polyapi.io): ") or "https://na1.polyapi.io" - key = input("? Poly App Key or User Key: ") + url = input(f"? Poly API Base URL ({url}): ").strip() or url + + if not key: + key = input("? Poly App Key or User Key: ").strip() + else: + key_input = input(f"? Poly App Key or User Key ({key}): ").strip() + key = key_input if key_input else key if url and key: + errors = [] + if not is_valid_polyapi_url(url): + errors.append(f"{url} is not a valid Poly API Base URL") + if not is_valid_uuid(key): + errors.append(f"{key} is not a valid Poly App Key or User Key") + if errors: + print_yellow("\n".join(errors)) + sys.exit(1) + set_api_key_and_url(key, url) + print_green(f"Poly setup complete.") if not key or not url: - print("Poly API Key and Poly API Base URL are required.") + print_yellow("Poly API Key and Poly API Base URL are required.") sys.exit(1) return key, url diff --git a/polyapi/utils.py b/polyapi/utils.py index 259e727..a5141a6 100644 --- a/polyapi/utils.py +++ b/polyapi/utils.py @@ -1,6 +1,7 @@ import keyword import re import os +import uuid from typing import Tuple, List from colorama import Fore, Style from polyapi.constants import BASIC_PYTHON_TYPES @@ -208,4 +209,23 @@ def rewrite_reserved(s: str) -> str: def rewrite_arg_name(s: str): - return rewrite_reserved(camelCase(s)) \ No newline at end of file + return rewrite_reserved(camelCase(s)) + + +valid_subdomains = ["na[1-2]", "eu[1-2]", "dev"] + + +def is_valid_polyapi_url(_url: str): + # Join the subdomains into a pattern + subdomain_pattern = "|".join(valid_subdomains) + pattern = rf"^https://({subdomain_pattern})\.polyapi\.io$" + return re.match(pattern, _url) is not None + + +def is_valid_uuid(uuid_string, version=4): + try: + uuid_obj = uuid.UUID(uuid_string, version=version) + except ValueError: + return False + + return str(uuid_obj) == uuid_string diff --git a/pyproject.toml b/pyproject.toml index 3969c19..3ae5911 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"] [project] name = "polyapi-python" -version = "0.3.0" +version = "0.3.1.dev0" description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers" authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }] dependencies = [