Skip to content

Commit

Permalink
factor util endpoints out to dedicated router
Browse files Browse the repository at this point in the history
  • Loading branch information
sbittrich committed Nov 27, 2023
1 parent 3adf38f commit b7479bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
35 changes: 0 additions & 35 deletions molviewspec/app/api/examples.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import inspect
import math
from typing import Literal, TypeAlias

import requests
from fastapi import APIRouter
from fastapi.responses import FileResponse, JSONResponse, PlainTextResponse, Response
from pydantic import BaseModel, ValidationError

import molviewspec
from app.config import settings
from molviewspec.builder import Representation, Root
from molviewspec.nodes import ComponentExpression
from molviewspec.nodes import validate_state_tree as validate_state_tree_internal
from molviewspec.utils import get_major_version_tag

MVSResponse: TypeAlias = Response
"""Response containing a MVS tree (as JSON)"""
Expand Down Expand Up @@ -304,36 +299,6 @@ async def validation_data(id: str) -> Response:
return JSONResponse(transformed_data)


# Create a custom endpoint to serve the OpenAPI JSON for your Pydantic models
@router.get("/models/openapi.json")
async def models_openapi() -> JSONResponse:
openapi_models = {}

# collect relevant impls
for name, clazz in inspect.getmembers(molviewspec.nodes) + inspect.getmembers(molviewspec.builder):
# TODO suppress 'private' classes?
if inspect.isclass(clazz) and issubclass(clazz, BaseModel) and clazz != BaseModel:
openapi_models[clazz.__name__] = clazz.schema()

openapi_spec = {
"openapi": "3.0.0",
"info": {"title": "MolViewSpec Node Schema OpenAPI", "version": get_major_version_tag()},
"paths": {},
"components": {"schemas": openapi_models},
}

return JSONResponse(content=openapi_spec)


@router.get("/validate-state-tree")
async def validate_state_tree(json: str) -> Response:
try:
validate_state_tree_internal(json)
return JSONResponse({"valid": True})
except ValidationError as e:
return JSONResponse(status_code=422, content=e.json())


##############################################################################
# Examples for frontend testing

Expand Down
41 changes: 41 additions & 0 deletions molviewspec/app/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import inspect

from fastapi import APIRouter
from fastapi.responses import JSONResponse, Response
from pydantic import BaseModel, ValidationError

import molviewspec
from molviewspec.nodes import validate_state_tree as validate_state_tree_internal
from molviewspec.utils import get_major_version_tag

router = APIRouter()


# Create a custom endpoint to serve the OpenAPI JSON for your Pydantic models
@router.get("/models/openapi.json")
async def models_openapi() -> JSONResponse:
openapi_models = {}

# collect relevant impls
for name, clazz in inspect.getmembers(molviewspec.nodes) + inspect.getmembers(molviewspec.builder):
# TODO suppress 'private' classes?
if inspect.isclass(clazz) and issubclass(clazz, BaseModel) and clazz != BaseModel:
openapi_models[clazz.__name__] = clazz.schema()

openapi_spec = {
"openapi": "3.0.0",
"info": {"title": "MolViewSpec Node Schema OpenAPI", "version": get_major_version_tag()},
"paths": {},
"components": {"schemas": openapi_models},
}

return JSONResponse(content=openapi_spec)


@router.get("/validate-state-tree")
async def validate_state_tree(json: str) -> Response:
try:
validate_state_tree_internal(json)
return JSONResponse({"valid": True})
except ValidationError as e:
return JSONResponse(status_code=422, content=e.json())
2 changes: 2 additions & 0 deletions molviewspec/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from fastapi.middleware.gzip import GZipMiddleware

from app.api.examples import router as examples_router
from app.api.utils import router as utils_router

router = APIRouter()
router.include_router(examples_router, prefix="/examples")
router.include_router(utils_router, prefix="/utils")


app = FastAPI(
Expand Down

0 comments on commit b7479bf

Please sign in to comment.