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.
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
.
The condition
argument takes a callable, which will be passed a dict
containing the name-to-value mapping of all fields on the base HyperModel
instance. In this example, a lambda function that returns True
or False
depending on the value is_locked
of HyperModel
instance.
Note
Conditional links will always show up in the auto-generated OpenAPI/Swagger documentation. These conditions only apply to the hypermedia fields generated at runtime.
Response for locked Person
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
Response for unlocked Person
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|