Skip to content

Commit 8723978

Browse files
eupharissudiptatj
andauthored
improve polyapi-python setup (#24)
* improve polyapi-python setup * # Feature (3019): improve polyapi-python setup (#25) * # Feature (3019): improve polyapi-python setup * # Feature (3019): improve polyapi-python setup - UUID Validation check added --------- Co-authored-by: Sudipta at TechJays <sudipta.kumar@techjays.com>
1 parent 2a4b538 commit 8723978

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

polyapi/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from polyapi.utils import print_green
44

5-
from .config import clear_config, set_api_key_and_url
5+
from .config import initialize_config, set_api_key_and_url
66
from .generate import generate, clear
77
from .function_cli import function_add_or_update, function_execute
88
from .rendered_spec import get_and_update_rendered_spec
@@ -43,7 +43,7 @@ def execute_from_cli() -> None:
4343
elif command == "setup" and len(args.subcommands) == 2:
4444
set_api_key_and_url(args.subcommands[1], args.subcommands[0])
4545
elif command == "setup":
46-
clear_config()
46+
initialize_config(force=True)
4747
generate()
4848
elif command == "update_rendered_spec":
4949
assert len(args.subcommands) == 1

polyapi/config.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import configparser
44
from typing import Tuple
55

6+
from polyapi.utils import is_valid_polyapi_url, is_valid_uuid, print_green, print_yellow
7+
68
# cached values
79
API_KEY = None
810
API_URL = None
@@ -55,18 +57,34 @@ def set_api_key_and_url(key: str, url: str):
5557
config.write(f)
5658

5759

58-
def initialize_config():
60+
def initialize_config(force=False):
5961
key, url = get_api_key_and_url()
60-
if not key or not url:
62+
if force or (not key or not url):
63+
url = url or "https://na1.polyapi.io"
6164
print("Please setup your connection to PolyAPI.")
62-
url = input("? Poly API Base URL (https://na1.polyapi.io): ") or "https://na1.polyapi.io"
63-
key = input("? Poly App Key or User Key: ")
65+
url = input(f"? Poly API Base URL ({url}): ").strip() or url
66+
67+
if not key:
68+
key = input("? Poly App Key or User Key: ").strip()
69+
else:
70+
key_input = input(f"? Poly App Key or User Key ({key}): ").strip()
71+
key = key_input if key_input else key
6472

6573
if url and key:
74+
errors = []
75+
if not is_valid_polyapi_url(url):
76+
errors.append(f"{url} is not a valid Poly API Base URL")
77+
if not is_valid_uuid(key):
78+
errors.append(f"{key} is not a valid Poly App Key or User Key")
79+
if errors:
80+
print_yellow("\n".join(errors))
81+
sys.exit(1)
82+
6683
set_api_key_and_url(key, url)
84+
print_green(f"Poly setup complete.")
6785

6886
if not key or not url:
69-
print("Poly API Key and Poly API Base URL are required.")
87+
print_yellow("Poly API Key and Poly API Base URL are required.")
7088
sys.exit(1)
7189

7290
return key, url

polyapi/utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import keyword
22
import re
33
import os
4+
import uuid
45
from typing import Tuple, List
56
from colorama import Fore, Style
67
from polyapi.constants import BASIC_PYTHON_TYPES
@@ -208,4 +209,23 @@ def rewrite_reserved(s: str) -> str:
208209

209210

210211
def rewrite_arg_name(s: str):
211-
return rewrite_reserved(camelCase(s))
212+
return rewrite_reserved(camelCase(s))
213+
214+
215+
valid_subdomains = ["na[1-2]", "eu[1-2]", "dev"]
216+
217+
218+
def is_valid_polyapi_url(_url: str):
219+
# Join the subdomains into a pattern
220+
subdomain_pattern = "|".join(valid_subdomains)
221+
pattern = rf"^https://({subdomain_pattern})\.polyapi\.io$"
222+
return re.match(pattern, _url) is not None
223+
224+
225+
def is_valid_uuid(uuid_string, version=4):
226+
try:
227+
uuid_obj = uuid.UUID(uuid_string, version=version)
228+
except ValueError:
229+
return False
230+
231+
return str(uuid_obj) == uuid_string

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"]
33

44
[project]
55
name = "polyapi-python"
6-
version = "0.3.0"
6+
version = "0.3.1.dev0"
77
description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
88
authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
99
dependencies = [

0 commit comments

Comments
 (0)