Implemented the refreshToken() method

This commit is contained in:
Clayton Wilson 2017-11-03 07:59:41 -04:00
parent 17fcc6cc18
commit 9507bcc26c
3 changed files with 35 additions and 12 deletions

View File

@ -5,9 +5,16 @@ import datetime
import dateutil
import os
from checks import checkTimestamp
from checks import getToken
def clear_screen():
IS_WINDOWS = os.name == "nt"
if IS_WINDOWS:
os.system("cls")
else:
os.system("clear")
def refreshToken():
print("Not implemented yet")
if os.path.exists("login.json"):
try:
with open("login.json") as json_data:
@ -15,14 +22,35 @@ def refreshToken():
saveTime = dateutil.parser.parse(login["TIMESTAMP"])
curTime = datetime.datetime.now().replace(tzinfo=None)# TODO use UTC time?
json_data.close()
LOGIN_DATA = {
"apikey": login["API_KEY"],
"userkey": login["USER_KEY"],
"username": login["USER_NAME"]
}
if checkTimestamp(saveTime, curTime):
While True:
print("The current token is still valid. Do you still want to grab a different one")
while True:
print("Your current token is still valid. Are you sure you want to grab a different one?")
choice = input("(y/n) ")
if choice is "n":
break
elif choice is "y":
token = getToken() # TODO finish setting this up. Make it occur if the if statement above fails
login["TOKEN"] = getToken(LOGIN_DATA) # TODO find a better way to run this on both paths
login["TIMESTAMP"] = str(datetime.datetime.now().replace(tzinfo=None))
obj = open("login.json", "w")
obj.write(json.dumps(login))
obj.close()
print("\nNew token acquired!\n")
break
clear_screen()
else:
login["TOKEN"] = getToken(LOGIN_DATA)
login["TIMESTAMP"] = str(datetime.datetime.now().replace(tzinfo=None))
obj = open("login.json", "w")
obj.write(json.dumps(login))
obj.close()
print("New token acquired!\n")
except Exception as e:
print("You need to log in first. Select Login/Change login.\n") # TODO make a set of constants for error codes
else:

View File

@ -2,13 +2,6 @@ import os # TODO: change the order of all import statements to
from login import * # TODO: 2. related 3rd party
from actions import * # TODO: 3. local application with blank lines between
def clear_screen():
IS_WINDOWS = os.name == "nt"
if IS_WINDOWS:
os.system("cls")
else:
os.system("clear")
def user_choice():
return input("> ").lower().strip()
@ -45,7 +38,8 @@ while True:
login()
wait()
elif choice == "4":
refreshToken()# TODO implement this
clear_screen()
refreshToken()
wait()
elif choice == "5":
print("install requirements")

View File

@ -52,6 +52,7 @@ def login():
obj = open("login.json", "w")
obj.write(json.dumps(login))
obj.close()
print("\nLogin successful!")
else:# if login.json already exists
with open("login.json") as json_data:# TODO add a check for a login that is damaged/modified
login = json.load(json_data)