diff --git a/gru/config.py b/gru/config.py index 4b5fa5e..6b8e205 100644 --- a/gru/config.py +++ b/gru/config.py @@ -1,5 +1,3 @@ -from typing import Any - from dotenv import load_dotenv from pydantic import BaseSettings diff --git a/gru/main.py b/gru/main.py index eda8ba0..16aa2b9 100644 --- a/gru/main.py +++ b/gru/main.py @@ -1,11 +1,15 @@ -from typing import Annotated import csv -import uvicorn import datetime + +from typing import Annotated + +import uvicorn + from fastapi import FastAPI, Form +from pydantic import BaseModel from .core.config import settings -from pydantic import BaseModel + app = FastAPI() @@ -15,16 +19,22 @@ class Reading(BaseModel): humidity: float age: int + @app.post("/upload", response_model=Reading) -def add_reading(temp: Annotated[float, Form()], humidity: Annotated[float, Form()], age: Annotated[int, Form()], device: Annotated[str, Form()]): +def add_reading( + temp: Annotated[float, Form()], + humidity: Annotated[float, Form()], + age: Annotated[int, Form()], + device: Annotated[str, Form()], +): current_datetime = datetime.datetime.now() deducted_datetime = current_datetime - datetime.timedelta(milliseconds=age) append_to_csv(settings.FILE_PATH, temp, humidity, deducted_datetime) - return Reading(temperature=temp,humidity=humidity,age=age) + return Reading(temperature=temp, humidity=humidity, age=age) def append_to_csv(filename, temperature, humidity, datetime_var): - with open(filename, 'a', newline='') as csvfile: + with open(filename, "a", newline="") as csvfile: writer = csv.writer(csvfile) writer.writerow([temperature, humidity, datetime_var])