responder v2.0.0 has been released on 2019-10-19 (JST)! It's been a while, but I've investigated the changes from v1 so I'll introduce them.
By the way, it seems that backward compatibility from v1.3.2 is secured, so there is no problem even if you upgrade to v2.0.5.
This time, we will only take up the changes from the v1 system. ** For more details, please refer to For getting started with Python responder ... Preliminary research --Qiita! ** **
The notation of Schema and Template has changed! Also, refactoring around the Router was done (honestly not relevant to users).
Up until now, only one import was required, and the advantage of being able to do anything with one instance was good or bad.
import responder
api = responder.API()
However, the definition of Scheme and Template also required ʻapi`, and file separation was a little troublesome. In the new v2, these are separated, making file separation a bit easier.
Router Refactoring has been done. I don't think there are any changes on the user side so far.
Schema
A new import of responder.ext.schema
(such as from responder.ext.schema import Schema as OpenAPISchema
) is required.
However, this still depends on ʻapi when generating the
responder.ext.schema.Schema` instance, and this instance is required when defining each schema, so file isolation is still tricky ...
New way of writing
import responder
from responder.ext.schema import Schema as OpenAPISchema
from marshmallow import Schema, fields
contact = {
"name": "API Support",
"url": "http://www.example.com/support",
"email": "[email protected]",
}
license = {
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
}
api = responder.API()
schema = OpenAPISchema(
app=api,
title="Web Service",
version="1.0",
openapi="3.0.2",
docs_route='/docs', #When providing documents
description="A simple pet store",
terms_of_service="http://example.com/terms/",
contact=contact,
license=license,
)
@schema.schema("Pet")
class PetSchema(Schema):
name = fields.Str()
@api.route("/")
def route(req, resp):
"""A cute furry animal endpoint.
---
get:
description: Get a random pet
responses:
200:
description: A pet to be returned
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
"""
resp.media = PetSchema().dump({"name": "little orange"})
In addition, this also seems to work with the conventional writing method, but it is not recommended, so the existing writing method is omitted.
Template In addition to the existing template function, it can be implemented as follows:
Newly separated template function
from responder.templates import Templates
templates = Templates()
@api.route("/hello/{name}/html")
def hello(req, resp, name):
resp.html = templates.render("hello.html", name=name)
It also supports asynchronous rendering render_async
:
Asynchronous rendering of templates
from responder.templates import Templates
templates = Templates(enable_async=True)
resp.html = await templates.render_async("hello.html", name=name)
Existing patterns are also available:
Existing template function
import responder
api = responder.API()
@api.route("/hello/{name}/html")
def hello_html(req, resp, *, who):
resp.html = api.template('hello.html', name=name)
Starting with v2.0.5, it supports Python 3.8.
It may be easier to implement medium-sized web applications.
Recommended Posts