-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
68 lines (53 loc) · 1.85 KB
/
config.py
File metadata and controls
68 lines (53 loc) · 1.85 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
import sys
import os
import configparser
from typing import Tuple
# 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 initialize_config():
key, url = get_api_key_and_url()
if not key or not url:
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: ")
if url and key:
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)
if not key or not url:
print("Poly API Key and Poly API Base URL are required.")
sys.exit(1)
return key, url