from . import conf
"""
This module mainly define decorators for resources
"""
[docs]def endpoint(_func=None, path="/", **kwargs):
"""
Deadly simple decorator, add options to the given function.
Can be user with or without any keyword arguments.
The default options would contain the path and the name of the function.
Based on configuration: :py:data:`.conf.decorate`
"""
def decorator(_func):
if not hasattr(_func, conf.decorate):
setattr(_func, conf.decorate, {})
getattr(_func, conf.decorate).update(kwargs)
getattr(_func, conf.decorate).update({
'path': path,
'name': kwargs.pop('name', _func.__name__),
'status': kwargs.pop('status', 200),
})
return _func
if _func is not None:
return decorator(_func)
return decorator
[docs]def GET(_func=None, **kwargs):
"""
Decorator function
Ensure the given function will be available for GET method
"""
return endpoint(_func, methods=['GET'], **kwargs)
[docs]def POST(_func=None, **kwargs):
"""
Decorator function
Ensure the given function will be available for POST method
"""
return endpoint(
_func, methods=['POST'], status=kwargs.pop('status', 201), **kwargs
)
[docs]def RPC(_func=None, **kwargs):
"""
Decorator function
Ensure the given function will be available for POST method
This action tend to use as Remote procedure call
"""
return endpoint(
_func, methods=['POST'], **kwargs
)
[docs]def PUT(_func=None, **kwargs):
"""
Decorator function
Ensure the given function will be available for GET method
"""
return endpoint(_func, methods=['PUT'], **kwargs)
[docs]def DELETE(_func=None, **kwargs):
"""
Decorator function
Ensure the given function will be available for GET method
"""
return endpoint(_func, methods=['DELETE'], **kwargs)
[docs]def PATCH(_func=None, **kwargs):
"""
Decorator function
Ensure the given function will be available for GET method
"""
return endpoint(_func, methods=['PATCH'], **kwargs)