Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions polyapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion polyapi/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -208,4 +209,23 @@ def rewrite_reserved(s: str) -> str:


def rewrite_arg_name(s: str):
return rewrite_reserved(camelCase(s))
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