diff --git a/polyapi/config.py b/polyapi/config.py index e1122b7..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 @@ -60,14 +62,29 @@ def initialize_config(force=False): if force or (not key or not url): url = url or "https://na1.polyapi.io" print("Please setup your connection to PolyAPI.") - url = input(f"? Poly API Base URL ({url}): ") or url - key = input(f"? Poly App Key or User Key ({key}): " if key else "? Poly App Key or User Key: ") or 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