"/response" endpoint is now functional

This commit is contained in:
Alexis Métaireau 2023-10-05 11:05:51 +02:00
parent f41e74d402
commit fa092ad75b
4 changed files with 23 additions and 27 deletions

View file

@ -25,11 +25,11 @@ async def complete_task(client: httpx.AsyncClient, task: dict) -> dict:
async def post_results(client: httpx.AsyncClient, server: str, results: List[ClientResult]):
json_results = [r.model_dump_json() for r in results]
response = await client.post(f"{server}/results", json={"results": json_results})
data = [r.model_dump() for r in results]
response = await client.post(f"{server}/results", json=data)
if response.status_code == httpx.codes.OK:
logger.info("Successfully posted results")
if response.status_code == httpx.codes.CREATED:
logger.error(f"Successfully posted results {response.json()}")
else:
logger.error(f"Failed to post results: {response.read()}")

View file

@ -41,14 +41,10 @@ async def read_tasks(request: Request, limit: int = 20, db: Session = Depends(ge
return tasks
class Results(BaseModel):
results: List[ClientResult]
@app.post("/results", status_code=201)
async def create_result(results: Results, db: Session = Depends(get_db)):
result_ids = []
async def create_result(results: List[ClientResult], db: Session = Depends(get_db)):
db_results = []
for client_result in results:
result_id = await queries.create_result(db, client_result)
db_results.append(await queries.create_result(db, client_result))
db.commit()
result_ids.append(result_id)
return {"result_ids": result_ids}
return {"result_ids": [r.id for r in db_results]}

View file

@ -1,5 +1,5 @@
from typing import List
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, JSON, DateTime
from typing import List, Literal
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, JSON, DateTime, Enum
from sqlalchemy.orm import relationship, Mapped, mapped_column, DeclarativeBase
from sqlalchemy_utils import ChoiceType
from sqlalchemy.orm import mapped_column
@ -40,5 +40,6 @@ class Result(Base):
id: Mapped[int] = mapped_column(primary_key=True)
submitted_at: Mapped[datetime] = mapped_column()
success: Mapped[bool] = mapped_column()
content: Mapped[str] = mapped_column()
status: Mapped[Literal["success", "failure", "error"]] =\
mapped_column(Enum("success", "failure", "error"))
context: Mapped[dict] = mapped_column()

View file

@ -10,8 +10,7 @@ from datetime import datetime
async def list_tasks(db: Session, client_id: str, limit: int = 100):
"""List tasks and mark them as selected
"""
"""List tasks and mark them as selected"""
tasks = db.query(Task).where(Task.selected_by == None).limit(limit).all()
now = datetime.now()
# XXX: Deactivated for now, as it simplifies testing.
@ -21,14 +20,15 @@ async def list_tasks(db: Session, client_id: str, limit: int = 100):
# db.commit()
return tasks
async def create_result(db: Session, client_result:schemas.ClientResult):
result = Result()
result.submitted_at = datetime.now()
result.success = True if client_result.status == "success" else False
result.content = str(client_result.context)
return db.add(result)
async def create_result(db: Session, client_result: schemas.ClientResult):
result = Result(
submitted_at=datetime.now(),
status=client_result.status,
context=client_result.context,
)
db.add(result)
return result
async def update_from_config(db: Session, config: schemas.Config):
@ -38,7 +38,6 @@ async def update_from_config(db: Session, config: schemas.Config):
url = urljoin(domain, str(p.path))
for check in p.checks:
for check_key, expected in check.items():
# Check the db for already existing tasks.
existing_task = db.query(
exists().where(