1717
1818import json
1919import logging
20- import os
2120import shutil
2221import tempfile
2322import time
2423from pathlib import Path
2524
25+ import httpx
2626import rich
2727
2828from 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+
178219def _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
189227def print_user_repo (repos : Repos , config : Config ):
0 commit comments