def on_result(self, req: HTTPRequest, res: HTTPResponse):
        c = self.get_client(req, res)
        if not c: return

        js = req.body_json()

        ret = c.result(js)
        if ret:
            res.ok(errors.OK, "OK", None)
        else:
            res.not_found(errors.ID_NOT_FOUND,
                          "L'id de la réponse n'existe pas")
    def on_get_result(self, req: HTTPRequest, res: HTTPResponse):
        if not self.is_authorized(req, res): return
        cid = req.params["clientid"]
        cmdid = req.params["cmdid"]
        if not self._clients.has(cid):
            return res.not_found(errors.ID_NOT_FOUND, "Client not found", None)

        client = self._clients[cid]

        if not client.has_command(cmdid):
            return res.not_found(errors.ID_NOT_FOUND, "Command not found",
                                 None)

        r = client.find_response(cmdid)
        while not r:
            time.sleep(0.01)
            client.find_response(cmdid)

        res.ok(errors.OK, "OK", r)
    def on_get_file(self, req: HTTPRequest, res: HTTPResponse):
        id = req.params["id"]
        path = "download/" + id
        if not self._clients.has_file(id):
            return res.not_found(errors.FILE_NOT_FOUND, "Not found", None)

        res.header(
            "Content-Disposition", 'attachment; filename="' +
            self._clients.get_file_info(id)["filename"] + '"')
        res.serve_file(path)
        os.remove(path)
        self._clients.remove_file(id)
    def _on_command(self, cmd, req: HTTPRequest, res: HTTPResponse):
        data = req.body_json()
        id = data["target"]
        sync = data["sync"]
        if not id in self._clients._clients:
            return res.not_found(errors.ID_NOT_FOUND,
                                 "Id " + id + " not found", None)

        c = self._clients._clients[id]
        c.send(cmd)
        if sync:
            r = c.find_response(cmd.id)
            while not r:
                time.sleep(0.01)
                r = c.find_response(cmd.id)
            res.ok(errors.OK, "OK", r)
        else:
            res.ok(errors.OK, "OK", CommandReturn(errors.OK, "").json())