Skip to content

How to migrate to new _WebHandler class

Chris Burr edited this page Jul 12, 2022 · 2 revisions

Starting with version 5.0, a new style of writing WebApp handlers is introduced.

Old style:

from WebAppDIRAC.Lib.WebHandler import WebHandler, asyncGen

class SimpleHandler(WebHandler):

    AUTH_PROPS = "authenticated"

    @asyncGen
    def web_hello(self):
        """ My test target method
        """
        name = self.get_argument("name")
        age = self.get_argument("age", 0)
        if age:
            age = int(age)
        result = yield self.threadTask(self.createMessage, name, age))
        if not result['OK']:
            self.finish({"success": "false", "error": result['Message']})
            return
        self.finish({"success": "true", "result": result['Value']})

New style:

from WebAppDIRAC.Lib.WebHandler import WebHandler

class SimpleHandler(WebHandler):

    DEFAULT_AUTHORIZATION = "authenticated"

    def web_hello(self, name, age=0):
        """ My test target method
        """
        result = self.createMessage(name, age)
        if not result['OK']:
            return {"success": "false", "error": result['Message']}
        return {"success": "true", "result": result['Value']}

To migrate to a new writing style you need

  1. Remove @asyncGen decorator and yield self.threadTask construction, you don't need it anymore, WebHandler inherit the logic of BaseRequestHandler, which makes parallelization of asyncio threads for you.

Old style:

    @asyncGen
    def web_hello(self):
        result = yield self.threadTask(self._myFunc)
        ...

New style:

    def web_hello(self):
        result = self._myFunc()
        ...
  1. Please use the usual return instead of the tornadoes self.write() or self.finish() methods.

Old style:

    def web_hello(self):
        ...
        self.finish({"success": "true", "...

New style:

    def web_hello(self):
        ...
        return {"success": "true", "...
  1. Please use function annotation to describe request arguments.

Old style:

    def web_hello(self):
        myName = self.get_argument("name")
        myAge = self.get_argument("age", "no age")
        userId = int(self.get_argument("user_id", 0))
        things = self.get_arguments("things")
        ...

New style:

    def web_hello(self, name, age: int = "no age", user_id=0, things=[]):
        ...
Clone this wiki locally