perf(cses): cache API token across submits
Problem: every `:CP submit` on CSES ran the full 5-request login flow (~1.5 s overhead) even when the token from a previous submit was still valid. Solution: persist the API token in credentials via a `credentials` ndjson event. On subsequent submits, validate the cached token with a single GET before falling back to the full login.
This commit is contained in:
parent
08593d828d
commit
027fae65a4
1 changed files with 35 additions and 3 deletions
|
|
@ -342,6 +342,19 @@ class CSESScraper(BaseScraper):
|
||||||
return None
|
return None
|
||||||
return token
|
return token
|
||||||
|
|
||||||
|
async def _check_token(
|
||||||
|
self, client: httpx.AsyncClient, token: str
|
||||||
|
) -> bool:
|
||||||
|
try:
|
||||||
|
r = await client.get(
|
||||||
|
f"{API_URL}/login",
|
||||||
|
headers={"X-Auth-Token": token, **HEADERS},
|
||||||
|
timeout=TIMEOUT_S,
|
||||||
|
)
|
||||||
|
return r.status_code == 200
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
async def submit(
|
async def submit(
|
||||||
self,
|
self,
|
||||||
contest_id: str,
|
contest_id: str,
|
||||||
|
|
@ -356,11 +369,30 @@ class CSESScraper(BaseScraper):
|
||||||
return self._submit_error("Missing credentials. Use :CP login cses")
|
return self._submit_error("Missing credentials. Use :CP login cses")
|
||||||
|
|
||||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
print(json.dumps({"status": "logging_in"}), flush=True)
|
token = credentials.get("token")
|
||||||
|
|
||||||
|
if token:
|
||||||
|
print(json.dumps({"status": "checking_login"}), flush=True)
|
||||||
|
if not await self._check_token(client, token):
|
||||||
|
token = None
|
||||||
|
|
||||||
token = await self._web_login(client, username, password)
|
|
||||||
if not token:
|
if not token:
|
||||||
return self._submit_error("Login failed (bad credentials?)")
|
print(json.dumps({"status": "logging_in"}), flush=True)
|
||||||
|
token = await self._web_login(client, username, password)
|
||||||
|
if not token:
|
||||||
|
return self._submit_error("Login failed (bad credentials?)")
|
||||||
|
print(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"credentials": {
|
||||||
|
"username": username,
|
||||||
|
"password": password,
|
||||||
|
"token": token,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
print(json.dumps({"status": "submitting"}), flush=True)
|
print(json.dumps({"status": "submitting"}), flush=True)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue