Exit if initial token request fails for Revolut

This commit is contained in:
Mark Veidemanis 2021-12-26 23:11:48 +00:00
parent 0c9fadbdf6
commit 3c7a6685b4
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 12 additions and 2 deletions

View File

@ -105,7 +105,7 @@ def start(handler, refresh_sec):
if settings.Revolut.SetupToken == "1":
deferLater(reactor, 1, handler.setup_auth)
else:
deferLater(reactor, 1, handler.get_new_token)
deferLater(reactor, 1, handler.get_new_token, True)
deferLater(reactor, 4, handler.setup_webhook)
lc = LoopingCall(handler.get_new_token)
lc.start(refresh_sec)

View File

@ -56,6 +56,7 @@ class Revolut(object):
try:
parsed = r.json()
except JSONDecodeError:
self.log.error("Error parsing access token response: {content}", content=r.content)
return False
if r.status_code == 200:
try:
@ -72,7 +73,7 @@ class Revolut(object):
self.log.error(f"Cannot refresh token: {parsed}", parsed=parsed)
return False
def get_new_token(self):
def get_new_token(self, fail=False):
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {
"grant_type": "refresh_token",
@ -85,6 +86,8 @@ class Revolut(object):
try:
parsed = r.json()
except JSONDecodeError:
if fail:
exit()
return False
if r.status_code == 200:
if "access_token" in parsed.keys():
@ -93,9 +96,13 @@ class Revolut(object):
return True
else:
self.log.error(f"Token refresh didn't contain access token: {parsed}", parsed=parsed)
if fail:
exit()
return False
else:
self.log.error(f"Cannot refresh token: {parsed}", parsed=parsed)
if fail:
exit()
return False
def setup_webhook(self):
@ -121,3 +128,6 @@ class Revolut(object):
if r.status_code == 200:
parsed = r.json()
return parsed
else:
self.log.error("Cannot get webhooks: {content}", r.content)
return False