mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-08-14 12:00:19 +08:00
first commit of backend app
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
from typing import Generator
|
||||
from sqlmodel import Session
|
||||
from fastapi import Security, HTTPException
|
||||
from fastapi.security.api_key import APIKeyQuery, APIKeyHeader, APIKey
|
||||
from app.database import engine
|
||||
from app.models import ServiceClient
|
||||
|
||||
from starlette.status import HTTP_403_FORBIDDEN
|
||||
|
||||
|
||||
def get_db() -> Generator:
|
||||
with Session(engine) as db:
|
||||
yield db
|
||||
|
||||
|
||||
api_key_query = APIKeyQuery(name="api_key", auto_error=False)
|
||||
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
|
||||
|
||||
|
||||
async def get_api_key(
|
||||
api_key_query: str = Security(api_key_query),
|
||||
api_key_header: str = Security(api_key_header),
|
||||
):
|
||||
if api_key_query:
|
||||
return api_key_query
|
||||
else:
|
||||
return api_key_header
|
||||
|
||||
|
||||
def api_auth(
|
||||
api_key: APIKey, db: Session, create: bool = False, read: bool = True, update: bool = False, delete: bool = False
|
||||
) -> ServiceClient:
|
||||
if api_key is not None:
|
||||
api_client = db.query(ServiceClient).filter(ServiceClient.api_key == api_key).first()
|
||||
if api_client is not None:
|
||||
if (
|
||||
(create == False or api_client.can_append)
|
||||
and (read == False or api_client.can_read)
|
||||
and (update == False or api_client.can_write)
|
||||
and (delete == False or api_client.can_delete)
|
||||
):
|
||||
return api_client
|
||||
|
||||
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials")
|
||||
@@ -0,0 +1,7 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.v1 import labelers, prompts
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(labelers.router, prefix="/labelers", tags=["labelers"])
|
||||
api_router.include_router(prompts.router, prefix="/prompts", tags=["prompts"])
|
||||
@@ -0,0 +1,115 @@
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.security.api_key import APIKey
|
||||
from sqlmodel import Session
|
||||
from starlette.status import HTTP_404_NOT_FOUND, HTTP_400_BAD_REQUEST
|
||||
|
||||
from app import crud, schemas
|
||||
from app.api import deps
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[schemas.Labeler])
|
||||
def read_labelers(
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
begin_id: int = 0,
|
||||
limit: int = 100,
|
||||
) -> Any:
|
||||
"""
|
||||
Retrieve labelers.
|
||||
"""
|
||||
deps.api_auth(api_key, db, read=True)
|
||||
if limit > 10000:
|
||||
raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="Bad request")
|
||||
labelers = crud.labeler.get_multi(db, begin_id=begin_id, limit=limit)
|
||||
return labelers
|
||||
|
||||
|
||||
@router.post("/", response_model=schemas.Labeler)
|
||||
def create_labeler(
|
||||
*,
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
item_in: schemas.LabelerCreate,
|
||||
) -> Any:
|
||||
"""
|
||||
Create new labeler.
|
||||
"""
|
||||
deps.api_auth(api_key, db, create=True)
|
||||
item = crud.labeler.create(db=db, obj_in=item_in)
|
||||
return item
|
||||
|
||||
|
||||
@router.put("/{id}", response_model=schemas.Labeler)
|
||||
def update_labeler(
|
||||
*,
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
id: int,
|
||||
item_in: schemas.LabelerUpdate,
|
||||
) -> Any:
|
||||
"""
|
||||
Update a labeler.
|
||||
"""
|
||||
deps.api_auth(api_key, db, update=True, read=True)
|
||||
item = crud.labeler.get(db=db, id=id)
|
||||
if not item:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
|
||||
item = crud.labeler.update(db=db, db_obj=item, obj_in=item_in)
|
||||
return item
|
||||
|
||||
|
||||
@router.get("/by-username", response_model=schemas.Labeler)
|
||||
def read_labeler_by_username(
|
||||
*,
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
discord_username: str,
|
||||
) -> Any:
|
||||
"""
|
||||
Get labeler by ID.
|
||||
"""
|
||||
deps.api_auth(api_key, db, read=True)
|
||||
item = crud.labeler.get_by_discord_username(db=db, discord_username=discord_username)
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return item
|
||||
|
||||
|
||||
@router.get("/{id}", response_model=schemas.Labeler)
|
||||
def read_labeler(
|
||||
*,
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
id: int,
|
||||
) -> Any:
|
||||
"""
|
||||
Get labeler by ID.
|
||||
"""
|
||||
deps.api_auth(api_key, db, read=True)
|
||||
item = crud.labeler.get(db=db, id=id)
|
||||
if not item:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
|
||||
return item
|
||||
|
||||
|
||||
@router.delete("/{id}", response_model=schemas.Labeler)
|
||||
def delete_labeler(
|
||||
*,
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
id: int,
|
||||
) -> Any:
|
||||
"""
|
||||
Delete a labeler.
|
||||
"""
|
||||
deps.api_auth(api_key, db, delete=True)
|
||||
labeler = crud.labeler.get(db=db, id=id)
|
||||
if not labeler:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
|
||||
labeler = crud.labeler.remove(db=db, id=id)
|
||||
return labeler
|
||||
@@ -0,0 +1,92 @@
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.security.api_key import APIKey
|
||||
from sqlmodel import Session
|
||||
from starlette.status import HTTP_404_NOT_FOUND, HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED
|
||||
|
||||
from app import crud, schemas
|
||||
from app.api import deps
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[schemas.Prompt])
|
||||
def read_prompts(
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
begin_id: int = 0,
|
||||
limit: int = 1000,
|
||||
) -> Any:
|
||||
"""
|
||||
Retrieve prompts.
|
||||
"""
|
||||
deps.api_auth(api_key, db, read=True)
|
||||
if limit > 10000:
|
||||
raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="Bad request")
|
||||
return crud.prompt.get_multi(db, begin_id=begin_id, limit=limit)
|
||||
|
||||
|
||||
@router.post("/", response_model=schemas.Prompt)
|
||||
def create_prompt(
|
||||
*,
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
item_in: schemas.PromptCreate,
|
||||
) -> Any:
|
||||
"""
|
||||
Create new prompt.
|
||||
"""
|
||||
deps.api_auth(api_key, db, create=True)
|
||||
if item_in.labeler_id is None:
|
||||
if item_in.discord_username is None:
|
||||
raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="Bad request")
|
||||
labeler = crud.labeler.get_by_discord_username(db=db, discord_username=item_in.discord_username)
|
||||
else:
|
||||
labeler = crud.labeler.get(db=db, id=item_in.labeler_id)
|
||||
|
||||
if labeler is None:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Invalid labeler user name")
|
||||
if not labeler.is_enabled:
|
||||
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Labeler disabled")
|
||||
|
||||
item_in.labeler_id = labeler.id
|
||||
item_in.discord_username = None
|
||||
item = crud.prompt.create(db=db, obj_in=item_in)
|
||||
return item
|
||||
|
||||
|
||||
@router.get("/{id}", response_model=schemas.Prompt)
|
||||
def read_prompt(
|
||||
*,
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
id: int,
|
||||
) -> Any:
|
||||
"""
|
||||
Get prompt by ID.
|
||||
"""
|
||||
deps.api_auth(api_key, db, read=True)
|
||||
item = crud.prompt.get(db=db, id=id)
|
||||
if not item:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
|
||||
return item
|
||||
|
||||
|
||||
@router.delete("/{id}", response_model=schemas.Prompt)
|
||||
def delete_prompt(
|
||||
*,
|
||||
db: Session = Depends(deps.get_db),
|
||||
api_key: APIKey = Depends(deps.get_api_key),
|
||||
id: int,
|
||||
) -> Any:
|
||||
"""
|
||||
Delete a prompt.
|
||||
"""
|
||||
deps.api_auth(api_key, db, delete=True)
|
||||
item = crud.prompt.get(db=db, id=id)
|
||||
if not item:
|
||||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
|
||||
item = crud.prompt.remove(db=db, id=id)
|
||||
return item
|
||||
Reference in New Issue
Block a user