Commit 6108235d authored by Hines, Jesse's avatar Hines, Jesse
Browse files

Add github pages with api docs

parent 9dc675af
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
name: Export openapi.json
on:
  push:
    branches:
      - main
jobs:
  export:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4.2.2

      - uses: actions/setup-python@v5
        id: setup_python
        with:
          python-version: '3.9'
          cache: 'pip'
      - run: python3 -m pip install -e .

      - run: |
          cd api_docs
          wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.18.2.tar.gz -O swagger-ui.tar.gz
          tar -xvf swagger-ui.tar.gz
          mv swagger-ui-*/dist dist
          cp swagger-initializer.js dist

      - run: ./scripts/export_openapi.py simulation_server.server.main app --out api_docs/dist/openapi.json

      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@v4.7.2
        with:
          branch: gh-pages
          folder: api_docs/dist
+3 −0
Original line number Diff line number Diff line
@@ -23,3 +23,6 @@ If you want to run replay data locally, you'll need to download the datasets (se
and then ingest them in Druid. After launching, you can access the Druid UI at http://localhost:8888
and submit druid ingests for the system you want.

## API Docs
You can view the API docs and the `openapi.json` with the API specification at
https://exadigit.github.io/SimulationServer
+41 −0
Original line number Diff line number Diff line
window.onload = function() {
  //<editor-fold desc="Changeable Configuration Block">

  // the following lines will be replaced by docker/configurator, when it runs in a docker-container
  window.ui = SwaggerUIBundle({
    url: "https://petstore.swagger.io/v2/swagger.json",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  });

  //</editor-fold>
};
  window.onload = function() {
    //<editor-fold desc="Changeable Configuration Block">

    // the following lines will be replaced by docker/configurator, when it runs in a docker-container
    window.ui = SwaggerUIBundle({
      url: "./openapi.json",
      dom_id: '#swagger-ui',
      deepLinking: true,
      presets: [
        SwaggerUIBundle.presets.apis,
        SwaggerUIStandalonePreset
      ],
      plugins: [
        SwaggerUIBundle.plugins.DownloadUrl
      ],
      layout: "StandaloneLayout",
      supportedSubmitMethods: [], // Disable "Try it out" buttons
    });

    //</editor-fold>
  };
+51 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
"""
Export the openapi.json file from a FastAPI application.
"""
import sys, json, argparse
import importlib.util
from pathlib import Path
import fastapi


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description = __doc__.strip(),
        allow_abbrev = False,
        formatter_class = argparse.RawDescriptionHelpFormatter,
    )

    parser.add_argument('module',
        help = 'Python module containing the FastAPI, e.g. simulation_server.server.main',
    )
    parser.add_argument('var',
        help = 'Name of FastAPI app within the module, or a function that returns a FastAPI app',
    )
    parser.add_argument('--out', default = "openapi.json", help = 'Path to output the json.')
    args = parser.parse_args()

    module_import_path = args.module
    var = args.var
    out = Path(args.out).resolve()

    module = importlib.import_module(module_import_path)

    if not hasattr(module, var):
        print(f"{module_import_path} has no attribute named {var}")
        sys.exit(1)
    
    app = getattr(module, var)

    if type(app) != fastapi.FastAPI:
        if callable(app):
            app = app()
            if type(app) != fastapi.FastAPI:
                print(f"{module_import_path}:{var}() does not return a FastAPI instance")
                sys.exit(1)
        else:
            print(f"{module_import_path}:{var} is not a FastAPI instance")
            sys.exit(1)

    openapi = app.openapi()
    out.parent.mkdir(exist_ok = True, parents = True)
    out.write_text(json.dumps(openapi, indent="  "))
+1 −1
Original line number Diff line number Diff line

#!/usr/bin/env python3
from pathlib import Path
import pandas as pd
import sys
Loading