|
| 1 | +# Response Model |
| 2 | + |
| 3 | +Each route handler has key-value pair of status codes and a response model. |
| 4 | +This response model holds information on the type of response to be returned. |
| 5 | + |
| 6 | +```python |
| 7 | +# project_name/apps/items/controllers.py |
| 8 | + |
| 9 | +from ellar.serializer import Serializer |
| 10 | +from ellar.common import Controller, get |
| 11 | +from ellar.core import ControllerBase |
| 12 | + |
| 13 | +class UserSchema(Serializer): |
| 14 | + username: str |
| 15 | + email: str = None |
| 16 | + first_name: str = None |
| 17 | + last_name: str = None |
| 18 | + |
| 19 | + |
| 20 | +@Controller |
| 21 | +class ItemsController(ControllerBase): |
| 22 | + @get("/me", response=UserSchema) |
| 23 | + def me(self): |
| 24 | + return dict(username='Ellar', email='ellar@example.com') |
| 25 | +``` |
| 26 | + |
| 27 | +During route response computation, the `me` route handler response will evaluate to a |
| 28 | +`JSONResponseModel` with `UserSchema` as content validation schema. |
| 29 | + |
| 30 | +The resulting route responses will be: |
| 31 | + |
| 32 | +```python |
| 33 | +from ellar.serializer import Serializer |
| 34 | +from ellar.core.response.model import JSONResponseModel |
| 35 | + |
| 36 | + |
| 37 | +class UserSchema(Serializer): |
| 38 | + username: str |
| 39 | + email: str = None |
| 40 | + first_name: str = None |
| 41 | + last_name: str = None |
| 42 | + |
| 43 | + |
| 44 | +response = {200: JSONResponseModel(model_field_or_schema=UserSchema)} |
| 45 | +``` |
| 46 | + |
| 47 | +For documentation purposes, we can apply some `description` to the returned response |
| 48 | + |
| 49 | +```python |
| 50 | +@get("/me", response=(UserSchema, 'User Schema Response')) |
| 51 | +def me(self): |
| 52 | + return dict(username='Ellar', email='ellar@example.com') |
| 53 | +``` |
| 54 | +This will be translated to: |
| 55 | + |
| 56 | +```python |
| 57 | + |
| 58 | +response = {200: JSONResponseModel(model_field_or_schema=UserSchema, description='User Schema Response')} |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +!!! info |
| 64 | + Each route handler has its own `ResponseModel` computation and validation. If there is no response definition, Ellar default the route handler model to `EmptyAPIResponseModel`. |
| 65 | + |
| 66 | + |
| 67 | +## Override Response Type |
| 68 | + |
| 69 | +When you use a `Response` class as response, a `ResponseModel` is used and the `response_type` is replaced with applied response class. |
| 70 | + |
| 71 | +For example: |
| 72 | + |
| 73 | +```python |
| 74 | +# project_name/apps/items/controllers.py |
| 75 | + |
| 76 | +from ellar.common import Controller, get |
| 77 | +from ellar.core import ControllerBase |
| 78 | +from starlette.responses import PlainTextResponse |
| 79 | +from ellar.serializer import Serializer |
| 80 | + |
| 81 | + |
| 82 | +class UserSchema(Serializer): |
| 83 | + username: str |
| 84 | + email: str = None |
| 85 | + first_name: str = None |
| 86 | + last_name: str = None |
| 87 | + |
| 88 | + |
| 89 | +@Controller |
| 90 | +class ItemsController(ControllerBase): |
| 91 | + @get("/me", response={200: PlainTextResponse, 201: UserSchema}) |
| 92 | + def me(self): |
| 93 | + return "some text response." |
| 94 | +``` |
| 95 | +This will be translated to: |
| 96 | + |
| 97 | +```python |
| 98 | +from ellar.core.response.model import ResponseModel, JSONResponseModel |
| 99 | +from starlette.responses import PlainTextResponse |
| 100 | + |
| 101 | +response = {200: ResponseModel(response_type=PlainTextResponse), 201: JSONResponseModel(model_field_or_schema=UserSchema)} |
| 102 | +``` |
| 103 | + |
| 104 | +## Response Model Properties |
| 105 | + |
| 106 | +All response model follows `IResponseModel` contract. |
| 107 | + |
| 108 | +```python |
| 109 | +import typing as t |
| 110 | + |
| 111 | +from pydantic.fields import ModelField |
| 112 | +from ellar.core.context import IExecutionContext |
| 113 | +from ellar.core.response import Response |
| 114 | + |
| 115 | + |
| 116 | +class IResponseModel: |
| 117 | + media_type: str |
| 118 | + description: str |
| 119 | + get_model_field: t.Callable[..., t.Optional[t.Union[ModelField, t.Any]]] |
| 120 | + create_response: t.Callable[[IExecutionContext, t.Any], Response] |
| 121 | +``` |
| 122 | +Properties Overview: |
| 123 | + |
| 124 | +- `media_type`: Read from response media type. **Required** |
| 125 | +- `description`: For documentation purpose. Default: `Success Response`. **Optional** |
| 126 | +- `get_model_field`: returns response schema if any. **Optional** |
| 127 | +- `create_response`: returns a response for the client. **Optional** |
| 128 | + |
| 129 | +There is also a `BaseResponseModel` concrete class for more generic implementation. |
| 130 | +And its adds extra properties for configuration purposes. |
| 131 | + |
| 132 | +They include: |
| 133 | + |
| 134 | +- `response_type`: Response classes eg. JSONResponse, PlainResponse, HTMLResponse. etc. Default: `Response`. **Required** |
| 135 | +- `model_field_or_schema`: `Optional` property. For return data validation. Default: `None` **Optional** |
| 136 | + |
| 137 | + |
| 138 | +## Different Response Models |
| 139 | +Let's see different `ResponseModel` available in Ellar and how you can create one too. |
| 140 | + |
| 141 | +### **ResponseModel** |
| 142 | +Response model that manages rendering of other response types. |
| 143 | + |
| 144 | +- Location: `ellar.core.response.model.base.ResponseModel` |
| 145 | +- response_type: `Response` |
| 146 | +- model_field_or_schema: `None` |
| 147 | +- media_type: `text/plain` |
| 148 | + |
| 149 | +### **JSONResponseModel** |
| 150 | +Response model that manages `JSON` response. |
| 151 | + |
| 152 | +- Location: `ellar.core.response.model.json.JSONResponseModel` |
| 153 | +- response_type: `JSONResponse` OR `config.DEFAULT_JSON_CLASS` |
| 154 | +- model_field_or_schema: `Required` |
| 155 | +- media_type: `application/json` |
| 156 | + |
| 157 | +### **HTMLResponseModel** |
| 158 | +Response model that manages `HTML` templating response. see [`@render`]() decorator. |
| 159 | + |
| 160 | +- Location: `ellar.core.response.model.html.HTMLResponseModel` |
| 161 | +- response_type: `TemplateResponse` |
| 162 | +- model_field_or_schema: `None` |
| 163 | +- media_type: `text/html` |
| 164 | + |
| 165 | + |
| 166 | +### **FileResponseModel** |
| 167 | +Response model that manages `FILE` response. see [`@file`]() decorator. |
| 168 | + |
| 169 | +- Location: `ellar.core.response.model.file.FileResponseModel` |
| 170 | +- response_type: `FileResponse` |
| 171 | +- model_field_or_schema: `None` |
| 172 | +- media_type: `Required` |
| 173 | + |
| 174 | + |
| 175 | +### **StreamingResponseModel** |
| 176 | +Response model that manages `STREAMING` response. see [`@file`]() decorator. |
| 177 | + |
| 178 | +- Location: `ellar.core.response.model.file.StreamingResponseModel` |
| 179 | +- response_type: `StreamingResponse` |
| 180 | +- model_field_or_schema: `None` |
| 181 | +- media_type: `Required` |
| 182 | + |
| 183 | + |
| 184 | +### **EmptyAPIResponseModel** |
| 185 | +Default `ResponseModel` applied when no response is defined. |
| 186 | + |
| 187 | +- Location: `ellar.core.response.model.html.EmptyAPIResponseModel` |
| 188 | +- response_type: `JSONResponse` OR `config.DEFAULT_JSON_CLASS` |
| 189 | +- model_field_or_schema: `dict` |
| 190 | +- media_type: `application/json` |
| 191 | + |
| 192 | +## Custom Response Model |
| 193 | + |
| 194 | +Lets create a new JSON response model. |
| 195 | + |
| 196 | +```python |
| 197 | +# project_name/apps/items/controllers.py |
| 198 | + |
| 199 | +import typing as t |
| 200 | +from ellar.common import Controller, get |
| 201 | +from ellar.core import ControllerBase |
| 202 | +from ellar.core.response.model.base import ResponseModel |
| 203 | +from ellar.core.response import JSONResponse |
| 204 | +from dataclasses import dataclass |
| 205 | + |
| 206 | + |
| 207 | +@dataclass |
| 208 | +class NoteSchema: |
| 209 | + id: t.Union[int, None] |
| 210 | + text: str |
| 211 | + completed: bool |
| 212 | + |
| 213 | + |
| 214 | +class JsonApiResponse(JSONResponse): |
| 215 | + media_type = "application/vnd.api+json" |
| 216 | + |
| 217 | + |
| 218 | +class JsonApiResponseModel(ResponseModel): |
| 219 | + response_type = JsonApiResponse |
| 220 | + model_field_or_schema = t.List[NoteSchema] |
| 221 | + default_description = 'Successful JsonAPI Response' |
| 222 | + |
| 223 | + |
| 224 | +@Controller |
| 225 | +class ItemsController(ControllerBase): |
| 226 | + @get("/notes/", response=JsonApiResponseModel()) |
| 227 | + def get_notes(self): |
| 228 | + return [ |
| 229 | + dict(id=1, text='My Json Api Response 1', completed=True), |
| 230 | + dict(id=2, text='My Json Api Response 2', completed=True), |
| 231 | + ] |
| 232 | +``` |
| 233 | + |
| 234 | + |
0 commit comments