From 82da9eae1329bea2534ce8e86fbb7acd6684a420 Mon Sep 17 00:00:00 2001 From: Kishore Narendran Date: Tue, 28 Feb 2017 05:50:16 -0800 Subject: [PATCH] Buku API - Adds preliminary endpoints for an API for Buku [WIP] (#124) * Adds requirements for the Buku API server * Adds all the necessary requirements for BukuDB * Adds preliminary API endpoints --- api/requirements.txt | 19 +++++++++++ api/server.py | 78 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 api/requirements.txt create mode 100644 api/server.py diff --git a/api/requirements.txt b/api/requirements.txt new file mode 100644 index 0000000..52489b2 --- /dev/null +++ b/api/requirements.txt @@ -0,0 +1,19 @@ +appdirs==1.4.1 +beautifulsoup4==4.5.3 +buku==2.9 +cffi==1.9.1 +click==6.7 +cryptography==1.7.2 +Flask==0.12 +idna==2.2 +itsdangerous==0.24 +Jinja2==2.9.5 +MarkupSafe==0.23 +packaging==16.8 +pyasn1==0.2.2 +pycparser==2.17 +pyparsing==2.1.10 +requests==2.13.0 +six==1.10.0 +urllib3==1.20 +Werkzeug==0.11.15 diff --git a/api/server.py b/api/server.py new file mode 100644 index 0000000..db4d772 --- /dev/null +++ b/api/server.py @@ -0,0 +1,78 @@ +from buku import BukuDb +from flask import Flask, jsonify, request + + +bukudb = BukuDb() + +app = Flask(__name__) + + +@app.route('/api/tags', methods=['GET']) +def get_tags(): + tags = bukudb.get_tag_all() + result = { + 'tags': tags[0] + } + return jsonify(result) + + +@app.route('/api/bookmarks', methods=['GET', 'POST']) +def bookmarks(): + if request.method == 'GET': + bookmarks = bukudb.get_rec_all() + result = { + 'bookmarks': [] + } + for bookmark in bookmarks: + result_bookmark = { + 'url': bookmark[1], + 'title': bookmark[2], + 'tags': list(filter(None, bookmark[3].split(','))), + 'description': bookmark[4] + } + result['bookmarks'].append(result_bookmark) + return jsonify(result) + elif request.method == 'POST': + result_flag = bukudb.add_rec(request.form['url'], request.form['title'], + request.form['tags'], request.form['description']) + if result_flag: + return jsonify({'status': 0, 'message': 'success'}), 200, {'ContentType': 'application/json'} + else: + return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'} + +@app.route('/api/bookmarks/', methods=['GET', 'PUT', 'DELETE']) +def bookmark_api(id): + try: + id = int(id) + except ValueError: + return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'} + if request.method == 'GET': + bookmark = bukudb.get_rec_by_id(id) + if bookmark is not None: + result = { + 'url': bookmark[1], + 'title': bookmark[2], + 'tags': list(filter(None, bookmark[3].split(','))), + 'description': bookmark[4] + } + return jsonify(result) + else: + return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'} + elif request.method == 'PUT': + result_flag = bukudb.update_rec(id, request.form['url'], request.form.get('title'), + request.form['tags'], request.form['description']) + if result_flag: + return jsonify({'status': 0, 'message': 'success'}), 200, {'ContentType': 'application/json'} + else: + return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'} + else: + result_flag = bukudb.delete_rec(id) + if result_flag: + return jsonify({'status': 0, 'message': 'success'}), 200, {'ContentType': 'application/json'} + else: + return jsonify({'status': 1, 'message': 'failure'}), 400, {'ContentType': 'application/json'} + +def run(): + app.run(debug=True) + +run()