A base62-encoded identifier for use in URLs. Supports Pydantic validation.
Project description
ResourceId
The resource-id package provides a ResourceId class implementing base62-encoded identifiers, suitable for URLs, URIs, and perhaps something else.
ResourceId is written to work with Pydantic. In particular, it can be used with FastAPI as a path parameter.
Requirements
Resource-id requires Python >= 3.10. As of version 1.4.0, resource-id requires pydantic 2.
Installation
pip install resource-id
Usage
There is one class, ResourceId.
from resource_id import ResourceId
Create a ResourceId:
id = ResourceId(43)
id1 = ResourceId('deadbeef')
id2 = ResourceId(UUID(int=101))
In fact a ResourceId can be created from any object that implements int,
Create a URL path using a ResourceId:
path = f"/api/foo/{ResourceId('deadbeef')}"
Define a FastAPI request handler with a ResourceId:
@app.get('/api/foo/{foo_id}')
async def get_foo(foo_id: ResourceId):
...
Here, FastAPI will validate the value of the path variable foo_id with Pydantic. If validation fails, FastAPI returns 422 Unprocessable Entity.
Convert a ResourceId to a UUID:
res_id = ResourceId(43)
id = res_id.uuid
More
I have some projects that use PostgreSQL and asyncpg. It is quite simple to add conversion between PostgreSQL UUID datatype and ResourceId.
def get_uuid(value: ResourceId) -> str:
assert isinstance(value, ResourceId)
return str(value.uuid)
def get_resource_id(value: str) -> ResourceId:
assert isinstance(value, str)
return ResourceId(UUID(value))
async def init_connection(conn: asyncpg.Connection):
await conn.set_type_codec(
"uuid", encoder=get_uuid, decoder=get_resource_id, schema="pg_catalog"
)
Testing
This project uses uv for development. To set up and run tests:
uv sync --all-extras --dev
uv run pytest tests
Package Verification
As of version 1.4.0, releases use trusted publishing. As of version 1.5.0, commits are now signed using gitsign.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file resource_id-1.5.0.tar.gz.
File metadata
- Download URL: resource_id-1.5.0.tar.gz
- Upload date:
- Size: 54.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89a1b84d9da3ae777b6ea9595c42f4858cb7807e93cad10777fa69ca8e410813
|
|
| MD5 |
e5c00033d9dac0d16b80275f2cafcdb4
|
|
| BLAKE2b-256 |
54b2bd4e641637a7a57ceebf1f6acb78d0a63e401fb655cdfc7bdb7b5b2752d3
|
Provenance
The following attestation bundles were made for resource_id-1.5.0.tar.gz:
Publisher:
release-package.yml on declaresub/resource-id
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
resource_id-1.5.0.tar.gz -
Subject digest:
89a1b84d9da3ae777b6ea9595c42f4858cb7807e93cad10777fa69ca8e410813 - Sigstore transparency entry: 1402864868
- Sigstore integration time:
-
Permalink:
declaresub/resource-id@9863e15dd33bd30cb9bd24d693a4a3d2b782384a -
Branch / Tag:
refs/tags/v1.5.0 - Owner: https://github.com/declaresub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-package.yml@9863e15dd33bd30cb9bd24d693a4a3d2b782384a -
Trigger Event:
push
-
Statement type:
File details
Details for the file resource_id-1.5.0-py3-none-any.whl.
File metadata
- Download URL: resource_id-1.5.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
325e304c136076144a68023fc7453e765fc838e7c3090fba8e6f211390d01aab
|
|
| MD5 |
088d595e139deab20c478fc2c9d5344c
|
|
| BLAKE2b-256 |
e8078d25af496a08e78a012881e37e02a0e7b7c11275e047ce57aef73f984564
|
Provenance
The following attestation bundles were made for resource_id-1.5.0-py3-none-any.whl:
Publisher:
release-package.yml on declaresub/resource-id
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
resource_id-1.5.0-py3-none-any.whl -
Subject digest:
325e304c136076144a68023fc7453e765fc838e7c3090fba8e6f211390d01aab - Sigstore transparency entry: 1402864950
- Sigstore integration time:
-
Permalink:
declaresub/resource-id@9863e15dd33bd30cb9bd24d693a4a3d2b782384a -
Branch / Tag:
refs/tags/v1.5.0 - Owner: https://github.com/declaresub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-package.yml@9863e15dd33bd30cb9bd24d693a4a3d2b782384a -
Trigger Event:
push
-
Statement type: