Skip to content

Use TokenRestrictions API for token creation#38

Merged
simonw merged 3 commits intomainfrom
claude/test-api-token-hook-6E0IQ
Feb 26, 2026
Merged

Use TokenRestrictions API for token creation#38
simonw merged 3 commits intomainfrom
claude/test-api-token-hook-6E0IQ

Conversation

@simonw
Copy link
Owner

@simonw simonw commented Feb 25, 2026

Refs:

This PR updates the token creation logic to use Datasette's TokenRestrictions API instead of manually managing restriction dictionaries.

Summary

Refactored the create_api_token view to leverage the TokenRestrictions class from datasette.tokens, simplifying the code and improving maintainability.

Key changes

  • Replaced manual restriction dictionary management (restrict_all, restrict_database, restrict_resource) with TokenRestrictions() instance
  • Updated restriction building to use TokenRestrictions methods: allow_all(), allow_database(), and allow_resource()
  • Changed datasette.create_token() call to await datasette.create_token() (now async)
  • Updated token creation call to pass restrictions parameter instead of individual restriction parameters
  • Added .gitignore entries for build/ and dist/ directories

Implementation details

The TokenRestrictions class provides a cleaner API for building token restrictions, eliminating the need for nested dictionary manipulation. The restriction building loop now directly calls methods on the TokenRestrictions instance, making the code more readable and less error-prone.

https://claude.ai/code/session_01RbZveeSogxSnjJ5yr6AFtq

These directories are generated by build tools (uv, pip, setuptools)
and should not be tracked.

https://claude.ai/code/session_01RbZveeSogxSnjJ5yr6AFtq
Adapt to Datasette's new token API: build a TokenRestrictions object
using builder methods instead of separate restrict_all/restrict_database/
restrict_resource dicts, and await the now-async create_token() call.

https://claude.ai/code/session_01RbZveeSogxSnjJ5yr6AFtq
@simonw
Copy link
Owner Author

simonw commented Feb 25, 2026

Tests will fail until I release Datasette 1.0a25 and depend on that.

@simonw simonw merged commit 3118208 into main Feb 26, 2026
10 checks passed
simonw added a commit that referenced this pull request Feb 26, 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.

2 participants