All Projects → UseTheApi → flask_url_discovery

UseTheApi / flask_url_discovery

Licence: MIT license
Flask extension for discovering urls in a service. Automatically expose urls for a service. Manage urls exposure settings.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to flask url discovery

Flasgger
Easy OpenAPI specs and Swagger UI for your Flask API
Stars: ✭ 2,825 (+21630.77%)
Mutual labels:  restful, flask-extensions
express-mysql-demo
基于node.js + express + mysql实现的restful风格的CRUD简单示例
Stars: ✭ 44 (+238.46%)
Mutual labels:  restful
Tourism Demo
Flutter app backed by Redux, shows animations, internationalization (i18n), ClipPath, fonts and others...
Stars: ✭ 232 (+1684.62%)
Mutual labels:  restful
accountbook server
📔 记账本 Django 后端
Stars: ✭ 20 (+53.85%)
Mutual labels:  restful
Jersey 2.x User Guide
Jersey 2.x User Guide《Jersey 2.x 用户指南》 ,中文翻译
Stars: ✭ 235 (+1707.69%)
Mutual labels:  restful
Flask-CSV
Easily render CSVs within any flask application
Stars: ✭ 20 (+53.85%)
Mutual labels:  flask-extensions
Wok
A cherrypy framework for multi-purpose plug-ins
Stars: ✭ 215 (+1553.85%)
Mutual labels:  restful
rails-rest-api
A simple RoR 5 REST API demo with JWT authentication.
Stars: ✭ 25 (+92.31%)
Mutual labels:  restful
REST API Test Framework Python
REST API Test Framework example using Python requests and flask for both functional and performance tests.
Stars: ✭ 43 (+230.77%)
Mutual labels:  restful
gorest
RESTful Server Systems [DEPRECATED]
Stars: ✭ 34 (+161.54%)
Mutual labels:  restful
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+1846.15%)
Mutual labels:  restful
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (+1792.31%)
Mutual labels:  restful
go-zero
A cloud-native Go microservices framework with cli tool for productivity.
Stars: ✭ 23,294 (+179084.62%)
Mutual labels:  restful
tinyspec
Simple syntax for describing REST APIs
Stars: ✭ 95 (+630.77%)
Mutual labels:  restful
Php Crud Api
Single file PHP script that adds a REST API to a SQL database
Stars: ✭ 2,904 (+22238.46%)
Mutual labels:  restful
Flask Restplus
Fully featured framework for fast, easy and documented API development with Flask
Stars: ✭ 2,585 (+19784.62%)
Mutual labels:  restful
rest
REST webservices for TYPO3 CMS
Stars: ✭ 78 (+500%)
Mutual labels:  restful
flask-restx-boilerplate
🔥 REST API written in Flask micro web framework with the Flask-RESTX framework.
Stars: ✭ 132 (+915.38%)
Mutual labels:  restful
router
An Fully Automatic RESTful PHP Router
Stars: ✭ 51 (+292.31%)
Mutual labels:  restful
premiere
A simple way to consume APIs with Javascript.
Stars: ✭ 67 (+415.38%)
Mutual labels:  restful

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"
        ]
    }
}

Make a use of enpoint parameter to encapsulate a function name:

from flask import Flask
from flask_url_discovery import url_discovery

app = Flask(__name__)
url_discovery(app)

@app.route('/hello_world/', endpoint='custom_endpoint')
def hello_world():
    return 'Hello World!'

/config/routes/ response:

<...>
"custom_endpoint": {
        "active_urls": [
            "/hello_world/"
        ],
        "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 hello_world():
  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)

/config/routes/ 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

Docs

The package is provided with Sphinx documentation. To create a documentation execute make html in docs directory.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].