|
1 | | -from fastapi import APIRouter, Depends, HTTPException, status |
| 1 | +import yaml |
| 2 | +from fastapi import ( |
| 3 | + APIRouter, |
| 4 | + Depends, |
| 5 | + HTTPException, |
| 6 | + Query, |
| 7 | + Response, |
| 8 | + status, |
| 9 | +) |
| 10 | +from fastapi.responses import JSONResponse |
2 | 11 | from sqlalchemy.orm import Session |
3 | 12 |
|
4 | 13 | from app.core.database import get_db |
5 | 14 | from app.modules.events import crud as event_crud |
6 | 15 | from app.modules.events.schemas import EventCreate, EventOut |
| 16 | +from app.modules.events.service import generate_json_schema_for_event |
7 | 17 | from app.modules.fields.crud import get_fields_by_ids |
8 | 18 | from app.modules.tags.crud import get_or_create_tags |
9 | 19 |
|
@@ -111,3 +121,48 @@ def delete_event_route(event_id: int, db: Session = Depends(get_db)): |
111 | 121 | if db_event is None: |
112 | 122 | raise HTTPException(status_code=404, detail="Event not found") |
113 | 123 | return db_event |
| 124 | + |
| 125 | + |
| 126 | +@router.get("/events/{event_id}/schema.json", response_class=JSONResponse) |
| 127 | +def get_event_json_schema( |
| 128 | + event_id: int, |
| 129 | + include_descriptions: bool = Query(True), |
| 130 | + include_examples: bool = Query(True), |
| 131 | + additional_properties: bool = Query(True), |
| 132 | + db: Session = Depends(get_db), |
| 133 | +): |
| 134 | + db_event = event_crud.get_event(db=db, event_id=event_id) |
| 135 | + if not db_event: |
| 136 | + raise HTTPException(status_code=404, detail="Event not found") |
| 137 | + |
| 138 | + event = EventOut.model_validate(db_event) |
| 139 | + schema = generate_json_schema_for_event( |
| 140 | + event, |
| 141 | + include_descriptions=include_descriptions, |
| 142 | + include_examples=include_examples, |
| 143 | + additional_properties=additional_properties, |
| 144 | + ) |
| 145 | + return schema |
| 146 | + |
| 147 | + |
| 148 | +@router.get("/events/{event_id}/schema.yaml") |
| 149 | +def get_event_yaml_schema( |
| 150 | + event_id: int, |
| 151 | + include_descriptions: bool = Query(True), |
| 152 | + include_examples: bool = Query(True), |
| 153 | + additional_properties: bool = Query(True), |
| 154 | + db: Session = Depends(get_db), |
| 155 | +): |
| 156 | + db_event = event_crud.get_event(db=db, event_id=event_id) |
| 157 | + if not db_event: |
| 158 | + raise HTTPException(status_code=404, detail="Event not found") |
| 159 | + |
| 160 | + event = EventOut.model_validate(db_event) |
| 161 | + schema = generate_json_schema_for_event( |
| 162 | + event, |
| 163 | + include_descriptions=include_descriptions, |
| 164 | + include_examples=include_examples, |
| 165 | + additional_properties=additional_properties, |
| 166 | + ) |
| 167 | + yaml_data = yaml.dump(schema, sort_keys=False) |
| 168 | + return Response(content=yaml_data, media_type="application/x-yaml") |
0 commit comments