To create our simple RESTful API, we will create a single file and use SQLite3 as a database:
- Create a folder named server and create a file named app.py in it:
import sqlite3 as sql
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flask_cors import CORS
app = Flask(__name__)
api = Api(app)
CORS(app)
parser = reqparse.RequestParser()
conn = sql.connect('tasks.db')
conn.execute('CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY
KEY AUTOINCREMENT, task TEXT)')
conn.close()
- Then, we will create our ToDo class, and on the constructor of the class, we will connect to the database and select all tasks:
class ToDo(Resource):
def get(self):
con = sql.connect('tasks.db')
cur = con.cursor()
cur.execute('SELECT * from tasks')
tasks = cur.fetchall()
con.close()
return {
'tasks': tasks
}
- To implement the RESTful POST method, create...