vlastic / vlastic / rom /

"""Some utility decorators for resource-object mapping."""

from . import resource

__all__ = ["method", "get", "post", "put", "delete"]

class method:
    """Creates a decorator to make a function to handle specific HTTP method
    e.g. GET, POST.


    def __init__(self, method):
        self.method = method.upper()

    def __call__(self, function):
        """Make given function to handle specific HTTP method."""
        handler_attr = resource.METHOD_HANDLER_ATTRIBUTE_NAME
        if hasattr(function, handler_attr):
            getattr(function, handler_attr).append(self.method)
            setattr(function, handler_attr, [self.method])
        return function

get = method("GET")
post = method("POST")
put = method("PUT")
delete = method("DELETE")