From 16cd786c483fdc62b1c9bb856ac1506e5facd11f Mon Sep 17 00:00:00 2001 From: David Brochart Date: Mon, 17 Oct 2022 09:17:42 +0200 Subject: [PATCH] Review --- jupyter_server_fileid/extension.py | 18 +++++++---- jupyter_server_fileid/handlers.py | 48 ++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/jupyter_server_fileid/extension.py b/jupyter_server_fileid/extension.py index 6e2b3ad..d3a152a 100644 --- a/jupyter_server_fileid/extension.py +++ b/jupyter_server_fileid/extension.py @@ -4,7 +4,7 @@ from jupyter_server_fileid.manager import FileIdManager -from .handlers import FileIdHandler +from .handlers import FilePath2IdHandler, FileId2PathHandler class FileIdExtension(ExtensionApp): @@ -48,9 +48,15 @@ async def cm_listener(logger: EventLogger, schema_id: str, data: dict) -> None: ) def initialize_handlers(self): - self.handlers.append( - ( - r"/api/fileid/(.*)", - FileIdHandler, - ) + self.handlers.extend( + [ + ( + r"/api/fileid/id/(.*)", + FilePath2IdHandler, + ), + ( + r"/api/fileid/path/(.*)", + FileId2PathHandler, + ) + ], ) diff --git a/jupyter_server_fileid/handlers.py b/jupyter_server_fileid/handlers.py index 2d48c9c..4e0a8e0 100644 --- a/jupyter_server_fileid/handlers.py +++ b/jupyter_server_fileid/handlers.py @@ -9,13 +9,57 @@ class FileIdAPIHandler(APIHandler): auth_resource = AUTH_RESOURCE -class FileIdHandler(FileIdAPIHandler): +class FilePath2IdHandler(FileIdAPIHandler): + @web.authenticated @authorized async def get(self, path): - idx = self.settings["file_id_manager"].index(path) + manager = self.settings["file_id_manager"] + idx = manager.get_id(path) if idx is None: + # index does not exist raise web.HTTPError(404, f"File {path!r} does not exist") + # index exists, return it return self.finish(str(idx)) + + @web.authenticated + @authorized + async def put(self, path): + manager = self.settings["file_id_manager"] + + idx = manager.get_id(path) + if idx is not None: + # index already exists + self.set_status(200) + return self.finish(str(idx)) + + # try indexing + idx = manager.index(path) + if idx is None: + # file does not exists + raise web.HTTPError(404, f"File {path!r} does not exist") + + # index successfully created + self.set_status(201) + return self.finish(str(idx)) + +class FileId2PathHandler(FileIdAPIHandler): + + @web.authenticated + @authorized + async def get(self, idx): + manager = self.settings["file_id_manager"] + + path = manager.get_path(idx) + + if path is None: + raise web.HTTPError(404, f"No ID found for file {path!r}") + + return self.finish(str(idx)) + + @web.authenticated + @authorized + async def put(self, idx): + raise web.HTTPError(501, "Cannot set ID for a file")