|
3 | 3 | import configparser |
4 | 4 | from typing import Tuple |
5 | 5 |
|
| 6 | +from polyapi.utils import is_valid_polyapi_url, is_valid_uuid, print_green, print_yellow |
| 7 | + |
6 | 8 | # cached values |
7 | 9 | API_KEY = None |
8 | 10 | API_URL = None |
@@ -55,18 +57,34 @@ def set_api_key_and_url(key: str, url: str): |
55 | 57 | config.write(f) |
56 | 58 |
|
57 | 59 |
|
58 | | -def initialize_config(): |
| 60 | +def initialize_config(force=False): |
59 | 61 | 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" |
61 | 64 | 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 |
64 | 72 |
|
65 | 73 | 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 | + |
66 | 83 | set_api_key_and_url(key, url) |
| 84 | + print_green(f"Poly setup complete.") |
67 | 85 |
|
68 | 86 | 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.") |
70 | 88 | sys.exit(1) |
71 | 89 |
|
72 | 90 | return key, url |
|
0 commit comments