From 37eabda8e68c5b868ba8ce4067a44399bd982112 Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Tue, 24 May 2022 14:50:02 +0100 Subject: [PATCH] Flip if statement to simplify things --- distributed/http/scheduler/api.py | 36 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/distributed/http/scheduler/api.py b/distributed/http/scheduler/api.py index a4cb72a3f5f..42300e14e91 100644 --- a/distributed/http/scheduler/api.py +++ b/distributed/http/scheduler/api.py @@ -14,29 +14,27 @@ def require_auth(method_func): def wrapper(self): auth = self.request.headers.get("Authorization", None) key = dask.config.get("distributed.scheduler.http.api-key") - if ( - key is None - or key == "" - or ( - key - and ( - not auth - or not auth.startswith("Bearer ") - or key != auth.split(" ")[-1] - ) - ) + if key is False or ( + key and auth and auth.startswith("Bearer ") and key == auth.split(" ")[-1] ): - self.set_status(403, "Unauthorized") - return + if not asyncio.iscoroutinefunction(method_func): + return method_func(self) + else: - if not asyncio.iscoroutinefunction(method_func): - return method_func(self) - else: + async def tmp(): + return await method_func(self) - async def tmp(): - return await method_func(self) + return tmp() + else: + self.set_status(403, "Unauthorized") + if not asyncio.iscoroutinefunction(method_func): + return + else: + # When wrapping a coroutine we need to return a coroutine even if it just returns None + async def tmp(): + return - return tmp() + return tmp() return wrapper