Skip to content

Commit

Permalink
Merge pull request #1081 from lsst-sqre/tickets/DM-46019
Browse files Browse the repository at this point in the history
DM-46019: Switch to new documenteer REST API method
  • Loading branch information
rra authored Aug 28, 2024
2 parents 3dafc5a + 2019d9d commit 2e2fe7e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 53 deletions.
15 changes: 7 additions & 8 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
ui/.cache/
ui/node_modules/

# VSCode
.vscode/

# Everything below this point is a copy of .gitignore.

# Byte-compiled / optimized / DLL files
Expand Down Expand Up @@ -77,8 +80,8 @@ instance/

# Sphinx documentation
docs/_build/
docs/api/
docs/_static/*.png
docs/_static/openapi.json
docs/dev/internals/

# PyBuilder
target/
Expand Down Expand Up @@ -142,9 +145,5 @@ dmypy.json
# pytype static type analyzer
.pytype/

# Emacs temporary files
.#*
\#*#

# Modified to set up the local development server.
/examples/secrets/github-client-secret
# macOS
.DS_Store
15 changes: 5 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ instance/
.scrapy

# Sphinx documentation
/docs/_build/
/docs/_static/openapi.json
/docs/_static/*.png
/docs/dev/internals/
docs/_build/
docs/_static/openapi.json
docs/dev/internals/

# PyBuilder
target/
Expand Down Expand Up @@ -136,9 +135,5 @@ dmypy.json
# pytype static type analyzer
.pytype/

# Emacs temporary files
.#*
\#*#

# Modified to set up the local development server.
/examples/secrets/github-client-secret
# macOS
.DS_Store
7 changes: 1 addition & 6 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@
REST API
########

Once Gafaelfawr is installed, API documentation is available at ``/auth/docs`` and ``/auth/redoc``.
The latter provides somewhat more detailed information.

You can view a pregenerated version of the Redoc documentation for the current development version of Gafaelfawr by following the link below.

`REST API <rest.html>`__
This is a stub page for the API.
12 changes: 0 additions & 12 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,3 @@
"_rst_epilog.rst",
"_templates/**",
]
redoc = [
{
"name": "REST API",
"page": "rest",
"spec": "_static/openapi.json",
"embed": True,
"opts": {"hide-hostname": True},
}
]
redoc_uri = (
"https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"
)
8 changes: 8 additions & 0 deletions docs/documenteer.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
title = "Gafaelfawr"
copyright = "2020-2022 Association of Universities for Research in Astronomy, Inc. (AURA)"

[project.openapi]
openapi_path = "_static/openapi.json"
doc_path = "api"

[project.openapi.generator]
function = "gafaelfawr.main:create_openapi"
keyword_args = {add_back_link = true}

[project.python]
package = "gafaelfawr"

Expand Down
20 changes: 4 additions & 16 deletions src/gafaelfawr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import asyncio
import json
import os
import subprocess
import sys
Expand All @@ -15,7 +14,6 @@
import uvicorn
from alembic.config import Config
from cryptography.fernet import Fernet
from fastapi.openapi.utils import get_openapi
from safir.asyncio import run_with_asyncio
from safir.click import display_help
from safir.database import create_database_engine
Expand All @@ -30,7 +28,7 @@
from .dependencies.config import config_dependency
from .factory import Factory
from .keypair import RSAKeyPair
from .main import create_app
from .main import create_openapi
from .metrics import StateMetrics
from .models.token import Token
from .schema import Base
Expand Down Expand Up @@ -257,22 +255,12 @@ async def maintenance(*, config_path: Path | None) -> None:
)
def openapi_schema(*, add_back_link: bool, output: Path | None) -> None:
"""Generate the OpenAPI schema."""
app = create_app(load_config=False, validate_schema=False)
description = app.description
if add_back_link:
description += "\n\n[Return to Gafaelfawr documentation](api.html)."
schema = get_openapi(
title=app.title,
description=description,
version=app.version,
routes=app.routes,
)
schema = create_openapi(add_back_link=add_back_link)
if output:
output.parent.mkdir(exist_ok=True)
with output.open("w") as f:
json.dump(schema, f)
output.write_text(schema)
else:
json.dump(schema, sys.stdout)
sys.stdout.write(schema)


@main.command()
Expand Down
30 changes: 30 additions & 0 deletions src/gafaelfawr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import json
import os
from collections.abc import AsyncIterator, Coroutine
from contextlib import asynccontextmanager
Expand All @@ -10,6 +11,7 @@

import structlog
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from fastapi.staticfiles import StaticFiles
from opentelemetry.sdk.metrics.export import MetricReader
from safir.dependencies.db_session import db_session_dependency
Expand Down Expand Up @@ -188,3 +190,31 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
app.exception_handler(ClientRequestError)(client_request_error_handler)

return app


def create_openapi(*, add_back_link: bool = False) -> str:
"""Generate the OpenAPI schema.
Parameters
----------
add_back_link
Whether to add a back link to the parent page to the description.
This is useful when the schema will be rendered as part of the
documentation.
Returns
-------
str
OpenAPI schema as serialized JSON.
"""
app = create_app(load_config=False, validate_schema=False)
description = app.description
if add_back_link:
description += "\n\n[Return to Gafaelfawr documentation](.)."
schema = get_openapi(
title=app.title,
description=description,
version=app.version,
routes=app.routes,
)
return json.dumps(schema)
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ commands =
rm -rf docs/dev/internals
# https://github.com/sphinx-contrib/redoc/issues/48
rm -f docs/_build/html/_static/redoc.js
gafaelfawr openapi-schema --add-back-link --output docs/_static/openapi.json
sphinx-build -W --keep-going -n -T -b html -d {envtmpdir}/doctrees docs docs/_build/html

[testenv:docs-linkcheck]
Expand Down

0 comments on commit 2e2fe7e

Please sign in to comment.