import sqlite3
import json
import os
import yaml
import sqlite3


from flask import Flask, g, request
from flask_restful import reqparse, abort, Api, Resource

app = Flask(__name__)
app.secret_key = os.urandom(24)


def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect('data/app.db')
        db.row_factory = sqlite3.Row
    return db


def db_query(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv


def db_execute(query: object, args: object = ()) -> object:
    cur = get_db().execute(query, args)
    get_db().commit()
    lastid = cur.lastrowid
    cur.close()
    return lastid


@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

# Setup Restful API Support
api = Api(app)

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PATCH,PUT,POST,DELETE')
    return response

parser = reqparse.RequestParser()
parser.add_argument('task')
parser.add_argument('id')
parser.add_argument('method')

# Todo
# shows a single todo item and lets you delete a todo item
class Task(Resource):
    def get(self, id):
        args = parser.parse_args()
        message = "record returned successfully"
        status = 200
        success = True
        row = db_query('SELECT * FROM todo WHERE id = ?', (id), one=True)
        if row is None:
            payload = []
            message = 'no matching record found'
            status = 404
            success = False
        else:
            payload = [dict(row)]
        response = {
            'success': success, 
            'status': status,
            'message': message,
            'payload': payload
        }
        return response

    def delete(self, id):
        query = 'DELETE FROM todo WHERE id = ?'
        id = db_execute(query, (id,))
        message = 'task removed successfully'
        response = {
            'success': True, 
            'status': 200,
            'message': message,
            'payload': []
        }
        return response, 201

    def patch(self, id):
        body = request.get_json()
        title = body['title']
        description = body['description']
        active = body['active']
        complete = body['complete']
        valuestrings = []
        for key, value in body.items():
            if key != 'id':
                valuestrings.append('{0} = ?'.format(key))

        query = 'UPDATE todo SET {0} WHERE id = ?'.format(", ".join(valuestrings))
        rowid = db_execute(query, (title, description, active, complete, id))

        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task updated successfully'
        response = {
            'success': True, 
            'status': 200,
            'message': ", ".join(valuestrings),
            'payload': [dict(row)]
        }
        return response, 201


# TodoList
# shows a list of all todos, and lets you POST to add new tasks
class TaskList(Resource):
    def get(self):
        todos = []
        rows = db_query('SELECT * FROM todo')
        for row in rows:
            todos.append(dict(row))
        message = "{} records returned successfully".format(len(todos))
        response = {
            'success': True, 
            'status': 200,
            'message': message,
            'payload': {'tasks': todos}
        }
        return response
        
    def post(self):
        body = request.get_json()
        task = body['task']
        query = 'INSERT INTO todo (title, description, complete) VALUES (?, ?, ?)'
        id = db_execute(query, (task['title'], task['description'], task['complete']))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task created successfully'
        response = {
            'success': True,
            'status': 200,
            'message': message,
            'payload': [dict(row)]
        }
        return response, 201


##
## Actually setup the Api resource routing here
##
api.add_resource(TaskList, '/tasks')
api.add_resource(Task, '/tasks/<id>')

@app.route('/')
def home():
    return "Please use a valid route", 404


if __name__ == '__main__':
    app.run(
        debug=True,
        host='0.0.0.0'
    )
f9f9ea100e56b446f9defdc6cd504b7ee33e108b
<mask token>


@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()


<mask token>


class Task(Resource):

    def get(self, id):
        args = parser.parse_args()
        message = 'record returned successfully'
        status = 200
        success = True
        row = db_query('SELECT * FROM todo WHERE id = ?', id, one=True)
        if row is None:
            payload = []
            message = 'no matching record found'
            status = 404
            success = False
        else:
            payload = [dict(row)]
        response = {'success': success, 'status': status, 'message':
            message, 'payload': payload}
        return response

    def delete(self, id):
        query = 'DELETE FROM todo WHERE id = ?'
        id = db_execute(query, (id,))
        message = 'task removed successfully'
        response = {'success': True, 'status': 200, 'message': message,
            'payload': []}
        return response, 201

    def patch(self, id):
        body = request.get_json()
        title = body['title']
        description = body['description']
        active = body['active']
        complete = body['complete']
        valuestrings = []
        for key, value in body.items():
            if key != 'id':
                valuestrings.append('{0} = ?'.format(key))
        query = 'UPDATE todo SET {0} WHERE id = ?'.format(', '.join(
            valuestrings))
        rowid = db_execute(query, (title, description, active, complete, id))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task updated successfully'
        response = {'success': True, 'status': 200, 'message': ', '.join(
            valuestrings), 'payload': [dict(row)]}
        return response, 201


class TaskList(Resource):

    def get(self):
        todos = []
        rows = db_query('SELECT * FROM todo')
        for row in rows:
            todos.append(dict(row))
        message = '{} records returned successfully'.format(len(todos))
        response = {'success': True, 'status': 200, 'message': message,
            'payload': {'tasks': todos}}
        return response

    def post(self):
        body = request.get_json()
        task = body['task']
        query = (
            'INSERT INTO todo (title, description, complete) VALUES (?, ?, ?)')
        id = db_execute(query, (task['title'], task['description'], task[
            'complete']))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task created successfully'
        response = {'success': True, 'status': 200, 'message': message,
            'payload': [dict(row)]}
        return response, 201


<mask token>


@app.route('/')
def home():
    return 'Please use a valid route', 404


<mask token>

<mask token>


def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect('data/app.db')
        db.row_factory = sqlite3.Row
    return db


def db_query(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv


def db_execute(query: object, args: object=()) ->object:
    cur = get_db().execute(query, args)
    get_db().commit()
    lastid = cur.lastrowid
    cur.close()
    return lastid


@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()


<mask token>


@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers',
        'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods',
        'GET,PATCH,PUT,POST,DELETE')
    return response


<mask token>
parser.add_argument('task')
parser.add_argument('id')
parser.add_argument('method')


class Task(Resource):

    def get(self, id):
        args = parser.parse_args()
        message = 'record returned successfully'
        status = 200
        success = True
        row = db_query('SELECT * FROM todo WHERE id = ?', id, one=True)
        if row is None:
            payload = []
            message = 'no matching record found'
            status = 404
            success = False
        else:
            payload = [dict(row)]
        response = {'success': success, 'status': status, 'message':
            message, 'payload': payload}
        return response

    def delete(self, id):
        query = 'DELETE FROM todo WHERE id = ?'
        id = db_execute(query, (id,))
        message = 'task removed successfully'
        response = {'success': True, 'status': 200, 'message': message,
            'payload': []}
        return response, 201

    def patch(self, id):
        body = request.get_json()
        title = body['title']
        description = body['description']
        active = body['active']
        complete = body['complete']
        valuestrings = []
        for key, value in body.items():
            if key != 'id':
                valuestrings.append('{0} = ?'.format(key))
        query = 'UPDATE todo SET {0} WHERE id = ?'.format(', '.join(
            valuestrings))
        rowid = db_execute(query, (title, description, active, complete, id))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task updated successfully'
        response = {'success': True, 'status': 200, 'message': ', '.join(
            valuestrings), 'payload': [dict(row)]}
        return response, 201


class TaskList(Resource):

    def get(self):
        todos = []
        rows = db_query('SELECT * FROM todo')
        for row in rows:
            todos.append(dict(row))
        message = '{} records returned successfully'.format(len(todos))
        response = {'success': True, 'status': 200, 'message': message,
            'payload': {'tasks': todos}}
        return response

    def post(self):
        body = request.get_json()
        task = body['task']
        query = (
            'INSERT INTO todo (title, description, complete) VALUES (?, ?, ?)')
        id = db_execute(query, (task['title'], task['description'], task[
            'complete']))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task created successfully'
        response = {'success': True, 'status': 200, 'message': message,
            'payload': [dict(row)]}
        return response, 201


api.add_resource(TaskList, '/tasks')
api.add_resource(Task, '/tasks/<id>')


@app.route('/')
def home():
    return 'Please use a valid route', 404


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

<mask token>
app = Flask(__name__)
app.secret_key = os.urandom(24)


def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect('data/app.db')
        db.row_factory = sqlite3.Row
    return db


def db_query(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv


def db_execute(query: object, args: object=()) ->object:
    cur = get_db().execute(query, args)
    get_db().commit()
    lastid = cur.lastrowid
    cur.close()
    return lastid


@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()


api = Api(app)


@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers',
        'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods',
        'GET,PATCH,PUT,POST,DELETE')
    return response


parser = reqparse.RequestParser()
parser.add_argument('task')
parser.add_argument('id')
parser.add_argument('method')


class Task(Resource):

    def get(self, id):
        args = parser.parse_args()
        message = 'record returned successfully'
        status = 200
        success = True
        row = db_query('SELECT * FROM todo WHERE id = ?', id, one=True)
        if row is None:
            payload = []
            message = 'no matching record found'
            status = 404
            success = False
        else:
            payload = [dict(row)]
        response = {'success': success, 'status': status, 'message':
            message, 'payload': payload}
        return response

    def delete(self, id):
        query = 'DELETE FROM todo WHERE id = ?'
        id = db_execute(query, (id,))
        message = 'task removed successfully'
        response = {'success': True, 'status': 200, 'message': message,
            'payload': []}
        return response, 201

    def patch(self, id):
        body = request.get_json()
        title = body['title']
        description = body['description']
        active = body['active']
        complete = body['complete']
        valuestrings = []
        for key, value in body.items():
            if key != 'id':
                valuestrings.append('{0} = ?'.format(key))
        query = 'UPDATE todo SET {0} WHERE id = ?'.format(', '.join(
            valuestrings))
        rowid = db_execute(query, (title, description, active, complete, id))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task updated successfully'
        response = {'success': True, 'status': 200, 'message': ', '.join(
            valuestrings), 'payload': [dict(row)]}
        return response, 201


class TaskList(Resource):

    def get(self):
        todos = []
        rows = db_query('SELECT * FROM todo')
        for row in rows:
            todos.append(dict(row))
        message = '{} records returned successfully'.format(len(todos))
        response = {'success': True, 'status': 200, 'message': message,
            'payload': {'tasks': todos}}
        return response

    def post(self):
        body = request.get_json()
        task = body['task']
        query = (
            'INSERT INTO todo (title, description, complete) VALUES (?, ?, ?)')
        id = db_execute(query, (task['title'], task['description'], task[
            'complete']))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task created successfully'
        response = {'success': True, 'status': 200, 'message': message,
            'payload': [dict(row)]}
        return response, 201


api.add_resource(TaskList, '/tasks')
api.add_resource(Task, '/tasks/<id>')


@app.route('/')
def home():
    return 'Please use a valid route', 404


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

import sqlite3
import json
import os
import yaml
import sqlite3
from flask import Flask, g, request
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
app.secret_key = os.urandom(24)


def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect('data/app.db')
        db.row_factory = sqlite3.Row
    return db


def db_query(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv


def db_execute(query: object, args: object=()) ->object:
    cur = get_db().execute(query, args)
    get_db().commit()
    lastid = cur.lastrowid
    cur.close()
    return lastid


@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()


api = Api(app)


@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers',
        'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods',
        'GET,PATCH,PUT,POST,DELETE')
    return response


parser = reqparse.RequestParser()
parser.add_argument('task')
parser.add_argument('id')
parser.add_argument('method')


class Task(Resource):

    def get(self, id):
        args = parser.parse_args()
        message = 'record returned successfully'
        status = 200
        success = True
        row = db_query('SELECT * FROM todo WHERE id = ?', id, one=True)
        if row is None:
            payload = []
            message = 'no matching record found'
            status = 404
            success = False
        else:
            payload = [dict(row)]
        response = {'success': success, 'status': status, 'message':
            message, 'payload': payload}
        return response

    def delete(self, id):
        query = 'DELETE FROM todo WHERE id = ?'
        id = db_execute(query, (id,))
        message = 'task removed successfully'
        response = {'success': True, 'status': 200, 'message': message,
            'payload': []}
        return response, 201

    def patch(self, id):
        body = request.get_json()
        title = body['title']
        description = body['description']
        active = body['active']
        complete = body['complete']
        valuestrings = []
        for key, value in body.items():
            if key != 'id':
                valuestrings.append('{0} = ?'.format(key))
        query = 'UPDATE todo SET {0} WHERE id = ?'.format(', '.join(
            valuestrings))
        rowid = db_execute(query, (title, description, active, complete, id))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task updated successfully'
        response = {'success': True, 'status': 200, 'message': ', '.join(
            valuestrings), 'payload': [dict(row)]}
        return response, 201


class TaskList(Resource):

    def get(self):
        todos = []
        rows = db_query('SELECT * FROM todo')
        for row in rows:
            todos.append(dict(row))
        message = '{} records returned successfully'.format(len(todos))
        response = {'success': True, 'status': 200, 'message': message,
            'payload': {'tasks': todos}}
        return response

    def post(self):
        body = request.get_json()
        task = body['task']
        query = (
            'INSERT INTO todo (title, description, complete) VALUES (?, ?, ?)')
        id = db_execute(query, (task['title'], task['description'], task[
            'complete']))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task created successfully'
        response = {'success': True, 'status': 200, 'message': message,
            'payload': [dict(row)]}
        return response, 201


api.add_resource(TaskList, '/tasks')
api.add_resource(Task, '/tasks/<id>')


@app.route('/')
def home():
    return 'Please use a valid route', 404


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

import sqlite3
import json
import os
import yaml
import sqlite3


from flask import Flask, g, request
from flask_restful import reqparse, abort, Api, Resource

app = Flask(__name__)
app.secret_key = os.urandom(24)


def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect('data/app.db')
        db.row_factory = sqlite3.Row
    return db


def db_query(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv


def db_execute(query: object, args: object = ()) -> object:
    cur = get_db().execute(query, args)
    get_db().commit()
    lastid = cur.lastrowid
    cur.close()
    return lastid


@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

# Setup Restful API Support
api = Api(app)

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PATCH,PUT,POST,DELETE')
    return response

parser = reqparse.RequestParser()
parser.add_argument('task')
parser.add_argument('id')
parser.add_argument('method')

# Todo
# shows a single todo item and lets you delete a todo item
class Task(Resource):
    def get(self, id):
        args = parser.parse_args()
        message = "record returned successfully"
        status = 200
        success = True
        row = db_query('SELECT * FROM todo WHERE id = ?', (id), one=True)
        if row is None:
            payload = []
            message = 'no matching record found'
            status = 404
            success = False
        else:
            payload = [dict(row)]
        response = {
            'success': success, 
            'status': status,
            'message': message,
            'payload': payload
        }
        return response

    def delete(self, id):
        query = 'DELETE FROM todo WHERE id = ?'
        id = db_execute(query, (id,))
        message = 'task removed successfully'
        response = {
            'success': True, 
            'status': 200,
            'message': message,
            'payload': []
        }
        return response, 201

    def patch(self, id):
        body = request.get_json()
        title = body['title']
        description = body['description']
        active = body['active']
        complete = body['complete']
        valuestrings = []
        for key, value in body.items():
            if key != 'id':
                valuestrings.append('{0} = ?'.format(key))

        query = 'UPDATE todo SET {0} WHERE id = ?'.format(", ".join(valuestrings))
        rowid = db_execute(query, (title, description, active, complete, id))

        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task updated successfully'
        response = {
            'success': True, 
            'status': 200,
            'message': ", ".join(valuestrings),
            'payload': [dict(row)]
        }
        return response, 201


# TodoList
# shows a list of all todos, and lets you POST to add new tasks
class TaskList(Resource):
    def get(self):
        todos = []
        rows = db_query('SELECT * FROM todo')
        for row in rows:
            todos.append(dict(row))
        message = "{} records returned successfully".format(len(todos))
        response = {
            'success': True, 
            'status': 200,
            'message': message,
            'payload': {'tasks': todos}
        }
        return response
        
    def post(self):
        body = request.get_json()
        task = body['task']
        query = 'INSERT INTO todo (title, description, complete) VALUES (?, ?, ?)'
        id = db_execute(query, (task['title'], task['description'], task['complete']))
        row = db_query('SELECT * FROM todo WHERE id = ?', [id], one=True)
        message = 'task created successfully'
        response = {
            'success': True,
            'status': 200,
            'message': message,
            'payload': [dict(row)]
        }
        return response, 201


##
## Actually setup the Api resource routing here
##
api.add_resource(TaskList, '/tasks')
api.add_resource(Task, '/tasks/<id>')

@app.route('/')
def home():
    return "Please use a valid route", 404


if __name__ == '__main__':
    app.run(
        debug=True,
        host='0.0.0.0'
    )