Skip to main content

Extended version of Flask to offer FastAPI inspired functionality.

Project description

Flast-FastAPI

Extended version of Flask to offer FastAPI inspired functionality. I wanted a way to offer similar functionality but run under AWS Lambda (and othre non-async environments).

Flask-FastAPI uses python type hints extensively to understand request and response data, and not including them will cause problems. Some examples below.

Pydantic is used extensively.

This is unfinished software

This software was extracted from another project that I built and retired. This portion of the software was of some use and so I thought I would rip it out and give it to the world for free. It is:

  • messy
  • buggy
  • incomplete
  • poorly documented
  • imperfect
  • amazing
  • working for me
  • a complete strange to automated testing

As I find time to improve the quality of this code, the documentation, and the test coverage I will aim to improve things a little. In the meantime please consider adding in your own fixes, tests, etc.

Decorators for common API methods

For each of the main HTTP requests there is an appropriate decorator. These decorators share some common parameters:

Route parameters

  • tags - array used in OpenAPI to categorise endpoint
  • summary - string used in OpenAPI to describe endpoint
  • response_code - used to override default response codes for each HTTP method,
  • requires_auth - boolean, whether this endpoint requires authentication, defaults to true
  • private - boolean, if set to true will not include endpoint in public documentation, defaults to false

Function

  • Python function doc - used as description in OpenAPI
  • A parameter called "body" will be considered as the POST request, and type hints determine how it's parsed
  • Parameter type hints will be used to coerce query parameters
  • A parameter which defaults to a Pydantic Field type will take further validation and details (such as description) from the information provided (more below)
  • Function response type hint will be used to determine response schema

Default response codes

Response code can be specified using the response_code route parameter, or the default will be used.

  • GET returns 200
  • POST returns 201
  • PUT returns 202
  • PATCH returns 202
  • DELETE returns 204, without content

Examples

GET

@api.get('/path/to/<id>', summary="Endpoint summary", tags=["tags","about","endpoint"])
def get_resource(
        id: int = Field(..., description="Unique identifier")
    ) -> ResponseSchema:

    ''' This documentation will be included to describe the endpoint.
    '''
    # this would return a Pydantic ResponseSchema in a real world example
    return ResponseSchema(...)

POST, PUT, and PATCH

@api.post('/path/to/entity', summary="...")
def post_resource(body: RequestSchema) -> ResponseSchema:
    ''' Request schema is automatically picked up from the name of the parameter.
    '''
    # this would return a Pydantic ResponseSchema in a real world example
    return ResponseSchema(...)

DELETE

@api.delete('/path/to/<id>', summary="Endpoint summary", tags=["tags","about","endpoint"])
def get_resource(
        id: int = Field(..., description="Unique identifier")
    ):

    ''' No return type hint is needed here, as delete does not return content by default
    '''
    pass

Exceptions

The exceptions defined in flask_fastapi.exceptions handle the most common cases encountered, but extension of exceptions.HttpException is trivial and can be used to manage different non-OK responses.

Automatic OpenAPI documentation

An openapi.json file will be generated from the routes that are created and can be downloaded by visiting the /openapi.json endpoint. There will also be an /openapi.yaml file available for anybody who wants it.

Documentation serving

Through automated generation of the OpenAPI documentation you also have serving of documentation through either Redoc of SwaggerUI, both of which have templates bundled with this package.

Redoc

Can be seen by navigating to $base_url/redoc/, e.g. http://localhost:5000/redoc/.

Github repo for redoc- https://github.com/Redocly/redoc

SwaggerUI

Can be seen by navigating to $base_url/docs/, e.g. http://localhost:5000/docs/.

Website for SwaggerUI - https://swagger.io/tools/swagger-ui/

To document

  • Security

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_fastapi-0.0.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

flask_fastapi-0.0.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file flask_fastapi-0.0.2.tar.gz.

File metadata

  • Download URL: flask_fastapi-0.0.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.6 Darwin/23.5.0

File hashes

Hashes for flask_fastapi-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d3c9745b04d95006b6ec426a472214cfd0fdd6a3dee55872420175985b76a59e
MD5 cf7878cef3062bb46c1c31131b4878f5
BLAKE2b-256 79b70984c4822d484d9171fc73137848c9f4b872010ae599137397457957e736

See more details on using hashes here.

File details

Details for the file flask_fastapi-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: flask_fastapi-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.6 Darwin/23.5.0

File hashes

Hashes for flask_fastapi-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 30cd64a2a2768dde64d2d7a372c8fc21c47a4052d77a97922c11a3fbb27d7ca3
MD5 5d4ca89573e5d4211c78da46eeedcfa5
BLAKE2b-256 86bde0ab528ea7869c3d815a2f74df9b7c07da3ee9aaad50094a1433df3d31fa

See more details on using hashes here.

Supported by

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