Extension that provides an easy way to add dev-only shortcuts to your routes.
Project description
Project Description
This extension provides an easy and safe way to add dev-only shortcuts to routes in your flask application.
The main beneficiaries are microservices that need to be tested regularly in conjunction with their clients. If you need to assert working communication and basic integration in a sufficiently complex ecosystem, clients that can not freely chose how their requests are formed gain a lot from being able to receive predictable responses. By skipping over the details of how the microservice is implemented, which bugs and minor changes it experiences over time, testing basic API compatibility gets a lot more manageable.
Usage
You can add shortcuts to your route functions either individually with decorators, or in a single swoop once all routes have been defined. Both ways are functionally equivalent.
Applying Shortcuts
With decorators:
from flask import Flask
from flask_shortcut import Shortcut
app = Flask(__name__)
short = Shortcut(app)
app.route('/foo', methods=['GET'])
short.cut(('short_foo', 200))
def foo():
return 'foo'
app.route('/bar', methods=['POST'])
short.cut({
'{"name": "TestUser"}': ('short_bar', 200)},
'{"name": "UserTest"}': ('longer_bar', 200),
)
def bar():
return 'bar'
With a wire call
from flask import Flask
from flask_shortcut import Shortcut
app = Flask(__name__)
app.route('/foo', methods=['GET'])
def foo():
return 'foo'
app.route('/bar', methods=['POST'])
def bar():
return 'bar'
Shortcut(app).wire(
{
'/foo': ('short_foo', 200),
'/bar': {
'{"name": "TestUser"}': ('short_bar', 200),
'{"name": "UserTest"}': ('longer_bar', 200),
}
}
)
What it looks like
To showcase how the shortcuts are supposed to work, here is the result of a couple of requests sent against the server from the example above if it were run with FLASK_ENV=test flask run:
>>> from request import get, post
>>> get('http://127.0.0.1:5000/foo').text
'short_foo' # the only response this route will give
>>> post('http://127.0.0.1:5000/bar', json={"name": "me"}).text
'bar' # no shortcut match -> the original logic was executed
>>> post('http://127.0.0.1:5000/bar', json={"name": "TestUser"}).text
'short_bar' # shortcut match
>>> post('http://127.0.0.1:5000/bar', json={"name": "UserTest", "job": None}).text
'longer_bar' # shortcut only needs to be contained for a match
One focus of this package was, that a production deployment would remain as ignorant as possible about the existence of shortcuts. While the shortcut object is still created, it only delegates the route functions and no shortcut code has any chance of being run.
Configuration
By default, shortcuts will only be applied when FLASK_ENV is set to something different than the default setting production. You can extend that list through the SHORTCUT_EXCLUSIONS config setting, either by adding it to your app’s config before creating any Shortcut objects, or preferably by setting up the whole config through a file.
Possible values for it are all environments other than production that you want to block separated by commas, for example staging,master.
Project home is on github.
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
Built Distribution
File details
Details for the file flask_shortcut-0.3.2.tar.gz
.
File metadata
- Download URL: flask_shortcut-0.3.2.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.6.10 Linux/5.0.0-1036-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7df5ee008c2e0275c712ab6a573a1330f2217fee82af12955ff502656e05a38 |
|
MD5 | b13c291189eb3eb5ea3de19836d012ca |
|
BLAKE2b-256 | 830a32e769f841ff8b555327178dfdce941e13159c2de8e48bf6b002d6891bf0 |
Provenance
File details
Details for the file flask_shortcut-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: flask_shortcut-0.3.2-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.6.10 Linux/5.0.0-1036-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0049bdb133cf33e477500c41ad83fdec2b4a3cec124604dd5c33d51da4800f49 |
|
MD5 | ea2e1bb38257dc6a04c05ea37191a7da |
|
BLAKE2b-256 | c3a62c3250b0678288d1da8406a088e14d8f275da780cc5b756f593b078cd80d |