/
produces.py
49 lines (38 loc) · 1.23 KB
/
produces.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
# Decorators to change the return type of endpoints
import functools
import logging
from .decorator import BaseDecorator
logger = logging.getLogger('connexion.decorators.produces')
# special marker object to return empty content for any status code
# e.g. in app method do "return NoContent, 201"
NoContent = object()
class BaseSerializer(BaseDecorator):
def __init__(self, mimetype='text/plain'):
"""
:type mimetype: str
"""
self.mimetype = mimetype
def __repr__(self):
"""
:rtype: str
"""
return '<BaseSerializer: {}>'.format(self.mimetype) # pragma: no cover
class Produces(BaseSerializer):
def __call__(self, function):
"""
:type function: types.FunctionType
:rtype: types.FunctionType
"""
@functools.wraps(function)
def wrapper(request):
url = request.url
response = function(request)
logger.debug('Returning %s', url,
extra={'url': url, 'mimetype': self.mimetype})
return response
return wrapper
def __repr__(self):
"""
:rtype: str
"""
return '<Produces: {}>'.format(self.mimetype) # pragma: no cover