Skip to content

Refresh token before execute - #448

Closed
MartinHjelmare wants to merge 1 commit into
Danielhiversen:masterfrom
MartinHjelmare:refresh-token-before-execute
Closed

Refresh token before execute#448
MartinHjelmare wants to merge 1 commit into
Danielhiversen:masterfrom
MartinHjelmare:refresh-token-before-execute

Conversation

@MartinHjelmare

Copy link
Copy Markdown
Contributor

Proactively refresh the access token before every reqest, if possible and needed.

When a refresh_access_token callback is configured, call it before
each HTTP request in execute() so that an expiring token is renewed
proactively rather than waiting for the watchdog reconnect cycle.
Rename _refresh_access_token_for_reconnect to _ensure_fresh_access_token
to reflect its broader role. Behaviour is unchanged for callers that
do not supply the callback. Add tests for the refresh, no-callback, and
None-return cases.
@augmentcode

augmentcode Bot commented Aug 17, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: This PR proactively refreshes a configured access token before GraphQL execution.
Changes:

  • Renames the realtime-specific wrapper to a shared token-refresh helper.
  • Calls that helper before `Tibber.execute()` sends an HTTP request.
  • Keeps the GraphQL and Data API token values updated when a new token is returned.
  • Adds tests for refreshed, absent, and unchanged-token callback outcomes.
Technical Notes: Refresh callback exceptions now stop the associated HTTP request and propagate to its caller.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 2 suggestions posted.

Fix All in Augment

Items Reviewed
  • ✅ Review PR #448

Comment augment review to trigger a new review at any time.

Comment thread tibber/__init__.py
timeout = timeout or self.timeout

if self._refresh_access_token is not None:
await self._ensure_fresh_access_token()

@augmentcode augmentcode Bot Aug 17, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tibber/__init__.py:136: When this callback returns a new token, _ensure_fresh_access_token() updates only Tibber._access_token and data_api; realtime continues holding the old transport token. An active subscription can then expire and gql retries will keep sending the stale token, causing a realtime outage until the watchdog runs.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

@MartinHjelmare MartinHjelmare Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is out of scope for this PR, since by design we have a separate flow for the realtime refresh access token during reconnect.

It's a good point though, and I think we can look at consolidating the token refresh and token state but that needs to be done separately, and carefully.

I'll mark this as draft for now, and see if we can do something better.

Comment thread tibber/__init__.py

async def _refresh_access_token_for_reconnect(self) -> str | None:
"""Refresh access token before reconnecting realtime subscriptions."""
async def _ensure_fresh_access_token(self) -> str | None:

@augmentcode augmentcode Bot Aug 17, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tibber/__init__.py:94: This helper is now used by normal GraphQL execution, but the public Tibber.__init__ documentation still says refresh_access_token runs only before reconnecting. Callers can therefore miss that it is invoked before each request and that callback failures now abort ordinary API operations.

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

@MartinHjelmare
MartinHjelmare marked this pull request as draft August 17, 2026 11:08
@MartinHjelmare MartinHjelmare mentioned this pull request Aug 24, 2026
1 task
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