-Model |
+Format |
Response |
-```python
-class ItemSummary(HyperModel):
- name: str
- id: str
- href = UrlFor(
- "read_item", {"item_id": ""}
- )
-```
+No Hypermdia
|
-```json
+```json linenums="1"
{
- "name": "Foo",
- "id": "item01",
- "href": "/items/item01"
+ "id_": "item01",
+ "name": "Foo",
+ "price": 10.2,
}
```
|
-
-```python
-class ItemSummary(HyperModel):
- name: str
- id: str
- link = HALFor(
- "read_item", {"item_id": ""},
- description="Read an item"
- )
+Level 0 Hypermedia (URLFor)
+
+ |
+
+
+```json linenums="1"
+{
+ "id_": "item01",
+ "name": "Foo",
+ "price": 10.2,
+
+ "href": "/items/item01",
+ "update": "/items/item01"
+}
```
|
+
+
-```json
+Level 1 Hypermedia (HAL)
+
+ |
+
+
+```json linenums="1"
{
- "name": "Foo",
- "id": "item01",
- "link": {
- "href": "/items/item01",
- "method": "GET",
- "description": "Read an item"
- }
+ "id_": "item01",
+ "name": "Foo",
+ "price": 10.2,
+
+ "_links": {
+ "self": {"href": "/items/item01"},
+ "update": {"href": "/items/item01"},
+ },
}
```
+ |
+
+
+
+
+Level 2 Hypermedia (Siren)
+
+ |
+
+
+```json linenums="1"
+{
+ "properties": {
+ "id_": "item01",
+ "name": "Foo",
+ "price": 10.2
+ },
+ "links": [
+ {
+ "rel": ["self"],
+ "href": "/items/item01"
+ }
+ ],
+ "actions": [
+ {
+ "name": "update",
+ "method": "PUT",
+ "href": "/items/item01",
+ "type": "application/x-www-form-urlencoded",
+ "fields": [
+ {
+ "name": "name",
+ "type": "text",
+ "value": "Foo"
+ },
+ {
+ "name": "description",
+ "type": "text",
+ "value": "None"
+ },
+ {
+ "name": "price",
+ "type": "number",
+ "value": "10.2"
+ }
+ ]
+ }
+ ]
+}
+```
|
@@ -102,4 +169,3 @@ This is an upstream issue, being tracked [here](https://github.com/encode/starle
Huge thanks to [@christoe](https://github.com/christoe) for building support for Pydantic 2.
-Some functionality is based on [Flask-Marshmallow](https://github.com/marshmallow-code/flask-marshmallow/blob/dev/src/flask_marshmallow/fields.py) `URLFor` class.
diff --git a/docs/advanced.md b/docs/advanced.md
index e4b9aa0..7152e97 100644
--- a/docs/advanced.md
+++ b/docs/advanced.md
@@ -1,90 +1,566 @@
# Advanced Usage
+In addition to what the standard for the format defines, fastapi-hypermodel has
+some additional features, such as conditional links.
+
## Conditional Links
-It is possible to add additional field-value-dependent conditions on links. For example, you may want certain links within a set to only appear if the application or session is in a state that allows that interaction.
+It is possible to add additional field-value-dependent conditions on links. For
+example, you may want certain links within a set to only appear if the
+application or session is in a state that allows that interaction.
-Let's begin with our `Person` example from earlier.
+A new model `Person` is defined below, a person has an `id`, a `name` and a
+collection of `items`. Moreover, a person could be locked, meaning no new items
+could be added, this is modeled by the `is_locked` flag. Each `Person` has three
+references, `self` (`href` for `URLFor`), `update` and `add_item`.
-```python
-class Person(HyperModel):
- id: str
- name: str
- items: List[ItemSummary]
- href = UrlFor("read_person", {"person_id": "