Esmerald framework - Highly scalable, performant, easy to learn, easy to code and for every sizeable and complex application
MIT License
Published by tarsil about 1 month ago
description
is provided from the handlers, it will read directly fromdict
or list
types. ThisNone
field which is now skipping for OpenAPI purposes.Now it is possible to do something like this:
from typing import Dict, List, Union, Optional
from esmerald import Gateway, JSONResponse, Query, get
@get("/item")
async def check_item(q: Union[List[str], None]) -> JSONResponse:
return JSONResponse({"value": q})
@get("/another-item")
async def check_item(q: Optional[Dict[str, str]]) -> JSONResponse:
return JSONResponse({"value": q})
!!! Warning
This is an additional functionality to the existing one and it does not represent any replacement. Be sure
you read the documentation and if you understand it.
As per some examples of the documentation:
from pydantic import BaseModel, EmailStr
from esmerald import Esmerald, Gateway, post
class User(BaseModel):
name: str
email: EmailStr
class Address(BaseModel):
street_name: str
post_code: str
@post("/create")
async def create_user(user: User, address: Address) -> None:
"""
Creates a user in the system and does not return anything.
Default status_code: 201
"""
app = Esmerald(routes=[Gateway(handler=create_user)])
You can expect to send a payload like this:
{
"user": {
"name": "John",
"email": "[email protected]",
},
"address": {
"street_name": "123 Queens Park",
"post_code": "90241"
}
}
More details can and must be read in the request data section.
status_code
in any response is now possible directly by specifying the intended response and ignoringhandler
.@get()
def create(name: Union[str, None]) -> Response:
if name is None:
return Response("Ok")
if name == "something":
return Response("Ok", status_code=status.HTTP_401_UNAUTHORIZED)
if name == "something-else":
return Response("Ok", status_code=status.HTTP_300_MULTIPLE_CHOICES)
If none of the conditions are met, then it will always default to the status_code
of the handler which in the get
case,
its 200
.
Published by tarsil 2 months ago
--context
allowing to generate application scaffolds containingPublished by tarsil 3 months ago
allow_private_networks
property to CORSMiddleware
.Gateway
now allows to pass also an optional operation_id
as parameter for OpenAPI documentation allowingoperation_id
is declared,operation_id
for it.View
to generate the routes in one place and reducingPublished by tarsil 3 months ago
This was missed from the release 3.3.4 and it was supposed to be included.
list
, List
, dict
and Dict
automatically parses the valuesPublished by tarsil 3 months ago
Query Parameters
and Path Parameters
.Extra, Advanced && Useful
is now renamed Advanced & Useful
and its located in the Features
Published by tarsil 3 months ago
ValidationError
for custom independent raising exceptions within any Esmerald applicationis_server_error
for dependencies was causing an exception to be raised in a loc[-1]
.Published by tarsil 3 months ago
Published by tarsil 3 months ago
from __future__ import annotation
when using the dependency injection defaulting to Any.Published by tarsil 3 months ago
PydanticEncoder
when checking for subclasses of BaseModel
causing the dependency injection to failpython-slugify
instead of awesome-slugify
for internals.app
as decorator prodiving another way of declaring the routing.#!/usr/bin/env python
import uvicorn
from esmerald import Esmerald, Gateway, JSONResponse, Request, get
app = Esmerald()
@app.get("/esmerald")
def welcome() -> JSONResponse:
return JSONResponse({"message": "Welcome to Esmerald"})
@app.get("/esmerald/{user}")
def user(user: str) -> JSONResponse:
return JSONResponse({"message": f"Welcome to Esmerald, {user}"})
@app.get("/esmerald/in-request/{user}")
def user_in_request(request: Request) -> JSONResponse:
user = request.path_params["user"]
return JSONResponse({"message": f"Welcome to Esmerald, {user}"})
if __name__ == "__main__":
uvicorn.run(app, port=8000)
The same is also applied to the Router
object.
Published by tarsil 4 months ago
globalise_settings
.AppSettingsModule
.Published by tarsil 4 months ago
XFrameOptionsMiddleware
to handle with options headers.SecurityMiddleware
adding various security headers to the request/response lifecycle.override_settings
as new decorator that allows to override the Lilya settings in any given test.--simple
flag for createproject
directive generating simple structured projects.rapidoc
as another alternative to display the OpenAPI docs.asyncz
decorator inside a wrapper.Published by tarsil 4 months ago
Published by tarsil 4 months ago
!!! danger
This new version deprecates an old style declaring the scheduler for Esmerald.
There is a new SchedulerConfig.
scheduler_config
parameter to Esmerald and EsmeraldAPISettings.scheduler_class
, scheduler_configurations
and scheduler_tasks
EsmeraldScheduler
.You must update the imports to be:
From:
from asyncz.contrib.esmerald.decorator import scheduler
To:
from esmerald.contrib.schedulers.asyncz.decorator import scheduler
Check the documentation about the Scheduler, handlers and the SchedulerConfig to
see how to update your current project to the new version with the minimum disruption.
This change should not break existing functionality, instead, its just an update to make it more modular.
There is an example how to simply use this.
--settings
into the runserver
directive.Published by tarsil 5 months ago
EsmeraldScheduler
integration with Asyncz is not assembled before the configuration of the routing,Published by tarsil 5 months ago
Published by tarsil 5 months ago
Context
is not inheriting directly from Lilya.scheduler_class
internal settings validation.Published by tarsil 5 months ago
settings_module
also supports import as string.encoders
to Esmerald settings and instance parameters.register_encoder
encoder in any Esmerald and ChildEsmerald instances.encoders
to Esmerald responses. This allows to use any Response as ASGI application.classmethods
of the TransformerModel
. The class methodsEsmeraldSignature
to SignatureModel
.Published by tarsil 6 months ago
This change was supposed to be shipped in the version 3.1.4 but it missed the release.
debug=True
providing a deeperPublished by tarsil 6 months ago