Skip to main content

Tooling to assist with building Flask APIs

Project description

Validate Build

Flask API Tools

Utilities for building, running, and maintaining Python APIs with Flask and associated Flask extensions.

Installation

Install and update using pip:

pip install -U Flask-API-Tools

Storage Backed Rate Limiting

The InMemoryLimiter class is an extension of Flask-Limiter. This class implements storage-backed rate limiting. You'll need to follow the rate limiting steps outlined in Flask-Limiter, and you must provide a RATELIMIT_STORAGE_URL to a redis (or other in-memory data structure) instance.

This class attaches any existing Flask log handlers to Flask-Limiter.

Basic Flask usage:

from flask import Flask
from flask_api_tools.rate_limiting.in_memory_limiter import InMemoryLimiter

app = Flask(__name__)
limiter = InMemoryLimiter(app=app, storage_uri="redis://localhost:6379")

This will raise a ConfigurationError (from limits.errors) if the backend storage is inaccessible.

Alternatively, you can still benefit from the configuration checking when using Flask-Limiter's init_app function:

from flask import Flask
from flask_api_tools.rate_limiting.in_memory_limiter import InMemoryLimiter

app = Flask(__name__)
limiter = InMemoryLimiter(storage_uri="redis://localhost:6379")
# Do some other things with the limiter...
limiter.init_app(app=app)

In addition to this, init_app allows you to set several in-memory useful variables. This lets you dynamically configure the rate limiter using objects from the Flask application context - such as app.config values. This is not possible with the base class. Currently, the supported variable overrides are:

  • storage_uri
  • key_prefix

Developing

The build pipeline requires your tests to pass and code to be formatted

Make sure you have Python 3.x installed on your machine (use pyenv).

Install the dependencies with pipenv (making sure to include dev and pre-release packages):

pipenv install --dev --pre

Configure your environment:

pipenv shell && export PYTHONPATH="$PWD"

Run the tests:

pytest

Or with logging:

pytest -s

Or tests with coverage:

pytest --cov=./

Format the code with Black:

black $PWD

Releases

Cleanup the (.gitignored) dist folder (if you have one):

rm -rf dist

Notch up the version number in setup.py and build:

python3 setup.py sdist bdist_wheel

Push to PyPi (using the ScholarPack credentials when prompted)

python3 -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*

Links

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-api-tools-1.0.5.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

flask_api_tools-1.0.5-py3-none-any.whl (6.6 kB view hashes)

Uploaded Python 3

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