All Projects → rantav → Flask Restful Swagger

rantav / Flask Restful Swagger

Licence: mit
A Swagger spec extractor for flask-restful

Programming Languages

javascript
184084 projects - #8 most used programming language
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Flask Restful Swagger

Docker Flask Mongodb Example
Uses docker compose with a python flask microservice and MongoDB instance to make a sample application
Stars: ✭ 49 (-92.21%)
Mutual labels:  swagger, flask
Mentorship Backend
Mentorship System is an application that matches women in tech to mentor each other, on career development, through 1:1 relations during a certain period of time. This is the backend of this system.
Stars: ✭ 132 (-79.01%)
Mutual labels:  swagger, flask
Flask Restx
Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
Stars: ✭ 1,050 (+66.93%)
Mutual labels:  swagger, flask
Apispec
A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..
Stars: ✭ 831 (+32.11%)
Mutual labels:  swagger, flask
Flask Api Starter Kit
Start a Flask API in less than 5 minutes
Stars: ✭ 296 (-52.94%)
Mutual labels:  swagger, flask
Flask Restplus Server Example
Real-life RESTful server example on Flask-RESTplus
Stars: ✭ 1,240 (+97.14%)
Mutual labels:  swagger, flask
Bhagavadgita
A non-profit initiative to help spread the transcendental wisdom from the Bhagavad Gita to people around the world.
Stars: ✭ 84 (-86.65%)
Mutual labels:  swagger, flask
Flask Restplus
Fully featured framework for fast, easy and documented API development with Flask
Stars: ✭ 2,585 (+310.97%)
Mutual labels:  swagger, flask
Safrs
SqlAlchemy Flask-Restful Swagger Json:API OpenAPI
Stars: ✭ 255 (-59.46%)
Mutual labels:  swagger, flask
Flasgger
Easy OpenAPI specs and Swagger UI for your Flask API
Stars: ✭ 2,825 (+349.13%)
Mutual labels:  swagger, flask
Full Stack
Full stack, modern web application generator. Using Flask, PostgreSQL DB, Docker, Swagger, automatic HTTPS and more.
Stars: ✭ 451 (-28.3%)
Mutual labels:  swagger, flask
Mini Shop Server
基于 Flask 框架开发的微信小程序后端项目,用于构建小程序商城后台 (电商相关;rbac权限管理;附带自动生成Swagger 风格的API 文档;可作「Python 项目毕设」;慕课网系列)---- 相关博客链接:🌟
Stars: ✭ 446 (-29.09%)
Mutual labels:  swagger, flask
Swagger Py Codegen
a Python web framework generator supports Flask, Tornado, Falcon, Sanic
Stars: ✭ 508 (-19.24%)
Mutual labels:  swagger, flask
Fxtest
接口自动化测试平台——python+flask版,支持http协议,java 版本开发完毕https://github.com/liwanlei/plan
Stars: ✭ 564 (-10.33%)
Mutual labels:  flask
Flask Caching
A caching extension for Flask
Stars: ✭ 582 (-7.47%)
Mutual labels:  flask
Swagger Stats
API Observability. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices.
Stars: ✭ 559 (-11.13%)
Mutual labels:  swagger
Osroom
[很多问题在维修中, 作者疫情期间忙碌,很多问题无法及时处理] 准备v3.0 Beta, v2.2维护少| OS鹿 | Python Flask开源网站
Stars: ✭ 557 (-11.45%)
Mutual labels:  flask
Eliasdb
EliasDB a graph-based database.
Stars: ✭ 611 (-2.86%)
Mutual labels:  swagger
Open Source Saas Boilerpate
Free SaaS boilerplate (Python/PostgreSQL/ReactJS/Webpack)
Stars: ✭ 582 (-7.47%)
Mutual labels:  flask
Cookiecutter Flask Restful
Flask cookiecutter template for builing APIs with flask-restful, including JWT auth, cli, tests, swagger, docker and more
Stars: ✭ 556 (-11.61%)
Mutual labels:  flask

flask-restful-swagger

flask_restful_swagger-automation
flask_restful_swagger-automation

What is flask-restful-swagger?

flask-restful-swagger is a wrapper for flask-restful which enables swagger support.

In essence, you just need to wrap the Api instance and add a few python decorators to get full swagger support.

Installation:

Install:

pip install flask-restful-swagger

(This installs flask-restful as well)

See Some Quick Examples:

PYTHONPATH=. python examples/basic.py
PYTHONPATH=. python examples/blueprints.py
PYTHONPATH=. python examples/inheritance.py

Browse to: http://localhost:5000/api/spec.html

Contributing, and Making Improvements:

How To:

In your program, where you'd usually just use flask-restful, add just a little bit of sauce and get a swagger spec out.

from flask import Flask
from flask_restful import Api
from flask_restful_swagger import swagger

app = Flask(__name__)

###################################
# Wrap the Api with swagger.docs. It is a thin wrapper around the Api class that adds some swagger smarts
api = swagger.docs(Api(app), apiVersion='0.1')
###################################


# You may decorate your operation with @swagger.operation
class Todo(Resource):
    "Describing elephants"
    @swagger.operation(
        notes='some really good notes',
        responseClass=ModelClass.__name__,
        nickname='upload',
        parameters=[
            {
              "name": "body",
              "description": "blueprint object that needs to be added. YAML.",
              "required": True,
              "allowMultiple": False,
              "dataType": ModelClass2.__name__,
              "paramType": "body"
            }
          ],
        responseMessages=[
            {
              "code": 201,
              "message": "Created. The URL of the created blueprint should be in the Location header"
            },
            {
              "code": 405,
              "message": "Invalid input"
            }
          ]
        )
    def get(self, todo_id):
    
# Operations not decorated with @swagger.operation do not get added to the swagger docs

class Todo(Resource):
    def options(self, todo_id):
        """
        I'm not visible in the swagger docs
        """
        pass


# Then you add_resource as you usually would

api.add_resource(TodoList, '/todos')
api.add_resource(Todo, '/todos/<string:todo_id>')

# You define models like this:
@swagger.model
class TodoItem:
  "A description ..."
  pass

# Swagger json:
    "models": {
        "TodoItemWithArgs": {
            "description": "A description...",
            "id": "TodoItem",
        },

# If you declare an __init__ method with meaningful arguments then those args could be used to deduce the swagger model fields.
@swagger.model
class TodoItemWithArgs:
  "A description ..."
  def __init__(self, arg1, arg2, arg3='123'):
    pass

# Swagger json:
    "models": {
        "TodoItemWithArgs": {
            "description": "A description...",
            "id": "TodoItem",
            "properties": {
                "arg1": {
                    "type": "string"
                },
                "arg2": {
                    "type": "string"
                },
                "arg3": {
                    "default": "123",
                    "type": "string"
                }
            },
            "required": [
                "arg1",
                "arg2"
            ]
        },


# Additionally, if the model class has a `resource_fields` class member then flask-restful-swagger is able to deduce the swagger spec by this list of fields.

@swagger.model
class TodoItemWithResourceFields:
  resource_fields = {
      'a_string': fields.String
  }

# Swagger json:
    "models": {
        "TodoItemWithResourceFields": {
            "id": "TodoItemWithResourceFields",
            "properties": {
                "a_string": {
                    "type": "string"
                },
            }
        }

# And in order to close the loop with flask-restify you'd also need to tell flask-restify to @marshal_with the same list of fields when defining your methods.
# Example:

@marshal_with(TodoItemWithResourceFields.resource_fields)
def get()
  return ...

Using @marshal_with

Let us recap usage of @marshal_with. flask-restful has a decorator @marshal_with. With the following setup it's possible to define the swagger model types with the same logic as @marshal_with.

You have to:

# Define your model with resource_fields
@swagger.model
class TodoItemWithResourceFields:
  resource_fields = {
      'a_string': fields.String,
      'a_second_string': fields.String(attribute='a_second_string_field_name')
  }

# And use @marshal_with(YourClass.resource_fields):
@marshal_with(TodoItemWithResourceFields.resource_fields)
def get()
  return ...

Running and testing

Now run your flask app

python example.py

And visit:

curl http://localhost:5000/api/spec.json

Passing more metadata to swagger

When creating the swagger.docs object you may pass additional arguments, such as the following:

api_spec_url - where to serve the swagger spec from. Default is /api/spec. This will make the json
available at /api/spec as well as /api/spec.json and will also present a nice interactive
HTML interface at /api/spec.html

apiVersion - passed directly to swagger as the apiVersion attribute. Default: 0.0

basePath - passed directly to swagger as the basePath attribute. Default: 'http://localhost:5000' (do not include a slash at the end)

resourcePath - same as before. default: '/'

produces - same as before, passed directly to swagger. The default is ["application/json"]

swaggerVersion - passed directly to swagger. Default: 1.2

description - description of this API endpoint. Defaults to 'Auto generated API docs by flask-restful-swagger'

Accessing the result json spec and an Interactive HTML interface

Assuming you provided swagger.docs with a parameter api_spec_url='/api/spec' (or left out in which case the default is '/api/spec') you may access the resulting json at /api/spec.json. You may also access /api/spec.html where you'd find an interactive HTML page that lets you play with the API to some extent.

Here's how this HTML page would look like:

An example /api/spec.html page

Accessing individual endpoints (.help.json)

flask-restful-swagger adds some useful help pages (well, json documents) to each of your resources. This isn't part of the swagger spec, but could be useful anyhow. With each endpoint you register, there's also an automatically registered help endpoint which ends with a .help.json extension. So for example when registering the resource api.add_resource(TodoList, '/todos') you may access the actual api through the url /todos and you may also access the help page at /todos.help.json. This help page spits out the relevant json content only for this endpoint (as opposed to /api/spec.json which spits out the entire swagger document, which could be daunting)

Example:

### python:

> api.add_resource(TodoList, '/todos')

### Shell:

$ curl localhost:5000/todos.help.json
{
    "description": null,
    "operations": [
        {
            "method": "GET",
            "nickname": "nickname",
            "parameters": [],
            "summary": null
        },
        {
            "method": "POST",
            "nickname": "create",
            "notes": "Creates a new TODO item",
            "parameters": [
                {
                    "allowMultiple": false,
                    "dataType": "TodoItem",
                    "description": "A TODO item",
                    "name": "body",
                    "paramType": "body",
                    "required": true
                }
            ],
            "responseClass": "TodoItem",
            "responseMessages": [
                {
                    "code": 201,
                    "message": "Created. The URL of the created blueprint should be in the Location header"
                },
                {
                    "code": 405,
                    "message": "Invalid input"
                }
            ],
            "summary": null
        }
    ],
    "path": "/todos"
}

When registering an endpoint with path parameters (e.g. /todos/<string:id>) then the .help url is may be found at the swagger path, e.g. /todos/{id}.help.json where {id} is just that - a literal string "{id}"

Example:

### Python:
> api.add_resource(Todo, '/todos/<string:todo_id>')

### Shell:
 # You might need to quote and escape to prevent the shell from messing around

curl 'localhost:5000/todos/\{todo_id\}.help.json'
{
    "description": "My TODO API",
    "operations": [
        {
            "method": "DELETE",
            "nickname": "nickname",
            "parameters": [
                {
                    "dataType": "string",
                    "name": "todo_id"
                }
            ],
            "summary": null
        },
        {
            "method": "GET",
            "nickname": "get",
            "notes": "get a todo item by ID",
            "parameters": [
                {
                    "allowMultiple": false,
                    "dataType": "string",
                    "description": "The ID of the TODO item",
                    "name": "todo_id_x",
                    "paramType": "path",
                    "required": true
                }
            ],
            "responseClass": "TodoItemWithResourceFields",
            "summary": "Get a todo task"
        },
        {
            "method": "PUT",
            "nickname": "nickname",
            "parameters": [
                {
                    "dataType": "string",
                    "name": "todo_id"
                }
            ],
            "summary": null
        }
    ],
    "path": "/todos/{todo_id}"
}

Accessing individual endpoints as HTML (.help.html)

Similarly to the .help.json URLs we have .help.html pages which are static HTML pages to document your APIs. Here's a screenshot to illustrate: An example .help.html page

This project is part of the Cloudify Cosmo project

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].