Response
class¶
esmerald.Response
¶
Response(content, *, status_code=HTTP_200_OK, media_type=None, background=None, headers=None, cookies=None, encoders=None)
Bases: Response
, Generic[T]
Default Response
object from Esmerald where it can be as the
return annotation of a handler.
Esmerakd automatically will understand what time of response is going to be used and parse all the details automatically.
Example
from pydantic import BaseModel
from esmerald import Esmerald, Gateway, Response, get
from esmerald.datastructures import Cookie
@get(path="/me")
async def home() -> Response:
return Response(
Item(id=1, sku="sku1238"),
headers={"SKY-HEADER": "sku-xyz"},
cookies=[Cookie(key="sku", value="a-value")],
)
Esmerald(routes=[Gateway(handler=home)])
PARAMETER | DESCRIPTION |
---|---|
content
|
Any content being sent to the response.
TYPE:
|
status_code
|
The response status code.
TYPE:
|
media_type
|
The media type used in the response.
TYPE:
|
background
|
Any instance of a BackgroundTask or BackgroundTasks.
TYPE:
|
headers
|
Any additional headers being passed to the response.
TYPE:
|
cookies
|
A sequence of Read more about the Cookies. Example
TYPE:
|
encoders
|
A sequence of Example
TYPE:
|
Source code in esmerald/responses/base.py
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 |
|
encoders
instance-attribute
¶
encoders = [encoder() if isclass(encoder) else encoder for encoder in encoders or _empty]
make_headers
¶
make_headers(content_headers=None)
Initializes the headers based on RFC specifications by setting appropriate conditions and restrictions.
PARAMETER | DESCRIPTION |
---|---|
content_headers
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
content_headers
|
Additional headers to include (default is None).
TYPE:
|
Source code in lilya/responses.py
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 |
|
set_cookie
¶
set_cookie(key, value='', *, path='/', domain=None, secure=False, max_age=None, expires=None, httponly=False, samesite='lax')
Sets a cookie in the response headers.
PARAMETER | DESCRIPTION |
---|---|
key
|
TYPE:
|
value
|
TYPE:
|
path
|
TYPE:
|
domain
|
TYPE:
|
secure
|
TYPE:
|
max_age
|
TYPE:
|
expires
|
TYPE:
|
httponly
|
TYPE:
|
samesite
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
key
|
The name of the cookie.
TYPE:
|
value
|
The value of the cookie.
TYPE:
|
path
|
The path for which the cookie is valid (default is '/').
TYPE:
|
domain
|
The domain to which the cookie belongs.
TYPE:
|
secure
|
If True, the cookie should only be sent over HTTPS.
TYPE:
|
max_age
|
The maximum age of the cookie in seconds.
TYPE:
|
expires
|
The expiration date of the cookie.
TYPE:
|
httponly
|
If True, the cookie should only be accessible through HTTP.
TYPE:
|
samesite
|
SameSite attribute of the cookie.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If samesite is not one of 'strict', 'lax', or 'none'. |
Source code in lilya/responses.py
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 |
|
delete_cookie
¶
delete_cookie(key, path='/', domain=None, secure=False, httponly=False, samesite='lax')
Deletes a cookie in the response headers by setting its max age and expiration to 0.
PARAMETER | DESCRIPTION |
---|---|
key
|
TYPE:
|
path
|
TYPE:
|
domain
|
TYPE:
|
secure
|
TYPE:
|
httponly
|
TYPE:
|
samesite
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
key
|
The name of the cookie to delete.
TYPE:
|
path
|
The path for which the cookie is valid (default is '/').
TYPE:
|
domain
|
The domain to which the cookie belongs.
TYPE:
|
secure
|
If True, the cookie should only be sent over HTTPS.
TYPE:
|
httponly
|
If True, the cookie should only be accessible through HTTP.
TYPE:
|
samesite
|
SameSite attribute of the cookie.
TYPE:
|
Source code in lilya/responses.py
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 203 204 |
|
message
¶
message(prefix)
PARAMETER | DESCRIPTION |
---|---|
prefix
|
TYPE:
|
Source code in lilya/responses.py
217 218 219 220 221 222 |
|
transform
staticmethod
¶
transform(value, *, encoders=None)
The transformation of the data being returned.
Supports all the default encoders from Lilya and custom from Esmerald.
PARAMETER | DESCRIPTION |
---|---|
value
|
TYPE:
|
encoders
|
TYPE:
|
Source code in esmerald/responses/base.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
make_response
¶
make_response(content)
PARAMETER | DESCRIPTION |
---|---|
content
|
TYPE:
|
Source code in esmerald/responses/base.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|