Hyperlinks for pydantic models
Read a blog post from the library author discussing why this library exists.
In a typical web application relationships between resources are modeled by primary and foreign keys in a database (integers, UUIDs, etc.). The most natural way to represent relationships in REST APIs is by URLs to the related resources (explained in this blog).
hrefs makes it easy to add hyperlinks between pydantic models in a declarative way. Just declare a Href field and the library will automatically convert between keys and URLs:
from hrefs import Href, BaseReferrableModel
from pydantic import BaseModel
class Book(BaseReferrableModel):
id: int
class Config:
details_view = "get_book"
class Library(BaseModel):
books: list[Href[Book]]
@app.get("/books/{id}")
def get_book(id: int) -> Book:
return Book(id=id)
@app.get("/library")
def get_library() -> Library:
"""
Will serialize into:
{"books":["https://example.com/books/1","https://example.com/books/2","https://example.com/books/3"]}
"""
return Library(books=[1,2,3])
@app.post("/library")
def post_library(library: Library):
"""
Assuming the request contains
{"books":["https://example.com/books/1","https://example.com/books/2","https://example.com/books/3"]}
Will deserialize into: [1,2,3]
"""
write_to_database([href.key for href in library.books])
hrefs was written especially with FastAPI in mind, but integrates into any application or framework using pydantic to parse and serialize models.
Check out the documentation to get started!
Installation
Install the library using pip or your favorite package management tool:
$ pip install hrefs
Metadata
Release files for hrefs 0.14
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hrefs-0.14.tar.gz | 42.6 kB | Without provenance | Details for hrefs-0.14.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hrefs-0.14-py3-none-any.whl | Python 3 | none | any | Details for hrefs-0.14-py3-none-any.whl |
Total release size: 59.9 kB
Release files / hrefs-0.14.tar.gz
| Download URL | hrefs-0.14.tar.gz |
|---|---|
| Size | 42.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a55a39a65b38eeabb76a24de3b8f3a98c16efd13019cff5f891f91208e46c253
|
|
BLAKE2b-256 checksum How to use checksums |
627075ffeae9229352fbd2ac55d496fd717f1243257448dbe84a389c6ff3b41b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|
Release files / hrefs-0.14-py3-none-any.whl
| Download URL | hrefs-0.14-py3-none-any.whl |
|---|---|
| Size | 17.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7040be02cabb4a9e998a3b5c62650b93199d05b27414573e2918f188e7c8b47f
|
|
BLAKE2b-256 checksum How to use checksums |
3a513ceacb47b789b10f0777ee8029716b4cdada8cbe2ce5d3ea8b7143d9cf1b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|
Security for hrefs 0.14
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.