Last active
October 26, 2017 05:16
-
-
Save whitekid/f3012372f220cf27a468d13080a55893 to your computer and use it in GitHub Desktop.
flaskrestful with blueprint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import importlib | |
import inspect | |
from flask import Flask | |
from flask_restful import Resource | |
app = Flask(__name__) | |
for module_name in ['v1', 'v2']: | |
module = importlib.import_module(module_name) | |
for name, obj in inspect.getmembers(module, inspect.isclass): | |
if not issubclass(obj, Resource) or obj == Resource: | |
continue | |
module.API.add_resource(obj, *obj.URLS) | |
app.register_blueprint(module.BLUEPRINT, url_prefix=module.URL_PREFIX) | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Blueprint | |
from flask_restful import Api, Resource | |
BLUEPRINT = Blueprint(__name__, __name__) | |
API = Api(BLUEPRINT) | |
URL_PREFIX = '/v1' | |
class ItemResource(Resource): | |
URLS = ['', '/<item>'] | |
def get(self, item=None): | |
if item is None: | |
return 'v1' | |
return 'v1 ' + item |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Blueprint | |
from flask_restful import Api, Resource | |
BLUEPRINT = Blueprint(__name__, __name__) | |
API = Api(BLUEPRINT) | |
URL_PREFIX = '/v2' | |
class ItemResource(Resource): | |
URLS = ['', '/<item>'] | |
def get(self, item=None): | |
if item is None: | |
return 'v2' | |
return ((item + ' ') * 2).strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment