Skip to main content

Flask extension for using 'specification first' or 'API-first' principle via OpenAPI specification.

Project description

Flask-First

Flask extension for using "specification first" and "API-first" principles.

Features

  • Application Factory supported.
  • Validating and serializing headers of request.
  • Validating and serializing path parameters of request.
  • Validating and serializing arguments of request.
  • Validating and serializing cookies of request.
  • Validating and serializing JSON of request.
  • Validating JSON from response for debugging.
  • Provides a Swagger UI.
  • Support OpenAPI version 3.1.0.
  • Support specification from multiple file.
  • The time zone is always UTC.

Installation

Recommended using the latest version of Python. Flask-First supports Python 3.9 and newer.

Install and update using pip:

$ pip install -U flask_first

Settings

  • FIRST_RESPONSE_VALIDATION - Default: False. Enabling response body validation. Useful when developing. Must be disabled in a production environment.
  • FIRST_DATETIME_FORMAT - Default: None. Set format for format: date-time. Example: %Y-%m-%dT%H:%M:%S.%fZ.

Tools

Possible to get data from path-parameters, arguments, JSON, cookies and headers in serialized form. Use flask-first object attached to the query.

from flask import request


def route_func():
    path_parameters = request.extensions['first']['views']
    args = request.extensions['first']['args']
    json = request.extensions['first']['json']
    cookies = request.extensions['first']['cookies']
    headers = request.extensions['first']['headers']

Data types

Supported formats for string type field:

  • uuid
  • date-time
  • date
  • time
  • email
  • ipv4
  • ipv6
  • uri
  • binary

date-time format

For date-dime format, the time zone is enforced set in the UTC. The time zone of the incoming DateTime object is discarded and set as UTC.

Examples

Simple example

OpenAPI 3 specification file openapi.yaml:

openapi: 3.1.0
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.

Specification from multiple file

Flask-First supported specification OpenAPI from multiple files. You need create root file for specification with name openapi.yaml.

Root file openapi.yaml:

openapi: 3.1.0
info:
  title: Simple API for Flask-First
  version: 1.0.0
paths:
  /{name}:
    $ref: 'name.openapi.yaml#/name'
components:
  schemas:
    MessageField:
      type: string
      description: Field for message.

Child file name.openapi.yaml:

name:
  parameters:
    - name: name
      in: path
      required: true
      schema:
      type: string
  get:
    operationId: index
    summary: Returns a list of items
    responses:
      '200':
        $ref: '#/components/responses/ResponseOK'
components:
  responses:
    ResponseOK:
      description: OK
      content:
        application/json:
          schema:
            type: object
            properties:
              message:
                $ref: 'openapi.yaml#/components/schemas/MessageField'

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: { }

Additional documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_first-0.20.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_first-0.20.0-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file flask_first-0.20.0.tar.gz.

File metadata

  • Download URL: flask_first-0.20.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for flask_first-0.20.0.tar.gz
Algorithm Hash digest
SHA256 c2edee739c66e9c7fd2104514fa75aa19baf1c116247840da1a4c636d1b598c9
MD5 bb7b2067192e56856d481eff0d6cdd72
BLAKE2b-256 99f2232a1c9d996913162cf3db5407b3c2d751decf8774870deeb6f8e1431067

See more details on using hashes here.

File details

Details for the file flask_first-0.20.0-py3-none-any.whl.

File metadata

  • Download URL: flask_first-0.20.0-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for flask_first-0.20.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb54b882360a790d9c66835cd24db3e00c7bae9250b894e7e19623ff7029797b
MD5 26ae61fd5fdb4139edb0d58f79e8e04d
BLAKE2b-256 5dd4af355c71c0ac4ab4d158095b91e26e71c75cea80edb7a0786066e87129f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page