1

I'm using django piston to implement my RESTful API. I already implemented an api which is

/api/[uuid of user]

will give all the information related to uuid of user.

However, I also want to implement another api

/api/user/username

where the output should exactly the same as the previous one.

I don't want to maintain two api with different input but has the same input. Therefore, what I want to do is redirect the piston api call. After searching around on the internet, it seems that piston can not do it (correct me if I'm wrong), so I come out of a workaround. For the second api, I can use django's view function to realize, and if the username does exist, then return the handler. If not exist, then return the error message in view function. My code is the following.

def username_url_map(request, username):
   try
        user = UserProfile.objects.get(user = username)
    except UserProfile.DoesNotExist:
        return HttpResponse(simplejson.dumps({'error':'This user does not exist.' }), mimetype='application/json')
    except UserProfile.MultipleObjectsReturned:
        return HttpResponse(simplejson.dumps({'error':'This user does not exist.'}), mimetype='application/json')
    uuid = user.uuid

    results=GenericHandler.read(request, uuid)

    json = simplejson.dumps(results)
    return HttpResponse(json, mimetype='application/json')

But I get the following error message:

TypeError
Exception Value: unbound method wrapper() must be called with GenericHandler instance as first argument (got WSGIRequest instance instead)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
DB Tsai
  • 1,378
  • 1
  • 13
  • 23

1 Answers1

2

The error message is telling you that the read method of GenericHander is an instance method, not a classmethod. You'll need to instantiate the object before you can call the method.

Without knowing anything more about the class or method, this might work:

handler = GenericHandler()
results = handler.read(request, uuid)

but the instantiation call might need some parameters, which should be documented.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895