From: Christian Heller Date: Thu, 13 Jun 2024 23:31:00 +0000 (+0200) Subject: Improve clarity of request wrapping code. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/tasks?a=commitdiff_plain;h=7171109f94bc2b532bc2f25bcecffa99d8c4a401;p=plomtask Improve clarity of request wrapping code. --- diff --git a/plomtask/http.py b/plomtask/http.py index 72ad872..d61588a 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -123,10 +123,10 @@ class TaskHandler(BaseHTTPRequestHandler): self.wfile.write(bytes(html, 'utf-8')) @staticmethod - def _wrap(method: str, not_found_msg: str - ) -> Callable[..., Callable[..., None]]: - def decorator(http_method_handler: Callable[..., str] - ) -> Callable[..., None]: + def _request_wrapper(http_method: str, not_found_msg: str + ) -> Callable[..., Callable[[TaskHandler], None]]: + def decorator(f: Callable[..., str | None] + ) -> Callable[[TaskHandler], None]: def wrapper(self: TaskHandler) -> None: try: self.conn = DatabaseConnection(self.server.db) @@ -134,10 +134,10 @@ class TaskHandler(BaseHTTPRequestHandler): self.site = path_split(parsed_url.path)[1] params = parse_qs(parsed_url.query, strict_parsing=True) self.params = InputsParser(params, False) - handler_name = f'do_{method}_{self.site}' + handler_name = f'do_{http_method}_{self.site}' if hasattr(self, handler_name): handler = getattr(self, handler_name) - redir_target = http_method_handler(self, handler) + redir_target = f(self, handler) if redir_target: self.send_response(302) self.send_header('Location', redir_target) @@ -154,8 +154,8 @@ class TaskHandler(BaseHTTPRequestHandler): return wrapper return decorator - @_wrap('GET', 'Unknown page') - def do_GET(self, handler: Callable[..., str | dict[str, object]] + @_request_wrapper('GET', 'Unknown page') + def do_GET(self, handler: Callable[[], str | dict[str, object]] ) -> str | None: """Render page with result of handler, or redirect if result is str.""" template = f'{self.site}.html' @@ -167,8 +167,8 @@ class TaskHandler(BaseHTTPRequestHandler): self.send_html(html) return None - @_wrap('POST', 'Unknown POST target') - def do_POST(self, handler: Callable[..., str]) -> str: + @_request_wrapper('POST', 'Unknown POST target') + def do_POST(self, handler: Callable[[], str]) -> str: """Handle POST with handler, prepare redirection to result.""" length = int(self.headers['content-length']) postvars = parse_qs(self.rfile.read(length).decode(),