From 9feccb5bd0241d9b7fbfd3f9711865edadb8463d Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 24 Sep 2021 14:37:59 +0200 Subject: [PATCH] Bump --- papi/main.py | 46 +++++++++++++++++++++++++++++++++++++++++----- tests/test_papi.py | 8 ++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/papi/main.py b/papi/main.py index d96f7bf..0c2d070 100644 --- a/papi/main.py +++ b/papi/main.py @@ -5,6 +5,8 @@ from fastapi import FastAPI, Request, Body from collections import defaultdict +from . import utils + app = FastAPI() @@ -24,11 +26,6 @@ def read_root(): return {"msg": "Hello World"} -@app.get("/items/{item_id}") -def read_item(item_id: int, q: Optional[str] = None): - return {"item_id": item_id, "q": q} - - @app.post("/sonde/") def post_sonde(sonde: Sonde): print(sonde) @@ -41,3 +38,42 @@ def list_sonde(): return [x for x in sondes.values()] +@app.post("/sonde/{idsonde}/") +def post_sonde_data(idsonde: str, body: dict = Body(...)): + if idsonde in sondes: + mesures[idsonde].append(body) + print(len(mesures[idsonde])) + # print(dict(mesures)) + + +@app.get("/sonde/{idsonde}/rapport") +def get_rapport(idsonde): + try: + last = mesures[idsonde][-1] + except IndexError: + pass + else: + last = utils.prepare(last) + for name, data in sorted(last["channels"].items()): + yield f'{name} {data["status"]}' + + +@app.get("/sonde/{idsonde}/historique") +def historique(idsonde): + print(len(mesures[idsonde])) + + for previous, present in zip(mesures[idsonde], mesures[idsonde][1:]): + date = present["date"] + previous = utils.prepare(previous) + present = utils.prepare(present) + all_channels = sorted(set((*previous["channels"], *present["channels"]))) + + diff = utils.compare(all_channels, previous, present) + + if diff: + for d in diff["changements"]: + line = date, *d + print(line) + yield " ".join(line) + else: + print(f"no change for {date}") diff --git a/tests/test_papi.py b/tests/test_papi.py index 5288b36..d1dd02d 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -32,3 +32,11 @@ def test_lister_sondes(): response = client.get("/sonde/") assert response.status_code == 200 assert sonde1 in response.json() + + +def test_post_data(): + id_ = "masonde_001" + data = {"example": "truc"} + + response = client.post(f"/sonde/{id_}") + assert response.ok