The App Handler

Step 3: The App Handler

At the end of the pipeline sits the application itself. AppHandler is a terminal handler: it produces the real response and never delegates, so it is always the last link in the chain.

Implement AppHandler.handle(request):

  • Return {"status": 200, "body": "ok:" + request["path"]}. Ignore any next handler entirely.

With this in place you can build the first real chain: auth.set_next(app). An authenticated request flows through auth into app and gets a 200; a request with no user is stopped at auth with a 401 and never reaches app.

Hints
Starter code
class Handler:
    def __init__(self):
        self._next = None

    def set_next(self, handler):
        self._next = handler
        return handler

    def handle(self, request):
        if self._next is not None:
            return self._next.handle(request)
        return None


class AuthHandler(Handler):
    def handle(self, request):
        if request.get("user") is None:
            return {"status": 401, "body": "unauthorized"}
        return super().handle(request)


class AppHandler(Handler):
    def handle(self, request):
        """Terminal handler: build the 200 response and ignore any next handler."""
        # TODO: return {"status": 200, "body": "ok:" + request["path"]}
        pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.