From 6debabbae0cdd943273a275fef4d5899bd8ff60e Mon Sep 17 00:00:00 2001 From: Sudipta Kumar Date: Mon, 4 Nov 2024 14:55:31 +0600 Subject: [PATCH 1/2] # Feature (3019): improve polyapi-python setup --- polyapi/config.py | 23 ++++++++++++++++++++--- polyapi/utils.py | 18 +++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) 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..f77a787 100644 --- a/polyapi/utils.py +++ b/polyapi/utils.py @@ -208,4 +208,20 @@ 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): + # Regular expression for UUID + pattern = r"^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$" + return re.match(pattern, uuid_string) is not None From 265e0f56145845fc3a3f553eab11cb718f96dc1a Mon Sep 17 00:00:00 2001 From: Sudipta Kumar Date: Mon, 4 Nov 2024 15:04:16 +0600 Subject: [PATCH 2/2] # Feature (3019): improve polyapi-python setup - UUID Validation check added --- polyapi/utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/polyapi/utils.py b/polyapi/utils.py index f77a787..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 @@ -221,7 +222,10 @@ def is_valid_polyapi_url(_url: str): return re.match(pattern, _url) is not None -def is_valid_uuid(uuid_string): - # Regular expression for UUID - pattern = r"^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$" - return re.match(pattern, uuid_string) 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