Flask Url Discovery
A Flask extension for discovering urls in a service and expose service’s routes for others.
Installation
Install the extention using pip or easy_install.
$ pip install -U Flask-UrlDiscovery
Usage
This package exposes a Flask extention that allows the user to automatically collect all (by default) routes that are created by Flask application or a Blueprint. The user can provide a custom uri string for exposing routes on the system as well as restrict the access to some routes or Blueprints.
Usage with Flask app and Blueprint
In order to expose all routes on the system the user only has to register Flask application with url_discovery:
from flask import Flask, Blueprint
from flask_url_discovery import url_discovery
app = Flask(__name__)
url_discovery(app)
app_bp = Blueprint('my_bp', __name__)
@app.route('/')
@app.route('/health_check/')
def hello_world():
return 'Hello World!'
@app_bp.route('/hello/')
def hello_bp():
return 'Hello Flask Blueprint'
if __name__ == '__main__':
app.register_blueprint(app_bp)
app.run('0.0.0.0', 5000)
By default all of the routes are getting exposed on http://host:port/config/routes/
Here is sample response for /config/routes/ GET request:
{
"flask_url_discovery.expose_routes": {
"active_urls": [
"/config/routes/"
],
"methods": [
"GET",
"HEAD",
"OPTIONS"
]
},
"hello_world": {
"active_urls": [
"/",
"/health_check/"
],
"methods": [
"GET",
"HEAD",
"OPTIONS"
]
},
"my_bp.hello_bp": {
"active_urls": [
"/hello/"
],
"methods": [
"GET",
"HEAD",
"OPTIONS"
]
},
"static": {
"active_urls": [
"/static/<path:filename>"
],
"methods": [
"GET",
"HEAD",
"OPTIONS"
]
}
}
Custom routes url
The user can specify custom routes url for url discovery
from flask import Flask
from flask_url_discovery import url_discovery
app = Flask(__name__)
url_discovery(app, custom_routes_url='/your_custom_routes_url/')
@app.route('/')
def helloWorld():
return "Hello World!"
Flask UrlDiscovery perfectly works with url_prefix for Flask Blueprints:
from flask import Flask, Blueprint
from flask_url_discovery import url_discovery
app = Flask(__name__)
url_discovery(app)
app_bp = Blueprint('my_bp', __name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app_bp.route('/hello/')
def hello_bp():
return 'Hello Flask Blueprint'
if __name__ == "__main__":
app.register_blueprint(app_bpm url_prefix='/custom_prefix')
app.run('0.0.0.0', 5000)
Response:
<...>
"my_bp.hello_bp": {
"active_urls": [
"/custom_prefix/hello/"
],
"methods": [
"GET",
"OPTIONS",
"HEAD"
]
},
<...>
Private routes and Blueprints
The user can private a single route of Flask application/Blueprint as well as a whole Blueprint. Flask UrlDiscovery provides a decorator function.
Usage with ```route()```:
from flask import Flask, Blueprint
from flask_url_discovery import url_discovery, private
app = Flask(__name__)
url_discovery(app)
app_bp = Blueprint("my_bp", __name__)
@app.route("/")
def hello_world():
return "Hello World!"
@private()
@app.route("/restricted_route/")
def private_endpoint():
return "Hello Private Endpoint"
@app_bp.route("/hello/")
def hello_bp():
return "Hello Flask Blueprint"
if __name__ == "__main__":
app.register_blueprint(app_bp)
app.run('0.0.0.0', 5000)
private_endpoint() will not be shown in the response of /config/routes/ request. Same approach is valid for privating a route of a Blueprint.
Usage with Flask Blueprints:
from flask import Flask, Blueprint
from flask_url_discovery import url_discovery, private
app = Flask(__name__)
url_discovery(app)
# or: app_bp = private(Blueprint("my_bp", __name__))
app_bp = Blueprint("my_bp", __name__)
private(app_bp)
@app.route("/")
def hello_world():
return "Hello World!"
@app_bp.route("/private/hello/")
def hello_bp():
return "Hello Flask Blueprint"
@app_bp.route("/private/goodbye/")
def bye_bp():
return "Goodbye Moonmen"
if __name__ == "__main__":
app.register_blueprint(app_bp)
app.run('0.0.0.0', 5000)
app_bp Blueprint is fully private now and none of the routes belong to this Blueprint will be exposed through API by UrlDiscovery
Test
The Package includes a test suite. To exercise tests run:
python setup.py tests
Contributing
If you have any questions, find any bugs/problems or have an idea of an improvement, please create an issue on GitHub and/or send me an e-mail.
Metadata
Release files for Flask-UrlDiscovery 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| Flask_UrlDiscovery-1.1.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for Flask_UrlDiscovery-1.1.1-py2.py3-none-any.whl |
Release files / Flask_UrlDiscovery-1.1.1-py2.py3-none-any.whl
| Download URL | Flask_UrlDiscovery-1.1.1-py2.py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
cec47aff6d93e4042574509f9298eac5d7468f6917f75ea0daa576c1c445312e
|
|
BLAKE2b-256 checksum How to use checksums |
ad2c6aa163581690c862945daa8d23080bbe0e75073c6237e5dd1fc6db10ad00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for Flask-UrlDiscovery 1.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.