Skip to content

Log in again automatically when the session expires - #164

Merged
RazerM merged 2 commits into
mainfrom
feature/reauth
Aug 23, 2026
Merged

Log in again automatically when the session expires#164
RazerM merged 2 commits into
mainfrom
feature/reauth

Conversation

@RazerM

@RazerM RazerM commented Aug 22, 2026

Copy link
Copy Markdown
Member

A session that expired after two hours without requests caused every subsequent request to fail, since the client never logged in again. A 401 response now triggers a fresh login and a single retry of the request.

Confirmed against the live API: sessions are extended by activity, and an idle session returns a 401 with a JSON error body.

@codecov

codecov Bot commented Aug 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.16%. Comparing base (961027f) to head (ec45fde).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #164      +/-   ##
==========================================
+ Coverage   97.55%   98.16%   +0.61%     
==========================================
  Files           7        7              
  Lines        1185     1255      +70     
  Branches      130      133       +3     
==========================================
+ Hits         1156     1232      +76     
+ Misses         18       13       -5     
+ Partials       11       10       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@RazerM RazerM changed the title feature/reauth Log in again automatically when the session expires Aug 22, 2026
@RazerM
RazerM force-pushed the feature/reauth branch 6 times, most recently from 1a54261 to 09a4051 Compare August 22, 2026 23:02
Base automatically changed from feature/ratelimit-accounting to main August 23, 2026 13:55
RazerM added 2 commits August 23, 2026 15:55
Space-Track sessions last two hours and are refreshed by activity
(verified against the live API: each request returns a rotated session
cookie with a renewed Max-Age, and after 2.5 idle hours queries return
HTTP 401 with a JSON error body). A long-idle client previously failed
every subsequent request, because nothing ever cleared _authenticated
and so the client never logged in again.

On a 401, read the response, re-authenticate, and retry the request
once; a second 401 raises as before. 401 is the only trigger: 403 is
not treated as session expiry since it plausibly means missing account
permissions, where re-authenticating would only add login spam.

The Cookie header is set when a request is built, so the retry replaces
it from the cookie jar, which only sets the header on requests that
have none. An upload is only retried if its body can be sent again:
bytes, str, or a seekable file object, which HTTPX rewinds. A
non-seekable stream would otherwise be sent empty the second time.
The upload branch of _generic_request_generator was not covered.
@RazerM
RazerM merged commit aa51fb4 into main Aug 23, 2026
11 checks passed
@RazerM
RazerM deleted the feature/reauth branch August 23, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant