Basically I'm working on an API that receives a POST method and then uses some data to create a json and send it.
There are some constraints, for example the names that I have in my database are different of the names that need to be in the json, but the values need to be the same. So what I did was create a dictionary and associate the names in the database with the names that will be in the json.
Code Explanation:
I receive a post method with a body message that will be useful to verify if that value exists in my database. If exists I will do another query that will retrieve all the devices that belong to the value that I received in the body. And then I do another query using influxDB to get all the values in the db.
In the first part what I do is for each device that I have I check if the ID exists in the influxDB and if it does I store the value.
With that value stored I check if my device name have a correspondent (a name that I don't have in the database in the) and I round the value received before and I append to a response.
In the second part I just check what names I have in my response and then put the values that are in the dictionary but not in the response there with a value of 0 (because that are names/values that I don't have in the database but are needed in the json response).
Code:
json_data = {
"wid":None,
"time_loc":None,
"date_loc":None,
"time_utc":None,
"temp_out": "outTemp",
"temp_in": "inTemp",
"hum_out": "outHumidity",
"hum_in": "inHumidity",
"barometer": "barometer",
"altimeter": "altimeter",
"raw_bar":None,
"bar_trend":None,
"wind_spd": "windSpeed",
"wind_dir": "windDir",
"wind_avg_2min":None,
"wind_avg_10min":None,
"wind_gust_10min":None,
"wind_gust_dir": "windGustDir",
"dew_point": "dewpoint",
"dew_point_cal":None,
"wind_chill": "windchill",
"heat_index": "heatindex",
"thsw_index":None,
"uv": "UV",
"solar_radiation":None,
"rain_rate": "rainRate",
"storm_rain":None,
"rain_15min":None,
"rain_1h":None,
"rain_day":None,
"rain_daily":None,
"rain_24":None,
"rain_month":None,
"rain_year":None,
"et_day": "ET" ,
"et_month":None,
"et_year":None,
"temp_extra_0": "extraTemp1",
"temp_extra_1": "extraTemp2",
"temp_extra_2": "extraTemp3",
"temp_extra_3": "extraTemp4",
"temp_extra_4": "extraTemp5",
"temp_extra_5": "extraTemp6",
"temp_extra_6": None,
"temp_leaf_0":"leafTemp1",
"temp_leaf_1": "leafTemp2",
"temp_leaf_2": None,
"temp_leaf_3":None,
"temp_soil_0": "soilTemp1",
"temp_soil_1": "soilTemp2",
"temp_soil_2": "soilTemp3",
"temp_soil_3": "soilTemp4",
"hum_extra_0": "extraHumid1",
"hum_extra_1": "extraHumid2",
"hum_extra_2": "extraHumid3",
"hum_extra_3": "extraHumid4",
"hum_extra_4": "extraHumid5",
"hum_extra_5": "extraHumid6",
"hum_extra_6": "extraHumid7",
"soil_moist_0": "soilMoist1",
"soil_moist_1": "soilMoist2",
"soil_moist_2": "soilMoist3",
"soil_moist_3": "soilMoist4",
"wet_leaf_0": "leafWet1",
"wet_leaf_1": "leafWet2",
"wet_leaf_2":None,
"wet_leaf_3":None,
"console_batt":None,
"stations_batt_status":None,
"forecast_icon":None,
"forecast_rule":None,
"sunrise_loc_time":None,
"sunset_loc_time":None,
}
@app.route("/info/", methods=["POST", "GET"])
def get_current_data_devices():
isAuth = False
auth = request.authorization
if auth and auth.username == ApiFlask.post_auth["username"] and auth.password == ApiFlask.post_auth["password"]:
isAuth = True
else:
isAuth = False
return "Unauthorized", 401
if isAuth:
data = request.get_data(as_text=True)
data = json.loads(data)
g_id = data["gateway_id"]
print(f"gateway_id: {g_id}")
valid_gateway = ApiFlask.getValidGateway(g_id)
if(valid_gateway):
devices = ApiFlask.getDataFromDevices(g_id)
influx_results = influxDB.get_all_devices()
response = []
# FIRST PART
for device in devices:
influx_value = 0
for key in influx_results:
#print(key)
if str(device["d_id"]) == str(key):
influx_value = influx_results[key]
print(influx_results[key])
break
for key, val in ApiFlask.json_data.items():
if str(device["d_piv_slug"]) == str(val):
try:
aux_value = round(influx_value, 2)
#print(influx_value)
except:
aux_value = 0
if str(device["d_piv_slug"]) == None:
break
response.append({key: aux_value})
break
# SECOND PART
not_in_database = []
for key in ApiFlask.json_data:
if key not in [list(item.keys())[0] for item in response]:
not_in_database.append({key:0})
for item in not_in_database:
for key in item:
if response: # se a lista tiver conteudo, tem keys
if key not in response[0].keys():
response.append({key:item[key]})
else:
response.append({key:item[key]})
response.sort(key=lambda x: list(ApiFlask.json_data).index(list(x.keys())[0]))
return json.dumps(response, indent=4), 200
else:
return 'Bad Request' , 400
I'm wondering if this code is good, if is not very pratical... I would like to hear your opinions.