Merge branch 'v1_db_schema' into main

This commit is contained in:
Andreas Köpf
2022-12-16 09:48:06 +01:00
15 changed files with 498 additions and 36 deletions
+10 -18
View File
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
from typing import Generator
from uuid import UUID
from app.config import settings
from app.database import engine
from app.models import ServiceClient
from app.models import ApiClient
from fastapi import HTTPException, Security
from fastapi.security.api_key import APIKey, APIKeyHeader, APIKeyQuery
from sqlmodel import Session
@@ -32,24 +32,16 @@ async def get_api_key(
def api_auth(
api_key: APIKey,
db: Session,
create: bool = False,
read: bool = True,
update: bool = False,
delete: bool = False,
) -> ServiceClient:
) -> ApiClient:
if api_key is not None:
if settings.ALLOW_ANY_API_KEY:
return ServiceClient(
api_key=api_key, name=api_key, can_append=True, can_read=True, can_write=True, can_delete=True
return ApiClient(
id=UUID('00000000-1111-2222-3333-444444444444'),
api_key=api_key, name=api_key
)
api_client = db.query(ServiceClient).filter(ServiceClient.api_key == api_key).first()
if api_client is not None:
if (
(create is False or api_client.can_append)
and (read is False or api_client.can_read)
and (update is False or api_client.can_write)
and (delete is False or api_client.can_delete)
):
return api_client
api_client = db.query(ApiClient).filter(ApiClient.api_key == api_key).first()
if api_client is not None and api_client.enabled:
return api_client
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials")
+6 -6
View File
@@ -21,7 +21,7 @@ def read_labelers(
"""
Retrieve labelers.
"""
deps.api_auth(api_key, db, read=True)
deps.api_auth(api_key, db)
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)
@@ -38,7 +38,7 @@ def create_labeler(
"""
Create new labeler.
"""
deps.api_auth(api_key, db, create=True)
deps.api_auth(api_key, db)
item = crud.labeler.create(db=db, obj_in=item_in)
return item
@@ -54,7 +54,7 @@ def update_labeler(
"""
Update a labeler.
"""
deps.api_auth(api_key, db, update=True, read=True)
deps.api_auth(api_key, db)
item = crud.labeler.get(db=db, id=id)
if not item:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
@@ -72,7 +72,7 @@ def read_labeler_by_username(
"""
Get labeler by ID.
"""
deps.api_auth(api_key, db, read=True)
deps.api_auth(api_key, db)
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")
@@ -89,7 +89,7 @@ def read_labeler(
"""
Get labeler by ID.
"""
deps.api_auth(api_key, db, read=True)
deps.api_auth(api_key, db)
item = crud.labeler.get(db=db, id=id)
if not item:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
@@ -106,7 +106,7 @@ def delete_labeler(
"""
Delete a labeler.
"""
deps.api_auth(api_key, db, delete=True)
deps.api_auth(api_key, db)
labeler = crud.labeler.get(db=db, id=id)
if not labeler:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
+4 -4
View File
@@ -21,7 +21,7 @@ def read_prompts(
"""
Retrieve prompts.
"""
deps.api_auth(api_key, db, read=True)
deps.api_auth(api_key, db)
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)
@@ -37,7 +37,7 @@ def create_prompt(
"""
Create new prompt.
"""
deps.api_auth(api_key, db, create=True)
deps.api_auth(api_key, db)
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")
@@ -66,7 +66,7 @@ def read_prompt(
"""
Get prompt by ID.
"""
deps.api_auth(api_key, db, read=True)
deps.api_auth(api_key, db)
item = crud.prompt.get(db=db, id=id)
if not item:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
@@ -83,7 +83,7 @@ def delete_prompt(
"""
Delete a prompt.
"""
deps.api_auth(api_key, db, delete=True)
deps.api_auth(api_key, db)
item = crud.prompt.get(db=db, id=id)
if not item:
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Item not found")
+3 -3
View File
@@ -55,7 +55,7 @@ def request_task(
"""
Create new task.
"""
deps.api_auth(api_key, db, create=True)
deps.api_auth(api_key, db)
try:
task = generate_task(request)
@@ -79,7 +79,7 @@ def acknowledge_task(
"""
The frontend acknowledges a task.
"""
deps.api_auth(api_key, db, create=True)
deps.api_auth(api_key, db)
match (type(response)):
case protocol_schema.PostCreatedTaskResponse:
@@ -107,7 +107,7 @@ def post_interaction(
"""
The frontend reports an interaction.
"""
deps.api_auth(api_key, db, create=True)
deps.api_auth(api_key, db)
match (type(interaction)):
case protocol_schema.TextReplyToPost: