July 2017
Intermediate to advanced
374 pages
8h
English
The GET method lists the tweets made by the specified ID.
Add the following code to app.py to add a route for the GET method with a specified ID:
@app.route('/api/v2/tweets/<int:id>', methods=['GET'])
def get_tweet(id):
return list_tweet(id)
Let's define the list_tweet() function, which connects to the database, gets us the tweets with the specified ID, and responds with the JSON data. This is done as follows:
def list_tweet(user_id): print (user_id) conn = sqlite3.connect('mydb.db') print ("Opened database successfully"); api_list=[] cursor=conn.cursor() cursor.execute("SELECT * from tweets where id=?",(user_id,)) data = cursor.fetchall() print (data) if len(data) == 0: abort(404) else: user = {} user['id'] = data[0][0] ...Read now
Unlock full access