Skip to main content

Hyperlinks for pydantic models

Project description

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:

class Book(ReferrableModel):
    id: int

    class Config:
        details_view = "get_book"

class Library(BaseModel):
    books: List[Href[Book]]

@app.get("/library")
def get_library():
    # Will produce something like:
    # {"books":["http://example.com/books/1","http://example.com/books/2","http://example.com/books/3"]}
    return Library(books=[1,2,3]).json()

hrefs was written especially with FastAPI in mind, but integrates to 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

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

hrefs-0.5.1.tar.gz (31.9 kB view hashes)

Uploaded Source

Built Distribution

hrefs-0.5.1-py3-none-any.whl (12.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page