Basic Usage
Choose Hypermedia Formats
Fastapi-hypermodel has support for following Hypermedia Maturity Model Levels:
- Level 0: URLFor - Plain Text
- Level 1: Hypertext Application Language (HAL)
- Level 2: Siren
There is a fully working example for each format in the examples directory.
Initialization
Create Basic Models
Two showcase the hypermedia feature, an Item
model will be used. Each item
will have an id_
, a name
, an optional description
and a price
. Moreover
a ItemCollection
will also be defined to return multiple items. Two hypermedia
references will be used, one called self
(href
in the case of URLFor
) and
an update
.
All formats support "links", that is, plain references of HTTP URIs fetchable via GET. Moreover, Level 2 formats (SIREN) support "actions", which also specify the HTTP method and the fields needed.
Even though not part of the standard, fastapi-hypermodel provides support for "templated URIs". Allowing the client to form the URI with information from the selected resource. This is useful when returning collections.
Info
The reason to define two classes ItemSummary
and Item
is to enable using
a lightweight version (ItemSummary
) for nested objects
Define your data
Before defining the app and the endpoints, sample data should be defined. In this case all formats will use the same data.
In the case of HAL, to showcase the "cURIes" feature the data will change and
use sc:items
instead of items
as the key. At the moment only HAL supports
"cURIes" as part of the standard.
It is important to note that none of the additional fields added to the response at runtime are leaked into the data implementation. Therefore, the hypermedia format and the data model are totally decoupled, granting great flexibility.
Create and Attach App
To make the app "hypermedia-aware", it is enough to initiliaze the format's HyperModel class with the app object.
Warning
At the moment this is handled by class variables so it is not thread-safe to have multiple apps.
Add API Endpoints
To expose the data via endpoints, they are defined as usual in any FastAPI app.
The response_model
and response_class
need to be defined when appropiate.
All formats are compatible with path parameters. In the case of Level 2 formats (SIREN), it can auto detect path and body parameters as well. Query parameters are not well supported yet.
Responses
The response generated by each format varies based on their specification. Using hypermedia usually results in heavier responses because of all the additional information provided.
Warning
At the moment no optimizations are done under the hood to minimize the size of the response. For instance, one such optimization could be removing cURIes in HAL if they are already defined in a parent.
Beware of highly nested objects.
Fetching /items/item01
Fetching /items
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 |
|
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|