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
4 changes: 2 additions & 2 deletions polyapi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 23 additions & 5 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 @@ -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
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down