home · contact · privacy
Improve clarity of request wrapping code.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 13 Jun 2024 23:31:00 +0000 (01:31 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 13 Jun 2024 23:31:00 +0000 (01:31 +0200)
plomtask/http.py

index 72ad872befe0f663dc6435a5ee8584e66b31a29b..d61588a9f7627eb7e0ce1acff8fe211d7065ba1c 100644 (file)
@@ -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(),