Skip to content

The wings-sanic is a lightweight python framework aimed at making it as simple as possible to document your Sanic API with Swagger UI, Plus param validation and model serialization.

Notifications You must be signed in to change notification settings

forencen/wings-sanic

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wings-sanic

Welcome to wings-sanic README file.

The wings-sanic is a lightweight python framework aimed at making it as simple as possible to document your Sanic API with Swagger UI, Plus param validation and model serialization.

What is wings-sanic

The wings-sanic(required Python3.5+) is lightweight python framework for sainc. This framework provides:

  • declarative generation of http handler interfaces by parsing function annotations
  • validation and serialization to and from a json.
  • validation and serialization to and from native python objects.
  • validation and serialization from complex object.
  • autodocumentation of all handlers generated by this way, via swagger.

Installation

$ pip install wings-sanic

Quick Start

Overview:

Image text

Simple example

import wings_sanic
from wings_sanic import application, settings

# -----------  user settings -------------
dev_settings = {
    'DEFAULT_CONTEXT': {
        'response_shape': 'wings_sanic.views.ResponseShapeCodeDataMsg'
    },

    'SWAGGER': {
        'info': {
            "version": '1.0.0',
            "title": 'Authors API'
        },
        'schemes': ['http']
    },
    'HTTP_PORT': 8080,
    'DEBUG': True,
    'DEV': True,
    'CORS': True
}
settings.load(**dev_settings)

authors = wings_sanic.WingsBluePrint('authors', url_prefix='api/authors')


class Author(wings_sanic.Serializer):
    id = wings_sanic.IntField('Id', read_only=True)
    name = wings_sanic.StringField("姓名", required=True)
    phone = wings_sanic.PhoneField('电话', required=True)
    password = wings_sanic.StringField('密码', required=True, write_only=True)


authors_db = {
    1: {'name': '张三', 'phone': '17788661234', 'id': 1, 'password': '123456'}
}


def get_author_id():
    author_id = getattr(get_author_id, 'author_id', 1) + 1
    setattr(get_author_id, 'author_id', author_id)
    return author_id


@authors.get('/', response_serializer=Author(many=True))
async def list_authors(request, *args, **kwargs):
    """
    authors列表
    返回authors列表,不分页
    """
    return authors_db.values()


@authors.post('/', body_serializer=Author(), response_serializer=Author())
async def create_author(request, body, *args, **kwargs):
    """
    新建author
    """
    author_id = get_author_id()
    body['id'] = author_id
    authors_db[author_id] = body
    return body


@authors.get('/<author_id>/',
             path_params={'author_id': wings_sanic.IntField('作者Id')},
             response_serializer=Author())
async def author_detail(request, author_id, *args, **kwargs):
    """
    获取指定id的author详情
    """
    return authors_db.get(author_id, None)


# --------------------- main -----------------
if __name__ == '__main__':
    application.app.blueprint(authors)
    application.start()

Run and then you can get swagger documents by browser http//:0.0.0.0:8080/swagger/

Sample Project

You can start a project from template_main

More, a sample project prepared for you.

Document

Todo

About

The wings-sanic is a lightweight python framework aimed at making it as simple as possible to document your Sanic API with Swagger UI, Plus param validation and model serialization.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Python 98.1%
  • HTML 1.9%