August 2018
Intermediate to advanced
366 pages
10h 14m
English
We are going to use WSGIApplication from our previous recipe, but instead of registering a function for a root, we are going to register a particular class able to dispatch based on the request method.
class RestController:
def __call__(self, req, resp):
method = req.environ['REQUEST_METHOD']
action = getattr(self, method, self._not_found)
return action(req, resp)
def _not_found(self, environ, resp):
resp.status = '404 Not Found'
return b'{}' # Provide an empty JSON document