-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
105 lines (81 loc) · 2.94 KB
/
config.py
File metadata and controls
105 lines (81 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sys
import os
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
def get_config_file_path() -> str:
currdir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(currdir, ".config.env")
def get_api_key_and_url() -> Tuple[str | None, str | None]:
""" return the api key and api url
"""
key = os.environ.get("POLY_API_KEY")
url = os.environ.get("POLY_API_BASE_URL")
if key and url:
return key, url
# check cached values to avoid disk read
global API_KEY
global API_URL
if API_KEY and API_URL:
return API_KEY, API_URL
# read config from disk
path = get_config_file_path()
if os.path.exists(path):
config = configparser.ConfigParser()
with open(path, "r") as f:
config.read_file(f)
if not key:
key = config.get("polyapi", "poly_api_key", fallback=None)
if not url:
url = config.get("polyapi", "poly_api_base_url", fallback=None)
# cache values so we only read from disk once
API_KEY = key
API_URL = url
return key, url
def set_api_key_and_url(key: str, url: str):
config = configparser.ConfigParser()
config["polyapi"] = {}
config.set("polyapi", "poly_api_key", key)
config.set("polyapi", "poly_api_base_url", url)
with open(get_config_file_path(), "w") as f:
config.write(f)
def initialize_config(force=False):
key, url = get_api_key_and_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(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_yellow("Poly API Key and Poly API Base URL are required.")
sys.exit(1)
return key, url
def clear_config():
if os.environ.get("POLY_API_KEY"):
print("Using POLY_API_KEY from environment. Please unset environment variable to manually set api key.")
return
global API_KEY
global API_URL
API_KEY = None
API_URL = None
path = get_config_file_path()
if os.path.exists(path):
os.remove(path)