Created
September 8, 2015 22:44
-
-
Save veesa/24d29b99563d42254604 to your computer and use it in GitHub Desktop.
A small Flask REST API app written to a spec.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify, request | |
import uuid | |
import sqlite3 | |
from flask import g | |
DATABASE = '/tmp/database.db' | |
app = Flask(__name__) | |
def get_db(): | |
db = getattr(g, '_database', None) | |
if db is None: | |
db = g._database = sqlite3.connect(DATABASE) | |
return db | |
@app.teardown_appcontext | |
def close_connection(exception): | |
db = getattr(g, '_database', None) | |
if db is not None: | |
db.close() | |
def query_db(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 init_db(): | |
with app.app_context(): | |
db = get_db() | |
with app.open_resource('schema.sql', mode='r') as f: | |
db.cursor().executescript(f.read()) | |
db.commit() | |
@app.route('/test/api/register', methods=['GET']) | |
def create_app_id(): | |
a = uuid.uuid4() | |
reg_token = a.hex | |
new_id = { | |
'app_id': reg_token | |
} | |
return jsonify(app_id=reg_token) | |
@app.route('/test/api/store/<reg_token>', methods=['POST']) | |
def store_data(reg_token): | |
db = get_db() | |
app_id = query_db('select id from application where app_id = ?', [reg_token], one=True) | |
if app_id: | |
print app_id[0] | |
else: | |
print "Nothing found" | |
''' If the app_id is not in the database, save it and generate an id for the pairs table''' | |
if not app_id: | |
print reg_token | |
db.execute('INSERT INTO application (app_id) VALUES (?)', [reg_token]) | |
print "success" | |
db.commit() | |
app_id = query_db('select id from application where app_id = ?', [reg_token], one=True) | |
''' Then save the key/pair data''' | |
data_record = [(request.json['key_name']), | |
(request.json['val']), | |
app_id[0], | |
] | |
print data_record | |
db.execute('INSERT INTO pairs(key_name, val, app) VALUES (?,?,?)', data_record) | |
db.commit() | |
return "Inserted new application id and key/pair data into database" | |
else: | |
'''If the app_id is in the database, save the key/pair value against it's id''' | |
data_record = [(request.json['key_name']), | |
(request.json['val']), | |
app_id[0], | |
] | |
print data_record | |
db.execute('INSERT INTO pairs(key_name, val, app) VALUES (?,?,?)', data_record) | |
db.commit() | |
return "Inserted key/pair data for existing app_id in database" | |
@app.route('/test/api/retrieve/<reg_token>', methods=['GET']) | |
def get_data_values(reg_token): | |
key = request.args.get('key_name') | |
print key | |
app_id = reg_token | |
print app_id | |
db = get_db() | |
identifier = query_db('select id from application where app_id=?', [app_id], one=True) | |
''' If the key_name is found ''' | |
key_exists = query_db('select key_name from pairs where key_name=?', [key], one=True) | |
if key_exists: | |
print "Key exists" | |
print identifier[0] | |
if not identifier: | |
return "Application id not found in database" | |
''' Return value for key specified ''' | |
print key | |
data_record=[ | |
key, | |
identifier[0], | |
] | |
val = query_db('SELECT val FROM pairs WHERE key_name=(?) AND app=(?)', data_record, one=True) | |
return jsonify(key_value=val) | |
else: | |
print "Key was not found" | |
''' Return all key/value pairs for identified application ''' | |
for row in query_db('SELECT key_name, val FROM pairs WHERE app=?', [identifier[0]]): | |
return jsonify(row=row) | |
return "Successfully finished" | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment