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.9. For python == 3.9, typing_extensions is required. 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
The requirements.txt file is for development and testing. If you have any interest in either, create a virtual environment and install this package. The following may work.
python -m venv /path/to/virtual-environment
cd /path/to/virtual-environment
source bin/activate
pushd /path/to/repository
pip install -r requirements.txt
Run unit tests:
pytest --cov=src --cov-report term-missing tests
Or
tox
Package Verification
As of version 1.4.0, releases use trusted publishing.
Project details
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.4.0.tar.gz.
File metadata
- Download URL: resource_id-1.4.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa17514541c3cdf84f12a573f1f0d1cd01e4d5b50e88dfc8ce5a99f45e028bb2
|
|
| MD5 |
1cec8824fc02061f9b579132df901359
|
|
| BLAKE2b-256 |
c83112d3192bc7834885436906acc512ca7fcc6060481036876c5eb938838d1d
|
Provenance
The following attestation bundles were made for resource_id-1.4.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.4.0.tar.gz -
Subject digest:
fa17514541c3cdf84f12a573f1f0d1cd01e4d5b50e88dfc8ce5a99f45e028bb2 - Sigstore transparency entry: 191049761
- Sigstore integration time:
-
Permalink:
declaresub/resource-id@e9d4b2c7d9ed6b84542ce9d13e0e1401d18297ae -
Branch / Tag:
refs/tags/v1.4.0 - Owner: https://github.com/declaresub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-package.yml@e9d4b2c7d9ed6b84542ce9d13e0e1401d18297ae -
Trigger Event:
push
-
Statement type:
File details
Details for the file resource_id-1.4.0-py3-none-any.whl.
File metadata
- Download URL: resource_id-1.4.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7781a04128271744dd931f6f20e0cded5d6c0fe508586b2a8907b22c2111fd2f
|
|
| MD5 |
33cdecbee3eb7a72509bb87578c2e02e
|
|
| BLAKE2b-256 |
a0addd683fa322fee80e810678e136de7517227919f15552962ac10533bc9312
|
Provenance
The following attestation bundles were made for resource_id-1.4.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.4.0-py3-none-any.whl -
Subject digest:
7781a04128271744dd931f6f20e0cded5d6c0fe508586b2a8907b22c2111fd2f - Sigstore transparency entry: 191049765
- Sigstore integration time:
-
Permalink:
declaresub/resource-id@e9d4b2c7d9ed6b84542ce9d13e0e1401d18297ae -
Branch / Tag:
refs/tags/v1.4.0 - Owner: https://github.com/declaresub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-package.yml@e9d4b2c7d9ed6b84542ce9d13e0e1401d18297ae -
Trigger Event:
push
-
Statement type: