py-scopes/scopes/api/main.py

45 lines
963 B
Python

# scopes.api.main
import sys
if '' not in sys.path:
sys.path = [''] + sys.path
import config
from scopes.storage import tracking
from scopes.storage.common import Storage
import transaction
from fastapi import Depends, FastAPI
from fastapi.responses import HTMLResponse
from typing import Annotated
# dependencies
def storage():
stf = config.StorageFactory(config)
return stf(config.dbschema)
# app and routes
app = FastAPI()
@app.get('/')
async def read_root():
return {'Hello': 'World'}
@app.get('/demo')
async def read_demo():
return HTMLResponse('<b>Hello World</b>')
@app.get('/persons/{name}')
async def read_person(name: str, q: str | None = None):
return {'name': name, 'query': q}
@app.get('/tracks')
async def get_tracks(storage: Annotated[Storage, Depends(storage)]):
tracks = storage.create(tracking.Container)
result = [tr.asDict() for tr in tracks.query()]
transaction.commit()
return result