8000 set-up API key config · jjjchens235/python-api-client@213d41c · GitHub
[go: up one dir, main page]

Skip to content

Commit 213d41c

Browse files
Justin WongJustin Wong
authored andcommitted
set-up API key config
1 parent cc8724e commit 213d41c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

setup.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import getpass
3+
import base64
4+
import time
5+
6+
7+
CONFIG_FILE_PATH = "config.py"
8+
9+
CONFIG_FILE_TEMPLATE = """credentials = dict(api_key='{0}') """
10+
11+
12+
# get hashed credentials
13+
try:
14+
api_key = base64.b64encode(raw_input("enter/paste API Key: ").encode()).decode()
15+
except:
16+
api_key = base64.b64encode(input("enter/paste API Key: ").encode()).decode()
17+
print(" Hashed: {}\n".format(api_key))
18+
19+
new_config = CONFIG_FILE_TEMPLATE.format(api_key)
20+
21+
# check if config file exists
22+
if not os.path.isfile(CONFIG_FILE_PATH):
23+
# create new config file
24+
with open(CONFIG_FILE_PATH, "w") as config_file:
25+
config_file.write(new_config)
26+
print("{} created successfully".format(CONFIG_FILE_PATH))
27+
else:
28+
with open(CONFIG_FILE_PATH, "r") as config_file:
29+
cur_config = config_file.read()
30+
if new_config != cur_config:
31+
# update config file
32+
with open(CONFIG_FILE_PATH, "w") as config_file:
33+
config_file.writelines(new_config)
34+
print("{} updated successfully".format(CONFIG_FILE_PATH))
35+
else:
36+
print("{} already contains latest credentials".format(CONFIG_FILE_PATH))
37+
time.sleep(2)
38+

0 commit comments

Comments
 (0)
0