fix(cses): add token fast path to login and improve error surfacing
Problem: `login()` always ran the full web login flow even with a
valid cached token, prompting the user unnecessarily. Submit errors
only checked `message`, missing `error` field.
Solution: check the cached token via `_check_token` at the start of
`login()`; return immediately if valid. Error body now checks
`body.get("error") or body.get("message")` before falling back to
raw text.
This commit is contained in:
parent
65d119cdfc
commit
397576ad93
1 changed files with 16 additions and 1 deletions
|
|
@ -248,7 +248,21 @@ class CSESScraper(BaseScraper):
|
|||
if not username or not password:
|
||||
return self._login_error("Missing username or password")
|
||||
|
||||
token = credentials.get("token")
|
||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||
if token:
|
||||
print(json.dumps({"status": "checking_login"}), flush=True)
|
||||
if await self._check_token(client, token):
|
||||
return LoginResult(
|
||||
success=True,
|
||||
error="",
|
||||
credentials={
|
||||
"username": username,
|
||||
"password": password,
|
||||
"token": token,
|
||||
},
|
||||
)
|
||||
|
||||
print(json.dumps({"status": "logging_in"}), flush=True)
|
||||
token = await self._web_login(client, username, password)
|
||||
if not token:
|
||||
|
|
@ -460,7 +474,8 @@ class CSESScraper(BaseScraper):
|
|||
|
||||
if r.status_code not in range(200, 300):
|
||||
try:
|
||||
err = r.json().get("message", r.text)
|
||||
body = r.json()
|
||||
err = body.get("error") or body.get("message") or r.text
|
||||
except Exception:
|
||||
err = r.text
|
||||
return self._submit_error(f"Submit request failed: {err}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue