Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Oct 17, 2022
1 parent e69ed2f commit 16cd786
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
18 changes: 12 additions & 6 deletions jupyter_server_fileid/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from jupyter_server_fileid.manager import FileIdManager

from .handlers import FileIdHandler
from .handlers import FilePath2IdHandler, FileId2PathHandler


class FileIdExtension(ExtensionApp):
Expand Down Expand Up @@ -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,
)
],
)
48 changes: 46 additions & 2 deletions jupyter_server_fileid/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 16cd786

Please sign in to comment.