Hyperlinks for pydantic models

Overview

Hyperlinks for pydantic models

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
You might also like...
flask extension for integration with the awesome pydantic package

Flask-Pydantic Flask extension for integration of the awesome pydantic package with Flask. Installation python3 -m pip install Flask-Pydantic Basics v

flask extension for integration with the awesome pydantic package

Flask-Pydantic Flask extension for integration of the awesome pydantic package with Flask. Installation python3 -m pip install Flask-Pydantic Basics v

A curated list of awesome things related to Pydantic! 🌪️

Awesome Pydantic A curated list of awesome things related to Pydantic. These packages have not been vetted or approved by the pydantic team. Feel free

Pydantic model support for Django ORM

Pydantic model support for Django ORM

flask extension for integration with the awesome pydantic package

flask extension for integration with the awesome pydantic package

Flask Sugar is a web framework for building APIs with Flask, Pydantic and Python 3.6+ type hints.
Flask Sugar is a web framework for building APIs with Flask, Pydantic and Python 3.6+ type hints.

Flask Sugar is a web framework for building APIs with Flask, Pydantic and Python 3.6+ type hints. check parameters and generate API documents automatically. Flask Sugar是一个基于flask,pyddantic,类型注解的API框架, 可以检查参数并自动生成API文档

Pydantic-ish YAML configuration management.
Pydantic-ish YAML configuration management.

Pydantic-ish YAML configuration management.

(A)sync client for sms.ru with pydantic responses

🚧 aioSMSru Send SMS Check SMS status Get SMS cost Get balance Get limit Get free limit Get my senders Check login/password Add to stoplist Remove fro

A Proof of concept of a modern python CLI with click, pydantic, rich and anyio

httpcli This project is a proof of concept of a modern python networking cli which can be simple and easy to maintain using some of the best packages

MongoX is an async python ODM for MongoDB which is built on top Motor and Pydantic.
MongoX is an async python ODM for MongoDB which is built on top Motor and Pydantic.

MongoX MongoX is an async python ODM (Object Document Mapper) for MongoDB which is built on top Motor and Pydantic. The main features include: Fully t

Comprehensive OpenAPI schema generator for Django based on pydantic
Comprehensive OpenAPI schema generator for Django based on pydantic

🗡️ Djagger Automated OpenAPI documentation generator for Django. Djagger helps you generate a complete and comprehensive API documentation of your Dj

A Fast Command Analyser based on Dict and Pydantic

Alconna Alconna 隶属于ArcletProject, 在Cesloi内有内置 Alconna 是 Cesloi-CommandAnalysis 的高级版,支持解析消息链 一般情况下请当作简易的消息链解析器/命令解析器 文档 暂时的文档 Example from arclet.alcon

🪛 A simple pydantic to Form FastAPI model converter.
🪛 A simple pydantic to Form FastAPI model converter.

pyfa-converter Makes it pretty easy to create a model based on Field [pydantic] and use the model for www-form-data. How to install? pip install pyfa_

A RESTful API for creating and monitoring resource components of a hypothetical build system. Built with FastAPI and pydantic. Complete with testing and CI.
A RESTful API for creating and monitoring resource components of a hypothetical build system. Built with FastAPI and pydantic. Complete with testing and CI.

diskspace-monitor-CRUD Background The build system is part of a large environment with a multitude of different components. Many of the components hav

Ever felt tired after preprocessing the dataset, and not wanting to write any code further to train your model? Ever encountered a situation where you wanted to record the hyperparameters of the trained model and able to retrieve it afterward? Models Playground is here to help you do that. Models playground allows you to train your models right from the browser. pyhsmm - library for approximate unsupervised inference in Bayesian Hidden Markov Models (HMMs) and explicit-duration Hidden semi-Markov Models (HSMMs), focusing on the Bayesian Nonparametric extensions, the HDP-HMM and HDP-HSMM, mostly with weak-limit approximations.
An implementation of model parallel GPT-3-like models on GPUs, based on the DeepSpeed library. Designed to be able to train models in the hundreds of billions of parameters or larger.

GPT-NeoX An implementation of model parallel GPT-3-like models on GPUs, based on the DeepSpeed library. Designed to be able to train models in the hun

CDIoU and CDIoU loss is like a convenient plug-in that can be used in multiple models. CDIoU and CDIoU loss have different excellent performances in several models such as Faster R-CNN, YOLOv4, RetinaNet and . There is a maximum AP improvement of 1.9% and an average AP of 0.8% improvement on MS COCO dataset, compared to traditional evaluation-feedback modules. Here we just use as an example to illustrate the code.
Code for pre-training CharacterBERT models (as well as BERT models).

Pre-training CharacterBERT (and BERT) This is a repository for pre-training BERT and CharacterBERT. DISCLAIMER: The code was largely adapted from an o

Comments
  • `Href` JSON schema

    `Href` JSON schema

    The JSON schema of Href should be that of its URL type. Right now pydantic doesn't support examining subfields of generic models, but provided it did, the Href.__modify_schema__() method should be implemented to use that.

    Re: https://github.com/samuelcolvin/pydantic/discussions/2902

    opened by jasujm 0
  • Configurable key type

    Configurable key type

    I would like to configure keys so that they could be called something else than just id. They could even be composite and support more complicated URL structures:

    class Page(ReferrableModel):
        book: Href[Book]
        page_number: int
    
        class Config:
            details_view = "get_page"
    
    @app.get("/books/{book_id}/pages/{page_number}", response_model=Page)
    def get_page(book_id: UUID, page_number: int):
        ...
    
    # Href[Page] would have key type consisting of (book_id, page_number): Tuple[UUID, int])
    
    opened by jasujm 0
  • Self references

    Self references

    I would like to be able to write self href fields for models, so that they would be identified with URL in API responses:

    class Book(ReferrableModel):
        self: Href[ForwardRef(Book)]
    
    Book.update_forward_refs()
    
    • I would like to populate the self reference from field that may not necessarily be self (i.e. the id field of the underlying database model)
    • The real primary key of the underlying (database) model doesn't appear in the model definition, so it needs to be configurable
    Book(id=123)
    # this should work and produce -> {"self":"http://example.com/books/123"} or similar
    
    opened by jasujm 0
Releases(0.5.1)
  • 0.5.1(Mar 23, 2022)

  • 0.5(Mar 22, 2022)

    Added

    • Implement Href.__hash__()
    • hypothesis build strategy for hyperlinks
    • hrefs.starlette.href_context() for setting things other than Starlette requests as hyperlink context
    Source code(tar.gz)
    Source code(zip)
  • 0.4(Jan 17, 2022)

    Added

    • Support Python 3.10

    Changed

    • Use URL type in Href schema if using pydantic version 1.9 or later

    Fixed

    • Require pydantic version 1.8 or later, since 1.7 doesn't work with the library
    Source code(tar.gz)
    Source code(zip)
  • 0.3.1(Dec 29, 2021)

  • 0.3(Dec 27, 2021)

    Added

    • tox for test automation
    • Support for hyperlinks as model keys

    Changed

    • Replace get_key_type() and get_key_url() with parse_as_key() and parse_as_url(), respectively
    Source code(tar.gz)
    Source code(zip)
  • 0.2(Dec 17, 2021)

    Added

    • Implement Href.__modify_schema__()
    • Make it possible to configure model key by using hrefs.PrimaryKey annotation.

    Changed

    • Split Referrable.href_types() into get_key_type() and get_url_type(), respectively
    Source code(tar.gz)
    Source code(zip)
A RESTful API for creating and monitoring resource components of a hypothetical build system. Built with FastAPI and pydantic. Complete with testing and CI.

diskspace-monitor-CRUD Background The build system is part of a large environment with a multitude of different components. Many of the components hav

Nick Hopewell 67 Dec 14, 2022
A dynamic FastAPI router that automatically creates CRUD routes for your models

⚡ Create CRUD routes with lighting speed ⚡ A dynamic FastAPI router that automatically creates CRUD routes for your models

Adam Watkins 950 Jan 8, 2023
A dynamic FastAPI router that automatically creates CRUD routes for your models

⚡ Create CRUD routes with lighting speed ⚡ A dynamic FastAPI router that automatically creates CRUD routes for your models Documentation: https://fast

Adam Watkins 943 Jan 1, 2023
A dynamic FastAPI router that automatically creates CRUD routes for your models

⚡ Create CRUD routes with lighting speed ⚡ A dynamic FastAPI router that automatically creates CRUD routes for your models Documentation: https://fast

Adam Watkins 130 Feb 13, 2021
FastAPI Skeleton App to serve machine learning models production-ready.

FastAPI Model Server Skeleton Serving machine learning models production-ready, fast, easy and secure powered by the great FastAPI by Sebastián Ramíre

null 268 Jan 1, 2023
pydantic-i18n is an extension to support an i18n for the pydantic error messages.

pydantic-i18n is an extension to support an i18n for the pydantic error messages

Boardpack 48 Dec 21, 2022
Seamlessly integrate pydantic models in your Sphinx documentation.

Seamlessly integrate pydantic models in your Sphinx documentation.

Franz Wöllert 71 Dec 26, 2022
🪄 Auto-generate Streamlit UI from Pydantic Models and Dataclasses.

Streamlit Pydantic Auto-generate Streamlit UI elements from Pydantic models. Getting Started • Documentation • Support • Report a Bug • Contribution •

Lukas Masuch 103 Dec 25, 2022
Collections of pydantic models

pydantic-collections The pydantic-collections package provides BaseCollectionModel class that allows you to manipulate collections of pydantic models

Roman Snegirev 20 Dec 26, 2022
Pydantic models for pywttr and aiopywttr.

Pydantic models for pywttr and aiopywttr.

Almaz 2 Dec 8, 2022