Search code examples
pythontornado

Verify Tornado arguments


I have a server which can accept 0, 1 or many of the following url arguments:

/api/cases?id={id}&name={name}&owner={owner}&status={status}

So these, amongst other, are correct:

/api/cases?owner=me
/api/cases
/api/cases?name=bob&status=waiting

Currently, my code looks like this

routes = [(r'/cases?([^/]+)', MyHandler)]
tornado.web.Application.__init__(self, routes, settings={})

class MyHandler(APIHandler):
    ACCEPTED_URL_ARGS = ["id", "name", "owner", "status"]

    def get(self, i):
        for key in self.request.arguments:
             if key not in self.ACCEPTED_URL_ARGS:
                   # error

Is there a better way to check for the url arguments?


Solution

  • What you have is correct. In Tornado there is no other way to verify that you only got the arguments that you expect than to iterate over self.request.arguments.