From fe472958c5b9561cd5d7e76b62168d60a6a3f5ed Mon Sep 17 00:00:00 2001 From: Nathan Gardiner Date: Tue, 22 Oct 2019 19:10:22 +1100 Subject: [PATCH] Improved Tesla Login handling --- lib/TWCManager/Control/HTTPControl.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/TWCManager/Control/HTTPControl.py b/lib/TWCManager/Control/HTTPControl.py index a7241354..4c8e3545 100644 --- a/lib/TWCManager/Control/HTTPControl.py +++ b/lib/TWCManager/Control/HTTPControl.py @@ -163,7 +163,9 @@ def do_GET(self): global master url = urllib.parse.urlparse(self.path) - if (url.path == '/'): + if (url.path == '/' or + url.path == '/apiacct/True' or + url.path == '/apiacct/False'): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() @@ -181,9 +183,15 @@ def do_GET(self): page += "" page += "
" - if (not master.teslaLoginAskLater): + if (url.path == '/apiacct/False'): + page += "Failed to log in to Tesla Account. Please check username and password and try again." + + if (not master.teslaLoginAskLater and url.path != '/apiacct/True'): page += self.request_teslalogin() + if (url.path == '/apiacct/True'): + page += "Thank you, successfully fetched Tesla API token." + page += self.show_status() page += "
" @@ -243,15 +251,17 @@ def process_teslalogin(self): master.teslaLoginAskLater = True if (not master.teslaLoginAskLater): - # Process the information submitted - self.send_response(200) - self.send_header('Content-type','text/html') - self.end_headers() + # Connect to Tesla API + master.carapi.setCarApiLastErrorTime(0) ret = master.carapi.car_api_available(self.fields['email'][0],self.fields['password'][0]) - page = str(ret) - self.wfile.write(page.encode("utf-8")) + # Redirect to an index page with output based on the return state of + # the function + self.send_response(302) + self.send_header('Location', '/apiacct/' + str(ret)) + self.end_headers() + self.wfile.write(''.encode("utf-8")) return else: # User has asked to skip Tesla Account submission for this session