Question

I'm getting a value from a HTTP GET request. I'm checking whether the value is not equal to none. If it is , then return message is sent else further processing is happening.

def api_servers():
    installation_name = request.args.get('installation_name', '')
    if installation_name == '':
        data = {"description": "Installation Name is required"}
        return HttpResponse(json.dumps(data), status=400, mimetype='application/json')

    data = { "arms": arms_list }

    return HttpResponse(json.dumps(data), status=200, mimetype='application/json')

Now, I want to check this condition using a decorator function. This is what I'm trying.

def wrapper(func):
    def inner(): # I dont know what to pass in the arguments. Installation_name? 
                 # Maybe? But then how do I pass Installation_name

        if installation_name == '':
            data = {"description": "Installation Name is required"}
            return HttpResponse(json.dumps(data), status=400, mimetype='application/json')

        else: 
            return func()
    return inner 

How can I achieve this via decorator?

EDIT While I was proceeding, I found that I need another decorator which checks data is None or not.It also checks whether installation_name is empty or not and then I need to "recycle" both the installation_name and data. I re-wrote your code again. But the values are being interchanged, ie data gets installation_name and vice-versa. Can you please have a look? Thanks

def with_description(fn):

    def inner(*args, **kwargs):
    # Precheck
        installation_name = 'inst1'
        if installation_name == '':
            return 1

        # If precheck passes
        return fn(installation_name, *args, **kwargs)

    return inner


def with_description1(fn):

    def inner(*args, **kwargs):
    # Precheck
        data = 'data1'
        if data == '':
            return 1

        # If precheck passes
        return fn(data, *args, **kwargs)

    return inner

@with_description
@with_description1
def api_servers1(installation_name,data):
    print installation_name,data



api_servers1()
Was it helpful?

Solution

Uhm, give this a try:

import functools

def with_description(fn):

    @functools.wraps(fn)  # This is not strictly necessary, just pulls info (docstrings, etc) from fn and pushes into the wrapper
    def wrapper(*args, **kwargs):
        # Precheck
        installation_name = request.args.get('installation_name', '')
        if installation_name == '':
            data = {"description": "Installation Name is required"}
            return HttpResponse(json.dumps(data), status=400, mimetype='application/json')

        # If precheck passes, call the actual function
        fn(*args, **kwargs)

    return wrapper

With this you win that every function decorated actually gets called only when the precheck passes.

EDIT

If as commented, you want to recycle the installation name, you just have to pass it to the function call. One possibility would be the following:

fn(installation_name, *args, **kwargs)

And of course, the decorated function has to expect that argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top