I have a weather station that sends json data and want to make a fastAPI server to receive it and save it to disk. Currently I have
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class WeatherItem(BaseModel):
wind_direction_raw: int
rain_amount_raw: int
timestamp: list = []
elapsed_time: int
wind_speed_raw: int
message_id: int
@app.post("/station")
async def create_item(item: WeatherItem):
return item
Which works well when I start it up with uvicorn main:app --host 192.168.1.151 --port 9494 and send test data from curl with
curl -X POST -H "Content-Type: application/json" --data '{"elapsed_time": 6245, "timestamp": [2020, 7, 26, 12, 2, 21, 6, 208], "wind_direction_raw": 108, "wind_speed_raw": 5, "message_id": 666, "rain_amount_raw": "0"}' http://192.168.1.151:9494/station
Now I need to save this data to disk. I think the simplest would be appending it to a csv file. But I can't figure out how to export a pydantic model to csv. Is there a straightforward way to do this or are other serialization methods preferred? I would like to analyze this data in R so I need it on disk in a fairly interchangeable format.