spec.py 825 B

12345678910111213141516171819202122232425262728293031
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """OpenAPI spec utils."""
  4. from __future__ import annotations
  5. import os
  6. import typing
  7. from pathlib import Path
  8. if typing.TYPE_CHECKING:
  9. from openapi_core.spec.paths import Spec
  10. HERE = Path(os.path.dirname(__file__)).resolve()
  11. def get_openapi_spec() -> Spec:
  12. """Get the OpenAPI spec object."""
  13. from openapi_core.spec.paths import Spec
  14. openapi_spec_dict = get_openapi_spec_dict()
  15. return Spec.from_dict(openapi_spec_dict) # type:ignore[arg-type]
  16. def get_openapi_spec_dict() -> dict[str, typing.Any]:
  17. """Get the OpenAPI spec as a dictionary."""
  18. from ruamel.yaml import YAML
  19. path = HERE / "rest-api.yml"
  20. yaml = YAML(typ="safe")
  21. return yaml.load(path.read_text(encoding="utf-8"))