Git Product home page Git Product logo

flask_autorest's Introduction

BPIT_flask_autorest

自动生成模型restfulapi--陈嘉琅

Autorest

Author: galain1994
Reference: Restless

Design

  • workflow: Restful API
-> request -> restapi ->

|-> get |-> query     -> serialize
        |-> instance

|-> put -> instance_id -> deserialize(update)

|-> post -> deserialize(create)

|-> delete -> instance_id -> instance.delete/session.delete

APIManager

  • Features:
    • Singleton
    • Manage Blueprints
    • Control Views

Serializer

  • Serializer:
serialize: instance -> dict -> converter -> filter -> result
  • Deserializer:
deserialize: dict -> filters -> validators -> converters ->
             -> get_by_id |-> instance(exist) -> update
                          |-> instance -> create
  • Filters: 过滤字典的字段(也可用于serializer中)

    • IncludeFilter 从字典中筛选所需要的字段
    • ExcludeFilter 从字典中过滤所需要的字段
  • params:

    • filter_list: 过滤的字段列表
    • name: 过滤器名称(可为None)
    • global_fields: 全局字段

Usage

Serializer


class A(model):

    b_ids -> [b1, b2, b3]
    c_ids -> [c1, c2, c3]


class B(model):

    a_id -> a
    d_ids -> [d1, d2, d3]


class C(model):

    a_ids -> [a1, a2, a3]


class D(model):

    b_id -> b

Filter

IncludeFilter

data = {
    "f1": "v1",
    "f2": {
        "f3": "v3",
        "f4": "v4"
    }
}
>>> include_filter1 = IncludeFilter(['f1', 'f2'])
>>> ret = include_filter1.filter(data)
{
    "f1": "v1",
    "f2": {
        "f3": "v3",
        "f4": "v4"
    }
}
>>> include_filter2 = IncludeFilter(['f1', 'f2.'])
>>> ret = include_filter2.filter(data)
{
    "f1": "v1",
    "f2": {}
}
>>> include_filter3 = IncludeFilter(['f1', 'f2.f3'])
>>> ret = include_filter3.filter(data)
{
    "f1": "v1",
    "f2": {
        "f3": "v3"
    }
}

ExcludeFilter

data = {
    "f1": "v1",
    "f2": {
        "f3": "v3",
        "f4": "v4"
    }
}

>>> exclude_filter1 = ExcludeFilter(['f2', ])
>>> ret = exclude_filter1.filter(data)
{
    "f1": "v1"
}
>>> exclude_filter2 = ExcludeFilter(['f2.'])
{
    "f1": "v1",
    "f2": {
        "f3": "v3",
        "f4": "v4"
    }
}

对于instance对象具有同样的过滤效果


Testing

current directory: BPIT_flask_services

  • Run Certain Test Function
pytest base/autorest/test_autorest.py::TestMultiModel::test_put -vv
  • Run all Tests
pytest base -vv

flask_autorest's People

Watchers

Castell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.