I am trying to familiarize myself with Python, coming from a heavy PHP background. I want to create a JSON object that returns the following:
Endpoint: /dashboard
Request params:{
method:”get”,
headers:headers,
url:string,
data:{
userId: string,
date: int
}
}
Response: (JSON Object)
{
code: 200/400,
message: String,
data: {
totalCal:
remainingCal:
}
}
Essentially connecting to the database and returning a 200 if the data matches, a 400 if i hit any errors. I came up with this so far, but I'd appreciate any input on how I could perform this better!
@app.route('/dashboard', methods=['GET'])
def availCalories():
cur = conn.cursor()
cur.execute('''SELECT remaining_cal FROM daily_cal WHERE user_id = 1 AND day= curdate();''')# WHERE userID=userID in session???
row_headers=[x[0] for x in cur.description] #EXTRACT ROW HEADERS
rv = cur.fetchall()
json_data=[]
for result in rv:
json_data.append(dict(zip(row_headers,result)))
return json.dumps(json_data)
I've been teaching myself Python for only 3 months now, so a lot of this is trial and error, but I find myself hitting a wall with this!
Thanks for your time in advance!