fix: fallback without auth if twitch token is invalid#1083
Conversation
WalkthroughAdded authentication-aware GraphQL request flow with retry logic and improved error handling. Introduced new TwitchGQLError type and extended TwitchGQLPlaybackAccessTokenResponse with error fields. Updated token retrieval flow to support both authenticated and unauthenticated requests with retry mechanism. Changes
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/platform/twitch_gql.go (1)
155-208: Token is re-read from config on every retry attempt.
config.Get()at line 179 reads the config file from disk (per the implementation ininternal/config/config.go). Inside the retry loop, this means up to 3 disk reads per call. More importantly, the caller (TwitchGQLGetPlaybackAccessToken) already reads the token at line 290 to decide whether to use auth—but the actual token applied to the request is read independently at line 179, so the two could diverge if config changes between calls.Consider accepting the token as a parameter instead of re-reading config internally. This would eliminate redundant I/O and the consistency gap:
♻️ Suggested approach
-func twitchGQLRequestWithAuth(body string, includeAuth bool) ([]byte, error) { +func twitchGQLRequestWithAuth(body string, twitchToken string) ([]byte, error) { client := &http.Client{} const maxAttempts = 3 var lastErr error for attempt := 1; attempt <= maxAttempts; attempt++ { ... - twitchToken := config.Get().Parameters.TwitchToken - if includeAuth && twitchToken != "" { + if twitchToken != "" { req.Header.Set("Authorization", fmt.Sprintf("OAuth %s", twitchToken)) } ...The original
twitchGQLRequestwrapper would become:func twitchGQLRequest(body string) ([]byte, error) { - return twitchGQLRequestWithAuth(body, true) + return twitchGQLRequestWithAuth(body, config.Get().Parameters.TwitchToken) }And in
TwitchGQLGetPlaybackAccessToken:twitchToken := config.Get().Parameters.TwitchToken if twitchToken != "" { - respBytes, err := twitchGQLRequestWithAuth(body, true) + respBytes, err := twitchGQLRequestWithAuth(body, twitchToken) ... } - respBytes, err := twitchGQLRequestWithAuth(body, false) + respBytes, err := twitchGQLRequestWithAuth(body, "")🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@internal/platform/twitch_gql.go` around lines 155 - 208, The function is re-reading the Twitch token from disk inside the retry loop (config.Get() in twitchGQLRequestWithAuth), causing redundant I/O and potential inconsistency with the caller (TwitchGQLGetPlaybackAccessToken). Change the API to accept the token from the caller (e.g., add a token string parameter to twitchGQLRequestWithAuth or to twitchGQLRequest and update the wrapper twitchGQLRequest to match), remove the config.Get() call from inside twitchGQLRequestWithAuth, and use the passed token when building the Authorization header; then update TwitchGQLGetPlaybackAccessToken to read the token once and pass it into the request call. Ensure includeAuth handling still works by checking the passed token and the includeAuth flag, and keep the retry logic unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@internal/platform/twitch_gql.go`:
- Around line 155-208: The function is re-reading the Twitch token from disk
inside the retry loop (config.Get() in twitchGQLRequestWithAuth), causing
redundant I/O and potential inconsistency with the caller
(TwitchGQLGetPlaybackAccessToken). Change the API to accept the token from the
caller (e.g., add a token string parameter to twitchGQLRequestWithAuth or to
twitchGQLRequest and update the wrapper twitchGQLRequest to match), remove the
config.Get() call from inside twitchGQLRequestWithAuth, and use the passed token
when building the Authorization header; then update
TwitchGQLGetPlaybackAccessToken to read the token once and pass it into the
request call. Ensure includeAuth handling still works by checking the passed
token and the includeAuth flag, and keep the retry logic unchanged.
Fixes #1068