import json from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("https://pythonbasics.org/", methods=['GET']) defquery_records(): name = request.args.get('name') print name with open('/tmp/data.txt', 'r') as f: data = f.read() records = json.loads(data) for record in records: if record['name'] == name: return jsonify(record) return jsonify({'error': 'data not found'})
@app.route("https://pythonbasics.org/", methods=['PUT']) defcreate_record(): record = json.loads(request.data) with open('/tmp/data.txt', 'r') as f: data = f.read() ifnot data: records = [record] else: records = json.loads(data) records.append(record) with open('/tmp/data.txt', 'w') as f: f.write(json.dumps(records, indent=2)) return jsonify(record)
@app.route("https://pythonbasics.org/", methods=['POST']) defupdate_record(): record = json.loads(request.data) new_records = [] with open('/tmp/data.txt', 'r') as f: data = f.read() records = json.loads(data) for r in records: if r['name'] == record['name']: r['email'] = record['email'] new_records.append(r) with open('/tmp/data.txt', 'w') as f: f.write(json.dumps(new_records, indent=2)) return jsonify(record) @app.route("https://pythonbasics.org/", methods=['DELETE']) defdelte_record(): record = json.loads(request.data) new_records = [] with open('/tmp/data.txt', 'r') as f: data = f.read() records = json.loads(data) for r in records: if r['name'] == record['name']: continue new_records.append(r) with open('/tmp/data.txt', 'w') as f: f.write(json.dumps(new_records, indent=2)) return jsonify(record)