Skip to content

Add WithToken context override for per-request authentication#54

Closed
Its-donkey wants to merge 1 commit intomainfrom
feat/with-token-docs
Closed

Add WithToken context override for per-request authentication#54
Its-donkey wants to merge 1 commit intomainfrom
feat/with-token-docs

Conversation

@Its-donkey
Copy link
Owner

Summary

  • Adds WithToken(ctx, *Token) for per-request token overrides via context, enabling concurrent requests with different user tokens through a single client instance
  • Follows the same context-override pattern as NoCacheContext
  • Includes 5 new tests covering override behaviour, nil fallback, concurrency safety, and empty context
  • Full documentation updates across changelog, auth reference, advanced features, quickstart, examples, FAQ, troubleshooting, and cookbook

Motivation

Endpoints like Get Channel Followers require a user token scoped to each channel (moderator:read:followers). Without per-request token overrides, users must create N separate Client instances to make concurrent requests for N different channels. WithToken solves this:

for _, ch := range channels {
    go func(ch Channel) {
        ctx := helix.WithToken(ctx, ch.UserToken)
        followers, _ := client.GetChannelFollowers(ctx, &helix.GetChannelFollowersParams{
            BroadcasterID: ch.ID,
        })
    }(channel)
}

Changes

Code:

  • helix/client.go: Added WithToken, tokenFromContext, tokenContextKey. Modified doOnceWithResponse token resolution order (context > authClient > tokenProvider)
  • helix/client_test.go: 5 new tests

Documentation:

  • CHANGELOG.md: Added unreleased entry
  • docs/auth.md: New "WithToken (Per-Request Override)" section under Token Helpers
  • docs/advanced.md: New "Per-Request Token Override" section with concurrent multi-token example
  • docs/quickstart.md: Added WithToken to Token Management snippet
  • docs/examples/authentication.md: New "Per-Request Token Override" section with full working example
  • docs/examples/batch-caching.md: New "Concurrent Requests with Different Tokens" section
  • docs/faq.md: New FAQ entry "Can I use different tokens for different requests?"
  • docs/troubleshooting.md: Added WithToken + Client ID mismatch pitfall note
  • docs/cookbook.md: Added index entries for per-request token override

Note: PR #53 was accidentally merged into test instead of main. This PR targets main correctly and includes the same code changes plus full documentation.

Allow callers to override the client-level token on a per-request basis
using WithToken(ctx, token). This enables concurrent requests with
different user tokens through a single client instance, following the
same context-override pattern as NoCacheContext.

Updated documentation across changelog, auth reference, advanced
features, quickstart, examples, FAQ, troubleshooting, and cookbook.
@Its-donkey
Copy link
Owner Author

Closing - PRs should target test branch per promotion workflow.

@Its-donkey Its-donkey closed this Feb 6, 2026
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