forked from miLibris/flask-rest-jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
57 lines (45 loc) · 1.82 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
from flask import Blueprint
class Api(object):
def __init__(self, app=None):
self.app = None
self.blueprint = None
if app is not None:
if isinstance(app, Blueprint):
self.blueprint = app
else:
self.app = app
self.resources = []
def init_app(self, app=None):
"""Update flask application with our api
:param Application app: a flask application
"""
if self.app is None:
self.app = app
if self.blueprint is not None:
self.app.register_blueprint(self.blueprint)
else:
for resource in self.resources:
self.route(**resource)
def route(self, resource, view, *urls, **kwargs):
"""Create an api view.
:param Resource resource: a resource class inherited from flask_rest_jsonapi.resource.Resource
:param str view: the view name
:param list urls: the urls of the view
:param dict kwargs: additional options of the route
"""
resource.view = view
view_func = resource.as_view(view)
url_rule_options = kwargs.get('url_rule_options') or dict()
if self.app is not None:
for url in urls:
self.app.add_url_rule(url, view_func=view_func, **url_rule_options)
elif self.blueprint is not None:
resource.view = '.'.join([self.blueprint.name, resource.view])
for url in urls:
self.blueprint.add_url_rule(url, view_func=view_func, **url_rule_options)
else:
self.resources.append({'resource': resource,
'view': view,
'urls': urls,
'url_rule_options': url_rule_options})