Search code examples
python-3.xtornado

python tornado how to get id of a edge


I have a web application that runs on flask web framework. Flask couldn't answer application requirements anymore. So we have decided to migrate tornado. I need to handle below two request.

 /entry GET Method
 /entry/id GET Method
  1. When first request called it must return authenticated entry.
  2. When second request called it must return the entry whose entry_id is id

Is there a different nice solution to handle above request except my solution. The solution i have found it creates cyclomatic complexity.

def get(self):
     id = self.request.path.split('/')[-1]
     if id is None:
         #return authenticated user
     else:
         #return the user whose entry_id is id

Note: I am looking a solution like this:

@rest_user.route('/user', methods=['GET'])
     #some code
@rest_user.route('/user/<user_id>', methods=['GET'])
     #some code

Solution

  • The analogous arrangement in Tornado uses two handler classes (perhaps with a common base class for shared methods):

    class AuthedUserHandler(RequestHandler):
        def get(self):
            ...
    
    class UserHandler(RequestHandler):
        def get(self, user_id):
            ...
    
    app = Application([
        ('/user', AuthedUserHandler),
        ('/user/(.*)', UserHandler),
    ])