Getting Started with Python responder v2

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.

Target audience

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! ** **

New Router and Schema (and Template)

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)

Support for Python 3.8

Starting with v2.0.5, it supports Python 3.8.

Afterword

It may be easier to implement medium-sized web applications.

reference

Recommended Posts

Getting Started with Python responder v2
1.1 Getting Started with Python
Getting Started with Python
Getting Started with Python
Getting Started with Python Functions
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Python Basics of Python
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Python Web Scraping Practice
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Django 1.11 started with Python3.6
Getting started with Android!
Getting Started with Golang 2
Getting started with apache2
Getting Started with Golang 1
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Getting started with Python with 100 knocks on language processing
[Translation] Getting Started with Rust for Python Programmers
Getting started with AWS IoT easily in Python
Materials to read when getting started with Python
Settings for getting started with MongoDB in python
Translate Getting Started With TensorFlow
Getting Started with Tkinter 2: Buttons
Getting Started with Go Assembly
Getting Started with PKI with Golang ―― 4
Get started with Python! ~ ② Grammar ~
Getting Started with Django with PyCharm
Getting Started with Google App Engine for Python & PHP
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Getting Started with python3 # 3 Try Advanced Computations Using Import Statements
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
How to get started with Python
Getting Started with Mathematics Starting with Python Programming Challenges Personal Notes-Problem 1-1
Getting Started with Cisco Spark REST-API
started python
Getting started with USD on Windows
Get started with Python in Blender