import functools


def only_xhr(method):
    """ Decorates a method in a handler to only accepts XMLHttpRequest
    requests.
    """
    @functools.wraps(method)
    def wrapper(self, *args, **kwargs):
        if "X-Requested-With" in self.request.headers:
            if self.request.headers['X-Requested-With'] == "XMLHttpRequest":
                return method(self, *args, **kwargs)
        else:
            self.set_status(403)
            self.write("This is a XMLHttpRequest request only.")
    return wrapper