"""Implement a server to check if a contribution is covered by a CLA(s)."""
from aiohttp import web

from . import abc
from . import ContribHost
from . import ServerHost
from . import CLAHost


class Handler:

    """Handle requests from the contribution host."""

    def __init__(self, server: ServerHost, cla_records: CLAHost):
        self.server = server
        self.cla_records = cla_records

    async def respond(request: web.Request) -> web.StreamResponse:  # XXX untested
        """Handle a webhook trigger from the contribution host."""
        try:
            contribution = ContribHost.process(request)
            usernames = await contribution.usernames()  # XXX not implemented
            cla_status = await self.cla_records.check(usernames)  # XXX not implemented
            # With a background queue, one could add the update as a work item
            # and return an HTTP 202 response.
            return (await contribution.update(cla_status))  # XXX not implemented
        except abc.ResponseExit as exc:
            return exc.response
        except Exception as exc:
            self.server.log(exc)
            return web.Response(
                    status=http.HTTPStatus.INTERNAL_SERVER_ERROR.value)


if __name__ == '__main__':
    server = ServerHost()
    cla_records = CLAHost()
    handler = Handler(server, cla_records)
    app = web.Application()
    app.router.add_route(*ContribHost.route, handler.respond)
    web.run_app(app, port=server.port())

@@ -803,18 +803,12 @@
 h a 
-background
+work
  que
@@ -821,19 +821,20 @@
 e could 
-add
+make
  the upd
@@ -839,38 +839,57 @@
 pdat
-e as a work item%0A            #
+ing of the%0A            # contribution a work item
  and

Tweak comment about 202 response