Skip to content

Commit c47ed33

Browse files
committed
Add api-generation support w/ swagger-ui-tag
closes: #40
1 parent 73fd738 commit c47ed33

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies = [
1212
"mkdocs-macros-plugin",
1313
"mkdocs-site-urls",
1414
"mkdocs-literate-nav",
15+
"mkdocs-swagger-ui-tag",
1516
"importlib_resources",
1617
"httpx",
1718
"rich",

src/pulp_docs/data/mkdocs.yml

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ plugins:
6363
separate_signature: false
6464
members_order: "source"
6565
merge_init_into_class: true
66+
- swagger-ui-tag:
67+
docExpansion: none
68+
filter_filenames: ["rest_api"]
69+
70+
6671
extra:
6772
social:
6873
- icon: fontawesome/brands/github-alt

src/pulp_docs/mkdocs_macros.py

+49-11
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
import json
1919
import logging
20-
import os
2120
import shutil
2221
import tempfile
2322
import time
2423
from pathlib import Path
2524

25+
import httpx
2626
import rich
2727

2828
from pulp_docs.cli import Config
@@ -89,6 +89,7 @@ def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
8989
# Download/copy source code to tmpdir
9090
repo_sources = TMPDIR / "repo_sources"
9191
repo_docs = TMPDIR / "repo_docs"
92+
api_src_dir = TMPDIR / "api_json"
9293
shutil.rmtree(repo_sources, ignore_errors=True)
9394
shutil.rmtree(repo_docs, ignore_errors=True)
9495

@@ -102,10 +103,13 @@ def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
102103
else:
103104
this_src_dir = repo_sources / repo_or_pkg.subpackage_of / repo_or_pkg.name
104105

106+
# restapi
107+
if repo_or_pkg.type in ("content", "core"):
108+
_download_api_json(api_src_dir, repo_or_pkg.name)
109+
_generate_rest_api_page(this_src_dir, repo_or_pkg.name, repo_or_pkg.title)
110+
105111
# install and post-process
106-
_place_doc_files(this_src_dir, this_docs_dir, repo_or_pkg)
107-
if repo_or_pkg.type == "content":
108-
_generate_rest_api_page(this_docs_dir, repo_or_pkg.name, repo_or_pkg.title)
112+
_place_doc_files(this_src_dir, this_docs_dir, repo_or_pkg, api_src_dir)
109113

110114
end = time.perf_counter()
111115
log.info(f"{repo_or_pkg.name} completed in {end - start:.2} sec")
@@ -124,7 +128,27 @@ def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
124128
return (repo_docs, repo_sources)
125129

126130

127-
def _place_doc_files(src_dir: Path, docs_dir: Path, repo: Repo):
131+
def _download_api_json(api_dir: Path, repo_name: str):
132+
api_json_path = api_dir / f"{repo_name}/api.json"
133+
if api_json_path.exists():
134+
log.info(f"{repo_name} api.json already downloaded.")
135+
return
136+
137+
log.info(f"Downloading api.json for {repo_name}")
138+
api_url_1 = "https://docs.pulpproject.org/{repo_name}/api.json"
139+
api_url_2 = "https://docs.pulpproject.org/{repo_name}/_static/api.json"
140+
response = httpx.get(api_url_1.format(repo_name=repo_name))
141+
if response.is_error:
142+
response = httpx.get(api_url_2.format(repo_name=repo_name))
143+
if response.is_error:
144+
raise Exception("Couldnt get rest api page")
145+
146+
api_json_path.parent.mkdir(parents=True, exist_ok=True)
147+
api_json_path.write_text(json.dumps(response.json()))
148+
log.info("Done.")
149+
150+
151+
def _place_doc_files(src_dir: Path, docs_dir: Path, repo: Repo, api_src_dir: Path):
128152
"""
129153
Copy only doc-related files from src_dir to doc_dir.
130154
@@ -148,6 +172,11 @@ def _place_doc_files(src_dir: Path, docs_dir: Path, repo: Repo):
148172
Path(docs_dir / "docs").mkdir(parents=True)
149173
repo.status.has_staging_docs = False
150174

175+
# Get restapi
176+
if repo.type in ("content", "core"):
177+
api_json = api_src_dir / f"{repo.name}/api.json"
178+
shutil.copy(api_json, docs_dir / "docs/api.json")
179+
151180
# Get changelog
152181
repo.status.has_changelog = False
153182
changes_dir = Path(docs_dir)
@@ -175,15 +204,24 @@ def _place_doc_files(src_dir: Path, docs_dir: Path, repo: Repo):
175204
)
176205

177206

207+
RESTAPI_TEMPLATE = """\
208+
---
209+
hide:
210+
- toc
211+
---
212+
213+
# {repo_title} REST Api
214+
215+
<swagger-ui src="api.json"/>
216+
"""
217+
218+
178219
def _generate_rest_api_page(docs_dir: Path, repo_name: str, repo_title: str):
179220
"""Create page that contain a link to the rest api, based on the project url template"""
180221
log.info("Generating REST_API page")
181-
rest_api_page = docs_dir / "docs" / "rest_api.md"
182-
rest_api_page.touch()
183-
restapi_url = RESTAPI_URL_TEMPLATE.format(repo_name)
184-
md_title = f"# {repo_title} REST Api"
185-
md_body = f"[{restapi_url}]({restapi_url})"
186-
rest_api_page.write_text(f"{md_title}\n\n{md_body}")
222+
rest_api_page = docs_dir / "staging_docs" / "rest_api.md"
223+
rest_api_page.parent.mkdir(parents=True, exist_ok=True)
224+
rest_api_page.write_text(RESTAPI_TEMPLATE.format(repo_title=repo_title))
187225

188226

189227
def print_user_repo(repos: Repos, config: Config):

src/pulp_docs/repository.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
from __future__ import annotations
88

9+
import json
910
import logging
1011
import os
1112
import shutil
1213
import subprocess
1314
import tarfile
1415
import tempfile
1516
import typing as t
16-
from collections import ChainMap, defaultdict
1717
from dataclasses import dataclass, field
1818
from io import BytesIO
1919
from pathlib import Path
@@ -116,13 +116,13 @@ def download(self, dest_dir: Path, clear_cache: bool = False) -> str:
116116
# from remote
117117
elif not cached_repo.exists():
118118
log_header = "Downloading from remote"
119+
src_copy_path = DOWNLOAD_CACHE_DIR / self.name
119120
download_from = download_from_gh_main(
120-
DOWNLOAD_CACHE_DIR / self.name,
121+
src_copy_path,
121122
self.owner,
122123
self.name,
123124
self.branch_in_use,
124125
)
125-
src_copy_path = DOWNLOAD_CACHE_DIR / self.name
126126

127127
# copy from source/cache to pulp-docs workdir
128128
log.info(f"{log_header}: source={download_from}, copied_from={src_copy_path}")
@@ -133,12 +133,15 @@ def download(self, dest_dir: Path, clear_cache: bool = False) -> str:
133133
src_copy_path,
134134
dest_dir,
135135
ignore=shutil.ignore_patterns(*ignore_patterns),
136+
dirs_exist_ok=True
136137
)
137138

138139
self.status.download_source = str(download_from)
139140
return self.status.download_source
140141

141142

143+
144+
142145
def download_from_gh_main(dest_dir: Path, owner: str, name: str, branch: str):
143146
"""
144147
Download repository source-code from main

0 commit comments

Comments
 (0)