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
|
||||
from flask import request
|
||||
|
||||
class RESTResource(object):
|
||||
"""Represents a REST resource, with the different HTTP verbs"""
|
||||
|
@ -72,8 +73,7 @@ class RESTResource(object):
|
|||
method = need_auth(self._authentifier,
|
||||
self._inject_name or self._name)(method)
|
||||
|
||||
# regarding the format, transform the response
|
||||
method = serialize("json")(method) #FIXME handle headers
|
||||
method = serialize(method)
|
||||
|
||||
app.add_url_rule(
|
||||
self._get_route_for(action),
|
||||
|
@ -120,12 +120,12 @@ def need_auth(authentifier, name=None, remove_attr=True):
|
|||
|
||||
# serializers
|
||||
|
||||
def serialize(format):
|
||||
def wrapper(func):
|
||||
def serialize(func):
|
||||
def wrapped(*args, **kwargs):
|
||||
return SERIALIZERS[format].encode(func(*args, **kwargs))
|
||||
mime = request.accept_mimetypes.best_match(SERIALIZERS.keys())
|
||||
return SERIALIZERS.get(mime, "text/json")\
|
||||
.encode(func(*args, **kwargs))
|
||||
return wrapped
|
||||
return wrapper
|
||||
|
||||
|
||||
class JSONEncoder(json.JSONEncoder):
|
||||
|
@ -139,4 +139,4 @@ class JSONEncoder(json.JSONEncoder):
|
|||
else:
|
||||
return json.JSONEncoder.default(self, o)
|
||||
|
||||
SERIALIZERS = {"json": JSONEncoder()}
|
||||
SERIALIZERS = {"text/json": JSONEncoder()}
|
||||
|
|
Loading…
Reference in a new issue