mirror of
https://github.com/YunoHost-Apps/ihatemoney_ynh.git
synced 2024-09-03 19:26:15 +02:00
add mimetype support to the rest API utils
This commit is contained in:
parent
d2e2260e52
commit
a60b0c2b48
1 changed files with 18 additions and 18 deletions
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
from flask import request
|
||||||
|
|
||||||
class RESTResource(object):
|
class RESTResource(object):
|
||||||
"""Represents a REST resource, with the different HTTP verbs"""
|
"""Represents a REST resource, with the different HTTP verbs"""
|
||||||
|
@ -23,14 +24,14 @@ class RESTResource(object):
|
||||||
:app:
|
:app:
|
||||||
Application to register the routes onto
|
Application to register the routes onto
|
||||||
|
|
||||||
:actions:
|
:actions:
|
||||||
Authorized actions. Optional. None means all.
|
Authorized actions. Optional. None means all.
|
||||||
|
|
||||||
:handler:
|
:handler:
|
||||||
The handler instance which will handle the requests
|
The handler instance which will handle the requests
|
||||||
|
|
||||||
:authentifier:
|
:authentifier:
|
||||||
callable checking the authentication. If specified, all the
|
callable checking the authentication. If specified, all the
|
||||||
methods will be checked against it.
|
methods will be checked against it.
|
||||||
"""
|
"""
|
||||||
if not actions:
|
if not actions:
|
||||||
|
@ -45,12 +46,12 @@ class RESTResource(object):
|
||||||
|
|
||||||
for action in actions:
|
for action in actions:
|
||||||
self.add_url_rule(app, action)
|
self.add_url_rule(app, action)
|
||||||
|
|
||||||
def _get_route_for(self, action):
|
def _get_route_for(self, action):
|
||||||
"""Return the complete URL for this action.
|
"""Return the complete URL for this action.
|
||||||
|
|
||||||
Basically:
|
Basically:
|
||||||
|
|
||||||
- get, update and delete need an id
|
- get, update and delete need an id
|
||||||
- add and list does not
|
- add and list does not
|
||||||
"""
|
"""
|
||||||
|
@ -58,22 +59,21 @@ class RESTResource(object):
|
||||||
|
|
||||||
if action in self._NEED_ID:
|
if action in self._NEED_ID:
|
||||||
route += "/<%s>" % self._identifier
|
route += "/<%s>" % self._identifier
|
||||||
|
|
||||||
return route
|
return route
|
||||||
|
|
||||||
def add_url_rule(self, app, action):
|
def add_url_rule(self, app, action):
|
||||||
"""Registers a new url to the given application, regarding
|
"""Registers a new url to the given application, regarding
|
||||||
the action.
|
the action.
|
||||||
"""
|
"""
|
||||||
method = getattr(self._handler, action)
|
method = getattr(self._handler, action)
|
||||||
|
|
||||||
# decorate the view
|
# decorate the view
|
||||||
if self._authentifier:
|
if self._authentifier:
|
||||||
method = need_auth(self._authentifier,
|
method = need_auth(self._authentifier,
|
||||||
self._inject_name or self._name)(method)
|
self._inject_name or self._name)(method)
|
||||||
|
|
||||||
# regarding the format, transform the response
|
method = serialize(method)
|
||||||
method = serialize("json")(method) #FIXME handle headers
|
|
||||||
|
|
||||||
app.add_url_rule(
|
app.add_url_rule(
|
||||||
self._get_route_for(action),
|
self._get_route_for(action),
|
||||||
|
@ -83,7 +83,7 @@ class RESTResource(object):
|
||||||
|
|
||||||
|
|
||||||
def need_auth(authentifier, name=None, remove_attr=True):
|
def need_auth(authentifier, name=None, remove_attr=True):
|
||||||
"""Decorator checking that the authentifier does not returns false in
|
"""Decorator checking that the authentifier does not returns false in
|
||||||
the current context.
|
the current context.
|
||||||
|
|
||||||
If the request is authorized, the object returned by the authentifier
|
If the request is authorized, the object returned by the authentifier
|
||||||
|
@ -100,7 +100,7 @@ def need_auth(authentifier, name=None, remove_attr=True):
|
||||||
of the decorated function
|
of the decorated function
|
||||||
|
|
||||||
:remove_attr:
|
:remove_attr:
|
||||||
Remove or not the `*name*_id` from the kwargs before calling the
|
Remove or not the `*name*_id` from the kwargs before calling the
|
||||||
function
|
function
|
||||||
"""
|
"""
|
||||||
def wrapper(func):
|
def wrapper(func):
|
||||||
|
@ -120,12 +120,12 @@ def need_auth(authentifier, name=None, remove_attr=True):
|
||||||
|
|
||||||
# serializers
|
# serializers
|
||||||
|
|
||||||
def serialize(format):
|
def serialize(func):
|
||||||
def wrapper(func):
|
def wrapped(*args, **kwargs):
|
||||||
def wrapped(*args, **kwargs):
|
mime = request.accept_mimetypes.best_match(SERIALIZERS.keys())
|
||||||
return SERIALIZERS[format].encode(func(*args, **kwargs))
|
return SERIALIZERS.get(mime, "text/json")\
|
||||||
return wrapped
|
.encode(func(*args, **kwargs))
|
||||||
return wrapper
|
return wrapped
|
||||||
|
|
||||||
|
|
||||||
class JSONEncoder(json.JSONEncoder):
|
class JSONEncoder(json.JSONEncoder):
|
||||||
|
@ -139,4 +139,4 @@ class JSONEncoder(json.JSONEncoder):
|
||||||
else:
|
else:
|
||||||
return json.JSONEncoder.default(self, o)
|
return json.JSONEncoder.default(self, o)
|
||||||
|
|
||||||
SERIALIZERS = {"json": JSONEncoder()}
|
SERIALIZERS = {"text/json": JSONEncoder()}
|
||||||
|
|
Loading…
Reference in a new issue