Metadata-Version: 2.2
Name: Flask-First
Version: 0.20.0
Summary: Flask extension for using 'specification first' or 'API-first' principle via OpenAPI specification.
Author-email: Konstantin Fadeev <fadeev@legalact.pro>
License: The MIT License (MIT)
        
        Copyright (c) 2021 Konstantin Fadeev
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        THE SOFTWARE.
        
Project-URL: changelog, https://github.com/flask-pro/flask-first/blob/master/CHANGES.md
Project-URL: repository, https://github.com/flask-pro/flask-first
Classifier: Framework :: Flask
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Flask>=2.0.3
Requires-Dist: PyYAML>=6.0.1
Requires-Dist: openapi-spec-validator>=0.5.0
Requires-Dist: marshmallow>=3.14.1
Provides-Extra: dev
Requires-Dist: bandit==1.7.9; extra == "dev"
Requires-Dist: build==1.2.1; extra == "dev"
Requires-Dist: mypy==1.11.2; extra == "dev"
Requires-Dist: pre-commit==3.8.0; extra == "dev"
Requires-Dist: pytest==8.3.2; extra == "dev"
Requires-Dist: pytest-cov==5.0.0; extra == "dev"
Requires-Dist: python-dotenv==1.0.1; extra == "dev"
Requires-Dist: tox==4.18.0; extra == "dev"
Requires-Dist: twine==5.1.1; extra == "dev"

# Flask-First

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

<!--TOC-->

- [Flask-First](#flask-first)
  - [Features](#features)
  - [Installation](#installation)
  - [Settings](#settings)
  - [Tools](#tools)
  - [Data types](#data-types)
    - [`date-time` format](#date-time-format)
  - [Examples](#examples)
    - [Simple example](#simple-example)
    - [Specification from multiple file](#specification-from-multiple-file)
    - [CORS support](#cors-support)
  - [Additional documentation](#additional-documentation)

<!--TOC-->

## 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`:

```shell
$ 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.

```python
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`:

```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`:

```python
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:

```shell
$ 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`:

```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`:

```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:

```yaml
...
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

* [OpenAPI Documentation](https://swagger.io/specification/).
* [OpenAPI on GitHub](https://github.com/OAI/OpenAPI-Specification).
* [JSON Schema Documentation](https://json-schema.org/specification.html).
