From 5f39075be454fe3bdec12e6d2f8313eedbb9f2ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bonhomme?= Date: Tue, 19 Dec 2023 10:05:42 +0100 Subject: [PATCH] fix: [API] Fixed an issue when storing the UUID of a scan. --- api/crud.py | 4 ++-- api/main.py | 25 +++++++++++++++---------- api/models.py | 1 + api/schemas.py | 12 ++++++++++++ scandale/probe.py | 4 +++- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/api/crud.py b/api/crud.py index 0c39438..dfbac3f 100644 --- a/api/crud.py +++ b/api/crud.py @@ -31,9 +31,9 @@ def create_item(db: Session, item: schemas.ItemCreate): return db_item -def create_tst(db: Session, data: schemas.ItemCreate): +def create_tst(db: Session, data: schemas.TimeStampTokenCreate): """Create a TimeStampToken.""" - db_tst = models.TimeStampToken(tst=data) + db_tst = models.TimeStampToken(**data) db.add(db_tst) db.commit() db.refresh(db_tst) diff --git a/api/main.py b/api/main.py index a55fdba..0368776 100644 --- a/api/main.py +++ b/api/main.py @@ -6,7 +6,6 @@ from fastapi import Depends from fastapi import FastAPI from fastapi import HTTPException -from fastapi import Request from fastapi.openapi.utils import get_openapi from sqlalchemy.orm import Session @@ -96,19 +95,25 @@ async def create_item(item: schemas.ScanDataCreate, db: Session = db_session): # -@app.post("/tst/") -async def create_tst(request: Request, db: Session = db_session): +@app.post("/tst/", response_model=schemas.TimeStampTokenCreate) +async def create_tst( + data: schemas.TimeStampTokenCreate, db: Session = db_session +) -> schemas.TimeStampToken: """Insert a TimeStampToken.""" - data: bytes = await request.body() - new_tst = crud.create_tst(db=db, data=data) - return {"message": new_tst.id} + dict_data = { + "scan_uuid": data.scan_uuid, + "tst": data.tst + } + new_tst = crud.create_tst(db=db, data=dict_data) + return new_tst -@app.get("/tsts/", response_model=str) -async def read_tst(skip: int = 0, limit: int = 100, db: Session = db_session) -> str: +@app.get("/tsts/", response_model=list[schemas.TimeStampToken]) +async def read_tst( + skip: int = 0, limit: int = 100, db: Session = db_session +) -> list[schemas.TimeStampToken]: tsts = crud.get_tst(db, skip=skip, limit=limit) - tst_list = [elem.tst for elem in tsts] - return str(tst_list) + return tsts # diff --git a/api/models.py b/api/models.py index 078922c..69f8b34 100644 --- a/api/models.py +++ b/api/models.py @@ -1,4 +1,5 @@ import uuid + from sqlalchemy import Column from sqlalchemy import Integer from sqlalchemy import LargeBinary diff --git a/api/schemas.py b/api/schemas.py index 37f67c4..b46949e 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -1,3 +1,5 @@ +import uuid + from pydantic import BaseModel @@ -36,5 +38,15 @@ class ScanDataCreate(ScanData): pass +# +# Timestamp +# + + class TimeStampToken(BaseModel): + scan_uuid: uuid.UUID tst: bytes + + +class TimeStampTokenCreate(TimeStampToken): + pass diff --git a/scandale/probe.py b/scandale/probe.py index 8de1377..73d8a99 100644 --- a/scandale/probe.py +++ b/scandale/probe.py @@ -4,6 +4,7 @@ import json import subprocess import time +import uuid import spade from spade.agent import Agent @@ -51,12 +52,13 @@ async def run(self): ) # Set the language of the message content # Set the message content + scan_uuid = str(uuid.uuid4()) msg.body = json.dumps( { "version": self.config["version"], "format": self.config["format"], "meta": { - "uuid": self.config["uuid"], + "uuid": scan_uuid, "ts": int(time.time()), "type": self.config["type"], },