当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

flask-rest-jsonapi

授权协议 MIT License
开发语言 Python
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 印振国
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Flask-REST-JSONAPI

Flask-REST-JSONAPI is a flask extension for building REST APIs. It combines the power of Flask-Restless and the flexibility of Flask-RESTful around a strong specification JSONAPI 1.0. This framework is designed to quickly build REST APIs and fit the complexity of real life projects with legacy data and multiple data storages.

Install

pip install Flask-REST-JSONAPI

A minimal API

# -*- coding: utf-8 -*-

from flask import Flask
from flask_rest_jsonapi import Api, ResourceDetail, ResourceList
from flask_sqlalchemy import SQLAlchemy
from marshmallow_jsonapi.flask import Schema
from marshmallow_jsonapi import fields

# Create the Flask application and the Flask-SQLAlchemy object.
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)

# Create model
class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

# Create the database.
db.create_all()

# Create schema
class PersonSchema(Schema):
    class Meta:
        type_ = 'person'
        self_view = 'person_detail'
        self_view_kwargs = {'id': '<id>'}
        self_view_many = 'person_list'

    id = fields.Integer(as_string=True, dump_only=True)
    name = fields.Str()

# Create resource managers
class PersonList(ResourceList):
    schema = PersonSchema
    data_layer = {'session': db.session,
                  'model': Person}

class PersonDetail(ResourceDetail):
    schema = PersonSchema
    data_layer = {'session': db.session,
                  'model': Person}

# Create the API object
api = Api(app)
api.route(PersonList, 'person_list', '/persons')
api.route(PersonDetail, 'person_detail', '/persons/<int:id>')

# Start the flask loop
if __name__ == '__main__':
    app.run()

This example provides the following API structure:

URL method endpoint Usage
/persons GET person_list Get a collection of persons
/persons POST person_list Create a person
/persons/<int:person_id> GET person_detail Get person details
/persons/<int:person_id> PATCH person_detail Update a person
/persons/<int:person_id> DELETE person_detail Delete a person

Flask-REST-JSONAPI vs Flask-RESTful

  • In contrast to Flask-RESTful, Flask-REST-JSONAPI provides a default implementation of get, post, patch and delete methods around a strong specification JSONAPI 1.0. Thanks to this you can build REST API very quickly.
  • Flask-REST-JSONAPI is as flexible as Flask-RESTful. You can rewrite every default method implementation to make custom work like distributing object creation.

Flask-REST-JSONAPI vs Flask-Restless

  • Flask-REST-JSONAPI is a real implementation of JSONAPI 1.0 specification. So in contrast to Flask-Restless, Flask-REST-JSONAPI forces you to create a real logical abstration over your data models with Marshmallow. So you can create complex resource over your data.
  • In contrast to Flask-Restless, Flask-REST-JSONAPI can use any ORM or data storage through the data layer concept, not only SQLAlchemy. A data layer is a CRUD interface between your resource and one or more data storage so you can fetch data from any data storage of your choice or create resource that use multiple data storages.
  • Like I said previously, Flask-REST-JSONAPI is a real implementation of JSONAPI 1.0 specification. So in contrast to Flask-Restless you can manage relationships via REST. You can create dedicated URL to create a CRUD API to manage relationships.
  • Plus Flask-REST-JSONAPI helps you to design your application with strong separation between resource definition (schemas), resource management (resource class) and route definition to get a great organization of your source code.
  • In contrast to Flask-Restless, Flask-REST-JSONAPI is highly customizable. For example you can entirely customize your URLs, define multiple URLs for the same resource manager, control serialization parameters of each method and lots of very useful parameters.
  • Finally in contrast to Flask-Restless, Flask-REST-JSONAPI provides a great error handling system according to JSONAPI 1.0. Plus the exception handling system really helps the API developer to quickly find missing resources requirements.

Documentation

Documentation available here: http://flask-rest-jsonapi.readthedocs.io/en/latest/

Thanks

Flask, marshmallow, marshmallow_jsonapi, sqlalchemy, Flask-RESTful and Flask-Restless are awesome projects. These libraries gave me inspiration to create Flask-REST-JSONAPI, so huge thanks to authors and contributors.

  • 这篇算是翻译、摘录和转写,原文:Headless CMS: REST vs JSON:API vs GraphQL,是Drupal项目的创始人Dries Buytaert和他同事一起在2019初写的,有很多跟Drupal相关的元素,但是质量很高,极具参考价值,所以我把其中跟Drupal相关的大部分去掉,然后把架构相关的摘录、翻译过来,并加入一些我自己的关于Python、Django、Flask开发

  • flask-restful中自定义json数据的方法 我们在实际开发中, 返回的JSON数据中除了包含基础数据, 往往还需要设置一些 统一的外层包装, 以便前端进行更好的解析处理, 如: { "message": "ok", // 外层包装 "code": 200, // 外层包装 "data": { // 基础数据 "name": "张三",

  • Flask-Restful 1.安装 pip install Flask-Restful 2.注册组件 from flask_restful import Api rest_api=Api() rest_api.init_app(app) 3.GET/POST/PUT/DELETE请求 from flask_restful import Resource class PostApi(Resourc

  • 原文链接: flask REST API. 上一篇: es 6 对象操作 下一篇: node axios 使用 http://flask-restful.readthedocs.io/en/0.3.5/quickstart.html#full-example Flask-RESTful 提供了一个 Resource 基础类,它能够定义一个给定 URL 的一个或者多个 HTTP 方法。 add_re

  • RESTful flask-restful: 我他么的,蓝图最终还是不能在restful中使用。。不过也不需要蓝图来使用。。一样可以拆分为多个,但是不能像java一样,一个函数响应两个访问方法。 在新建立一个api类,继承自Resource。在里面写 get/post方法 然后通过 api.add_resource(HelloResource,'/hello') 来写下来路由。 注意事项: ret

  • Flask-RESTful 的介绍和基本使用 Flask-RESTful 是一个 Flask 扩展,它添加了快速构建 REST API 的支持。 环境安装:(终端执行) pip install flask-restful 文档: 点我查看官方文档 点我查看中文翻译 构建RESTAPI 使用 flask-restful 构建RESTAPI只需要进行三步操作 创建扩展/组件对象 组件对象 = Api

  • 前言 在校验请求参数的时候,除了一些基本的required=True, type类型外,还会遇到一些校验,比如是否为空,字符串长度,以及一些自定义的参数规则。 add_argument 参数 下面是add_argument 可以使用的参数,部分源码如下: class Argument(object): """ :param name: Either a name or a lis

  • 2000年,Roy Thomas Fielding博士在他的博士论文《Architectural Styles and the Design of Network-based Software Architectures》中提出了几种软件应用的架构风格,REST作为其中的一种架构风格在这篇论文中进行了概括性的介绍。 REST:Representational State Transfer的缩写,翻

  • 安装 pip install flask-restful 手册参照 https://flask-restful.readthedocs.io/en/latest/ RESTful(表现层状态转化) adds support for quickly building REST APIs rest api是前后端分离的最佳实践,是开发的一套标准或规范,不是框架。 1、轻量,直接通过http,不需要额外

  • Flask-RESTful 是一个 Flask 扩展,它添加了快速构建 REST API 的支持。 安装 pip install flask-restful  基本使用 # user_flask_restful_get_base_test.py from flask import Flask,jsonify from flask_restful import Api,Resource app

 相关资料
  • REST APIs with Flask and Python This repository contains code that is created in my course, REST APIs with Flask and Python. The code is divided in folders, one for each section of the course which co

  • 问题内容: 在官方的快速入门中,建议在使用单个 模块 时使用: 2. …如果您使用的是单个模块(如本例所示),则应使用,因为取决于它是作为应用程序启动还是作为模块导入,其名称将有所不同(与实际导入名称不同)。… 但是,在他们的API文档中,当我的应用程序为 软件包 时,建议进行硬编码: 因此,您在此处提供的内容很重要。如果使用单个模块,则始终为正确的值。但是,如果您使用的是包,通常建议在其中硬编码

  • 在前面,我们介绍了 REST Web 服务,并使用 Flask 提供服务。这里,我们使用第三方库 Flask-RESTful,它使得在 Flask 中提供 REST 服务变得更加简单。 安装 使用 pip 安装: $ pip install flask-restful 使用 下面我们主要使用官方文档的例子进行说明。 Hello World 我们先来看一个简单的例子。 # -*- coding: u

  • Bootstrap 是 Twitter 开源的一个 CSS/HTML 框架,它让 Web 开发变得更加迅速,简单。要想在我们的 Flask 应用中使用 Boostrap,有两种方案可供选择: 第 1 种,在我们的 Jinja 模板中直接引入 Bootstrap 层叠样式表 (CSS) 和 JavaScript 文件,比如 bootstrap.min.css,bootstrap.min.js; 第

  • 在 Web 应用中,我们经常需要保护我们的 api,以避免非法访问。比如,只允许登录成功的用户发表评论等。Flask-HTTPAuth 扩展可以很好地对 HTTP 的请求进行认证,不依赖于 Cookie 和 Session。本文主要介绍两种认证的方式:基于密码和基于令牌 (token)。 安装 使用 pip 安装: $ pip install Flask-HTTPAuth 基于密码的认证 为了简化

  • 假设你的 Web 服务对于某些请求比较耗时,而该请求的返回结果在较短的时间内(比如 5 分钟内)都是足够有效的,这时你能想到什么方法去改善这种状况呢?缓存?对,至少这是一种提高性能的最简单的方法。 Flask 本身不提供缓存功能,但是作为 Flask 核心的 Werkzeug 框架则提供了一个简单的缓存对象 SimpleCache,它将缓存项存放在 Python 解释器的内存中。使用 Simple