私はこれを適応させようとしています:
http://flask.pocoo.org/docs/views/
青写真自体に (私が見た他の青写真に基づいて)。API 登録をアプリケーションから分離して、ブループリントの初期化に抽象化します。これはフラスコのドキュメントからのコードで、いくつかの変更が加えられています。
これはうまくいくようです:
class MyAPI(MethodView):
def __init__(self, name):
self.name = name
bp = Blueprint(name, __name__)
bp_endpoint = '{0}_api'.format(name)
bp_url = '/{0}/'.format(name)
bp_pk = '{0}_tag'.format(name)
self.register_api(bp, bp_endpoint, bp_url, bp_pk, 'string')
self._blueprint = bp
def register_api(self, blueprint, endpoint, url, pk='id', pk_type='int'):
view_func = self.as_view(endpoint)
blueprint.add_url_rule(url, defaults={pk: None},
view_func=view_func, methods=['GET',])
blueprint.add_url_rule(url, view_func=view_func, methods=['POST',])
blueprint.add_url_rule('{0}<{1}:{2}>'.format(url, pk_type, pk), view_func=view_func,
methods=['GET', 'PUT', 'DELETE'])
def get(self, my_tag):
#... with post, put methods etc.
次に、私のアプリでこれを行うことができます:
m = MyAPI('my')
app.register_blueprint(m._blueprint)
これは機能しているようで、URLを登録して取得できるようにします:
Map([<Rule '/my/' (POST, OPTIONS) -> my.my_api>,
<Rule '/my/<my_tag>' (PUT, HEAD, DELETE, OPTIONS, GET) -> my.my_api>,
<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,
<Rule '/my/' (HEAD, OPTIONS, GET) -> my.my_api>])
ただし、今ルートに行くとエラーが発生します(GETを試しました):
Traceback (most recent call last):
File "/media/686e26f8-c6d4-4448-8fe4-c19802726dcb/projects/1_current/private/pycleaver/venv/lib/python2.7/site-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/media/686e26f8-c6d4-4448-8fe4-c19802726dcb/projects/1_current/private/pycleaver/venv/lib/python2.7/site-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/media/686e26f8-c6d4-4448-8fe4-c19802726dcb/projects/1_current/private/pycleaver/venv/lib/python2.7/site-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/media/686e26f8-c6d4-4448-8fe4-c19802726dcb/projects/1_current/private/pycleaver/venv/lib/python2.7/site-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/media/686e26f8-c6d4-4448-8fe4-c19802726dcb/projects/1_current/private/pycleaver/venv/lib/python2.7/site-packages/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/media/686e26f8-c6d4-4448-8fe4-c19802726dcb/projects/1_current/private/pycleaver/venv/lib/python2.7/site-packages/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/media/686e26f8-c6d4-4448-8fe4-c19802726dcb/projects/1_current/private/pycleaver/venv/lib/python2.7/site-packages/flask/views.py", line 83, in view
self = view.view_class(*class_args, **class_kwargs)
**TypeError: __init__() takes exactly 2 arguments (1 given)**
ああ、これは私が考えることができる atm よりも 1 つまたは 2 つ低いレベルです。私が見逃したものについては、どんな意見でも感謝します。最初は register_api の view_func と関係があるのではないかと思います。
編集:
ある種の答え
class MyAPI(MethodView):
def __init__(self, name):
self.name = name
bp = Blueprint(name, __name__)
self.endpoint = '{0}_api'.format(name)
self.url = '/{0}/'.format(name)
self.pk = '{0}_tag'.format(name)
self._blueprint = bp
self.register_api(self._blueprint, self.endpoint, self.url, self.pk)
def register_api(self, bp, endpoint, url, pk ='id', pk_type='int'):
view_func = self.__class__.as_view(endpoint)
bp.add_url_rule(url, defaults={pk: None},
view_func=view_func, methods=['GET',])
bp.add_url_rule(url, view_func=view_func, methods=['POST',])
bp.add_url_rule('{0}<{1}:{2}>'.format(url, pk_type, pk), view_func=view_func,
methods=['GET', 'PUT', 'DELETE'])