-
Notifications
You must be signed in to change notification settings - Fork 183
How to migrate to new _WebHandler class
Chris Burr edited this page Jul 12, 2022
·
2 revisions
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']})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']}- Remove
@asyncGendecorator andyield self.threadTaskconstruction, you don't need it anymore,WebHandlerinherit the logic ofBaseRequestHandler, 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()
...- Please use the usual
returninstead of the tornadoesself.write()orself.finish()methods.
Old style:
def web_hello(self):
...
self.finish({"success": "true", "...New style:
def web_hello(self):
...
return {"success": "true", "...- 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=[]):
...