async def validate_toolbox(toolbox: Toolbox) -> None:
try:
async with httpx.AsyncClient() as client:
resp = await client.get(toolbox.openapi_url) # type: ignore[arg-type]
except Exception as e:
raise HTTPException(status_code=422, detail="OpenAPI URL is invalid") from e
if not (resp.status_code >= 200 and resp.status_code < 400):
raise HTTPException(
status_code=422, detail=f"OpenAPI URL returns error code {resp.status_code}"
)
try:
if "yaml" in toolbox.openapi_url or "yml" in toolbox.openapi_url: # type: ignore [operator]
openapi_spec = yaml.safe_load(resp.text)
else:
openapi_spec = resp.json()
if "openapi" not in openapi_spec:
raise HTTPException(
status_code=422,
detail="OpenAPI URL does not contain a valid OpenAPI spec",
)
except Exception as e:
raise HTTPException(
status_code=422, detail="OpenAPI URL does not contain a valid OpenAPI spec"
) from e