handlers.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Tornado handlers for frontend config storage."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import json
  5. from tornado import web
  6. from jupyter_server.auth.decorator import authorized
  7. from ...base.handlers import APIHandler
  8. AUTH_RESOURCE = "config"
  9. class ConfigHandler(APIHandler):
  10. """A config API handler."""
  11. auth_resource = AUTH_RESOURCE
  12. @web.authenticated
  13. @authorized
  14. def get(self, section_name):
  15. """Get config by section name."""
  16. self.set_header("Content-Type", "application/json")
  17. self.finish(json.dumps(self.config_manager.get(section_name)))
  18. @web.authenticated
  19. @authorized
  20. def put(self, section_name):
  21. """Set a config section by name."""
  22. data = self.get_json_body() # Will raise 400 if content is not valid JSON
  23. self.config_manager.set(section_name, data)
  24. self.set_status(204)
  25. @web.authenticated
  26. @authorized
  27. def patch(self, section_name):
  28. """Update a config section by name."""
  29. new_data = self.get_json_body()
  30. section = self.config_manager.update(section_name, new_data)
  31. self.finish(json.dumps(section))
  32. # URL to handler mappings
  33. section_name_regex = r"(?P<section_name>\w+)"
  34. default_handlers = [
  35. (r"/api/config/%s" % section_name_regex, ConfigHandler),
  36. ]