From 919136874d3c32b354a3ea9d3fa2872cb233b754 Mon Sep 17 00:00:00 2001
From: Athanasius <github@miggy.org>
Date: Sat, 6 Nov 2021 08:41:09 +0000
Subject: [PATCH] CAPI: Re-auth when Access Token expired: Need to .close()
 first

The old code did this, and without it we enter a loop of:

1. CAPI says unauthorized
2. We call login()
3. But companion.session.state == STATE_OK, so do nothing
4. Re-scheduled CAPI request goes back to 1.
---
 EDMarketConnector.py | 2 ++
 monitor.py           | 1 +
 2 files changed, 3 insertions(+)

diff --git a/EDMarketConnector.py b/EDMarketConnector.py
index 32e4072a..3b969d8b 100755
--- a/EDMarketConnector.py
+++ b/EDMarketConnector.py
@@ -1160,6 +1160,8 @@ class AppWindow(object):
             self.status['text'] = _('Frontier CAPI server error')
 
         except companion.CredentialsRequireRefresh:
+            # We need to 'close' the auth else it'll see STATE_OK and think login() isn't needed
+            companion.session.close()
             # LANG: Frontier CAPI Access Token expired, trying to get a new one
             self.status['text'] = _('CAPI: Refreshing access token...')
             if companion.session.login():
diff --git a/monitor.py b/monitor.py
index debba020..65119f7f 100644
--- a/monitor.py
+++ b/monitor.py
@@ -1210,6 +1210,7 @@ class EDLogs(FileSystemEventHandler):  # type: ignore # See below
                         }
 
                     except KeyError:
+                        # TODO: Log the exception details too, for some clue about *which* key
                         logger.error(f"LoadoutEquipModule: {entry}")
 
             elif event_type == 'loadoutremovemodule':