書名: Cloud Native Python作者名: Manish Sethi本章字數: 131字更新時間: 2021-07-02 19:11:58
GET /api/v1/users
The GET/api/v1/users method shows the list of all users.
Let's create an /api/v1/users route by adding the following code snippet to app.py:
@app.route('/api/v1/users', methods=['GET']) def get_users(): return list_users()
Now that we have added the route, we need to define the list_users() function, which will connect with the database to get you the complete list of users. Add the following code to app.py:
def list_users():
conn = sqlite3.connect('mydb.db')
print ("Opened database successfully");
api_list=[]
cursor = conn.execute("SELECT username, full_name,
email, password, id from users")
for row in cursor:
a_dict = {}
a_dict['username'] = row[0]
a_dict['name'] = row[1]
a_dict['email'] = row[2]
a_dict['password'] = row[3]
a_dict['id'] = row[4]
api_list.append(a_dict)
conn.close()
return jsonify({'user_list': api_list})
Now that we have added the route and the handle for it, let's test check the http://localhost:5000/api/v1/users URL as follows:

推薦閱讀
- Visual C++實例精通
- Python從入門到精通(精粹版)
- 跟老齊學Python:輕松入門
- aelf區塊鏈應用架構指南
- 人臉識別原理及算法:動態人臉識別系統研究
- PostgreSQL 11從入門到精通(視頻教學版)
- Elasticsearch for Hadoop
- C陷阱與缺陷
- IPython Interactive Computing and Visualization Cookbook
- C Primer Plus(第6版)中文版【最新修訂版】
- VMware vSphere 5.5 Cookbook
- Getting Started with RethinkDB
- LabVIEW數據采集(第2版)
- SAP HANA Cookbook
- 大學計算機基礎