From 9507bcc26c8edc07185ee753752509d57e4eb97d Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Fri, 3 Nov 2017 07:59:41 -0400 Subject: [PATCH] Implemented the refreshToken() method --- actions.py | 36 ++++++++++++++++++++++++++++++++---- launcher.py | 10 ++-------- login.py | 1 + 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/actions.py b/actions.py index 662a3a9..3f531c5 100644 --- a/actions.py +++ b/actions.py @@ -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: diff --git a/launcher.py b/launcher.py index 01b8a62..0b18ad3 100644 --- a/launcher.py +++ b/launcher.py @@ -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") diff --git a/login.py b/login.py index 5ffd798..349363c 100644 --- a/login.py +++ b/login.py @@ -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)