Package: sanic-boom
Components injection, fast routing and non-global (layered) middlewares. Give your Sanic application a Boom!In a nutshell"""Example code taken from
https://marshmallow.readthedocs.io/en/3.0/quickstart.html#quickstart
"""importdatetimeasdtimportinspectimporttypingastfrommarshmallowimportSchema,fields,post_loadfromsanic.exceptionsimportServerErrorfromsanic.responseimporttextfromsanic_boomimportComponent,SanicBoom# --------------------------------------------------------------------------- ## marshmallow related code# --------------------------------------------------------------------------- #classUser(object):def__init__(self,name,email):self.name=nameself.email=emailself.created_at=dt.datetime.now()def__repr__(self):return"<User(name={self.name!r})>".format(self=self)defsay_hi(self):return"hi, my name is{}".format(self.name)classUserSchema(Schema):name=fields.Str()email=fields.Email()created_at=fields.DateTime()@post_loaddefmake_user(self,data):returnUser(**data)# --------------------------------------------------------------------------- ## sanic-boom related code# --------------------------------------------------------------------------- #classJSONBody(t.Generic[t.T_co]):passclassJSONBodyComponent(Component):defresolve(self,param:inspect.Parameter)->bool:ifhasattr(param.annotation,"__origin__"):returnparam.annotation.__origin__==JSONBodyreturnFalseasyncdefget(self,request,param:inspect.Parameter)->object:inferred_type=param.annotation.__args__[0]try:returninferred_type().load(request.json).dataexceptException:raiseServerError("Couldn't convert JSON body to{!s}".format(inferred_type))app=SanicBoom(__name__)app.add_component(JSONBodyComponent)@app.post("/")asyncdefhandler(user:JSONBody[UserSchema]):# notice the handler parametersreturntext(user.say_hi())if__name__=="__main__":app.run(host="0.0.0.0",port=8000,workers=1)$ curl -v http://localhost:8000/ -d '{"name":"John Doe","email":"john.doe@example.tld"}'
*   Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 8000 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8000 (#0)
> POST / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.61.1
> Accept: */*
> Content-Length: 50
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 50 out of 50 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< Keep-Alive: 5
< Content-Length: 23
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
hi, my name is John DoeWarningIMPORTANT:sanic-boomis invery early stagesof development! Use with caution and be aware that some functionalities and APIs may change between versions until they’re out ofalpha.Dependenciessanic-boomdepends on two “not-so-known” libraries (both created by the author ofsanic-boom):sanic-ipware; andxrtrImportantSincexrtrreplacesthe Sanic default router under the hood insanic-boom, it is very important for the developer to read its documentation (in the link provided above).Documentationhttps://sanic-boom.readthedocs.io/en/latest/Licensesanic-boomis a free software distributed under theMITlicense.Changelogv0.1.2 on 2018-10-23Addedcomponentsproperty onBoomRequestso any request “leftover” may be handled properly (like an open database connection).v0.1.1 on 2018-10-18Fixed a bug where handlers derived fromHTTPMethodViewclass were not being executed (for their signature actually be*args, **kwargs).v0.1.0 on 2018-10-17First release on PyPI. (Probably) not stable.