From 9f6c9563b9afdfe4a3bfb58eb107941566a79e26 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Tue, 22 Oct 2019 23:25:56 -0400 Subject: [PATCH] Create a router for methods --- phial.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/phial.py b/phial.py index 081f262..5d083d3 100644 --- a/phial.py +++ b/phial.py @@ -191,6 +191,50 @@ class RedirectResponse(_RedirectResponse): def content(self): return (b'',) +REQUEST_METHODS = ( + 'GET', + 'HEAD', + 'POST', + 'PUT', + 'PATCH', + 'DELETE', + 'CONNECT', + 'OPTIONS', + 'TRACE', +) + +def default_method_not_allowed_handler(request): + return Response('') + +def default_options_handler(handlers): + def handler(request): + return Response(','.join(handlers.keys())) + return handler + +def route_on_method(**kwargs): + handlers = {} + for method in REQUEST_METHODS: + if method in kwargs: + handlers[method] = kwargs.pop(method) + + method_not_allowed_handler = kwargs.pop( + method_not_allowed, + default_method_not_allowed_handler, + ) + + assert len(kwargs) == 0 + + if 'OPTIONS' not in handlers: + handlers['OPTIONS'] = default_options_handler(handlers) + + def handler(request): + return handlers.get( + request.method.upper(), + method_not_allowed_handler, + )(request) + + return handler + def _get_status(response): return { 200: '200 OK', -- 2.20.1