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
This commit is contained in:
parent
013849d232
commit
82da9eae13
19
api/requirements.txt
Normal file
19
api/requirements.txt
Normal file
@ -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
|
78
api/server.py
Normal file
78
api/server.py
Normal file
@ -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/<id>', 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()
|
Loading…
Reference in New Issue
Block a user