Revert Context Manager Run

This commit is contained in:
David Sangrey committed 2026-01-04 21:10:15 -05:00
1 parent 5ea845c3ac
commit f0f26fb275
2 files changed
+36 -64

No files matched your search

+17 -39
View File
@@ -302,40 +302,17 @@ class Auth:
self.requests_session = self._sessions[cmdr]
def close(self) -> None:
"""Close session for this commander."""
with self._sessions_lock:
if self.cmdr in self._sessions:
self._sessions[self.cmdr].close()
del self._sessions[self.cmdr]
def __enter__(self):
"""Allow Opening as a Context Manager."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Allow Closing as a Context Manager."""
self.close()
def post(self, url: str, **kwargs) -> requests.Response:
"""POST with retry/backoff handled by requests Session."""
try:
return self.requests_session.post(url, **kwargs)
except requests.RequestException:
logger.exception(f"CAPI POST request failed for {self.cmdr} at {url}")
raise
def get(self, url: str, **kwargs) -> requests.Response:
"""GET with retry/backoff handled by requests Session."""
try:
return self.requests_session.get(url, **kwargs)
except requests.RequestException:
logger.exception(f"CAPI GET request failed for {self.cmdr} at {url}")
raise
def refresh(self) -> str | None:
"""Attempt to use Refresh Token to get a valid Access Token, else start new authorization."""
logger.debug(f'Trying refresh for Commander "{self.cmdr}"')
"""
Attempt use of Refresh Token to get a valid Access Token.
If the Refresh Token doesn't work, make a new authorization request.
:return: Access Token if retrieved, else None.
"""
logger.debug(f'Trying for "{self.cmdr}"')
should_return: bool
# Killswitch check
should_return, _ = killswitch.check_killswitch('capi.auth', {})
@@ -645,13 +622,14 @@ class Session:
self.credentials = credentials
self.state = Session.STATE_INIT
self.auth = Auth(self.credentials['cmdr']) # type: ignore
with Auth(self.credentials['cmdr']) as auth: # type: ignore
access_token = auth.refresh()
if access_token:
logger.debug('We have an access_token')
self.start_frontier_auth(access_token)
return True
access_token = self.auth.refresh()
if access_token:
logger.debug('We have an access_token')
self.auth = None
self.start_frontier_auth(access_token)
return True
logger.debug('We do NOT have an access_token')
self.state = Session.STATE_AUTH
+19 -25
View File
@@ -377,6 +377,24 @@ else: # Linux / Run from source
class HTTPRequestHandler(BaseHTTPRequestHandler):
"""Simple HTTP server to handle IPC from protocol handler."""
AUTH_RESPONSE_HTML = """\
<html>
<head>
<title>Authentication successful - Elite: Dangerous</title>
<style>
body { background-color: #000; color: #fff; font-family: "Helvetica Neue", Arial, sans-serif; }
h1 { text-align: center; margin-top: 100px; }
p { text-align: center; }
</style>
</head>
<body>
<h1>Authentication successful</h1>
<p>Thank you for authenticating.</p>
<p>Please close this browser tab now.</p>
</body>
</html>
"""
def parse(self) -> bool:
"""Parse a request."""
logger.trace_if("frontier-auth.http", f"Got message on path: {self.path}")
@@ -401,7 +419,7 @@ else: # Linux / Run from source
if self.parse():
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(self._generate_auth_response().encode())
self.wfile.write(self.AUTH_RESPONSE_HTML.encode())
else:
self.send_response(404)
self.end_headers()
@@ -409,30 +427,6 @@ else: # Linux / Run from source
def log_request(self, code: int | str = "-", size: int | str = "-") -> None:
"""Override to prevent logging."""
def _generate_auth_response(self) -> str:
"""
Generate the authentication response HTML.
:return: The HTML content of the authentication response.
"""
return (
"<html>"
"<head>"
"<title>Authentication successful - Elite: Dangerous</title>"
"<style>"
'body { background-color: #000; color: #fff; font-family: "Helvetica Neue", Arial, sans-serif; }'
"h1 { text-align: center; margin-top: 100px; }"
"p { text-align: center; }"
"</style>"
"</head>"
"<body>"
"<h1>Authentication successful</h1>"
"<p>Thank you for authenticating.</p>"
"<p>Please close this browser tab now.</p>"
"</body>"
"</html>"
)
def get_handler_impl() -> type[GenericProtocolHandler]:
"""