Flask extension for using 'specification first' principle via OpenAPI specification
Project description
Flask-First
Flask extension for using "specification first" principle.
Features:
Application Factory
supported.- Validating and serializing arguments from
request.headers
torequest.first_headers
. - Validating and serializing arguments from
request.view_args
torequest.first_view_args
. - Validating and serializing arguments from
request.args
torequest.first_args
. - Validating and serializing arguments from
request.cookies
torequest.first_cookies
. - Validating and serializing arguments from
request.json
torequest.first_json
. - Validating headers from request.
- Validating cookies from request.
- Validating path parameters from request.
- Validating parameters from request.
- Validating JSON from request.
- Validating JSON from response.
- Provides a Swagger UI.
Limitations
Will be added in future releases.
- Full specification in one file.
- Authorization not supported.
Installing
Recommended using the latest version of Python. Flask-First supports Python 3.9 and newer.
Install and update using pip
:
$ pip install flask_first
Simple example
OpenAPI 3 specification file openapi.yaml
:
openapi: 3.0.3
info:
title: Simple API for Flask-First
version: 1.0.0
paths:
/{name}:
parameters:
- name: name
in: path
required: true
schema:
type: string
get:
operationId: index
summary: Returns a list of items
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
message:
type: string
File with application initialization main.py
:
import os
from flask import Flask
from flask_first import First
basedir = os.path.abspath(os.path.dirname(__file__))
path_to_spec = os.path.join(basedir, 'openapi.yaml')
app = Flask(__name__)
app.config['FIRST_RESPONSE_VALIDATION'] = True
first = First(path_to_spec, app=app, swagger_ui_path='/docs')
def index(name):
return {'message': name}
first.add_view_func(index)
if __name__ == '__main__':
app.run()
Run application:
$ python main.py
Check url in browser http://127.0.0.1:5000/username
. Check SwaggerUI url in
browser http://127.0.0.1:5000/docs
.
Settings
FIRST_RESPONSE_VALIDATION
- Default: False
. Enabling response body validation. Useful when
developing. May be disabled in a production environment.
CORS support
Your need enable CORS in Flask and adding OPTIONS
method in your specification. Example:
...
paths:
/index:
post: ...
get: ...
put: ...
patch: ...
delete: ...
options:
summary: CORS support
responses:
200:
headers:
Access-Control-Allow-Origin:
schema:
type: string
Access-Control-Allow-Methods:
schema:
type: string
Access-Control-Allow-Headers:
schema:
type: string
content: { }
Data types
Supported formats for string type field:
- uuid
- date-time
- date
- time
- ipv4
- ipv6
- uri
- binary
Additional documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for Flask_First-0.14.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45bc295d55fd39291ff76e7b4aa435be2510eaf0833bafeb19e1376427d24e24 |
|
MD5 | 5499cf7e707cd8cf53df6e7fdf7fbfcb |
|
BLAKE2b-256 | d249baa7a0d4c17197df3596fea91a4840aeed80b4fb2d6df37aabd394aa3a13 |