Feature/server (#255)
* chg: dev: update api - use application factory method defined here http://flask.pocoo.org/docs/0.12/patterns/appfactories/ . related method http://flask.pocoo.org/docs/0.12/cli/#custom-scripts - possibility to add additional cli command - add flask shell context. - use flask global variable http://flask.pocoo.org/docs/0.12/api/#flask.g to store BukuDb instance * fix: dev; fix runtimerror error: RuntimeError: Working outside of application context. * chg: dev: api requirements - remove cryptography - add requests - set buku min version * chg: dev: fix bookmarks var * fix: test: lint
This commit is contained in:
parent
5c728030aa
commit
474c403521
@ -1,10 +1,10 @@
|
||||
appdirs==1.4.3
|
||||
beautifulsoup4==4.5.3
|
||||
buku==2.9
|
||||
buku>=2.9
|
||||
cffi==1.9.1
|
||||
click==6.7
|
||||
cryptography==1.7.2
|
||||
Flask==0.12
|
||||
requests==2.18.4
|
||||
Flask-API==0.6.9
|
||||
idna==2.5
|
||||
itsdangerous==0.24
|
||||
|
102
api/server.py
102
api/server.py
@ -1,27 +1,24 @@
|
||||
#!/usr/bin/env python
|
||||
from buku import BukuDb
|
||||
from flask import Flask, jsonify, request
|
||||
import response
|
||||
from flask.cli import FlaskGroup
|
||||
from flask_api import status
|
||||
import click
|
||||
import response
|
||||
import flask
|
||||
|
||||
|
||||
bukudb = BukuDb()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/api/tags', methods=['GET'])
|
||||
def get_tags():
|
||||
tags = bukudb.get_tag_all()
|
||||
tags = getattr(flask.g, 'bukudb').get_tag_all()
|
||||
result = {
|
||||
'tags': tags[0]
|
||||
}
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@app.route('/api/tags/<tag>', methods=['PUT'])
|
||||
def update_tag(tag):
|
||||
if request.method == 'PUT':
|
||||
result_flag = bukudb.replace_tag(tag, request.form.getlist('tags'))
|
||||
result_flag = getattr(flask.g, 'bukudb').replace_tag(tag, request.form.getlist('tags'))
|
||||
if result_flag:
|
||||
return jsonify(response.response_template['success']), status.HTTP_200_OK, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -30,10 +27,9 @@ def update_tag(tag):
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
@app.route('/api/bookmarks', methods=['GET', 'POST', 'DELETE'])
|
||||
def bookmarks():
|
||||
if request.method == 'GET':
|
||||
all_bookmarks = bukudb.get_rec_all()
|
||||
all_bookmarks = getattr(flask.g, 'bukudb').get_rec_all()
|
||||
result = {
|
||||
'bookmarks': []
|
||||
}
|
||||
@ -47,8 +43,8 @@ def bookmarks():
|
||||
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'])
|
||||
result_flag = getattr(flask.g, 'bukudb').add_rec(
|
||||
request.form['url'], request.form['title'], request.form['tags'], request.form['description'])
|
||||
if result_flag:
|
||||
return jsonify(response.response_template['success']), status.HTTP_200_OK, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -56,7 +52,7 @@ def bookmarks():
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
elif request.method == 'DELETE':
|
||||
result_flag = bukudb.cleardb()
|
||||
result_flag = getattr(flask.g, 'bukudb').cleardb()
|
||||
if result_flag:
|
||||
return jsonify(response.response_template['success']), status.HTTP_200_OK, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -65,12 +61,11 @@ def bookmarks():
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
@app.route('/api/bookmarks/refresh', methods=['POST'])
|
||||
def refresh_bookmarks():
|
||||
if request.method == 'POST':
|
||||
print(request.form['index'])
|
||||
print(request.form['threads'])
|
||||
result_flag = bukudb.refreshdb(request.form['index'], request.form['threads'])
|
||||
result_flag = getattr(flask.g, 'bukudb').refreshdb(request.form['index'], request.form['threads'])
|
||||
if result_flag:
|
||||
return jsonify(response.response_template['success']), status.HTTP_200_OK, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -79,7 +74,6 @@ def refresh_bookmarks():
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
@app.route('/api/bookmarks/<id>', methods=['GET', 'PUT', 'DELETE'])
|
||||
def bookmark_api(id):
|
||||
try:
|
||||
id = int(id)
|
||||
@ -87,7 +81,7 @@ def bookmark_api(id):
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
if request.method == 'GET':
|
||||
bookmark = bukudb.get_rec_by_id(id)
|
||||
bookmark = getattr(flask.g, 'bukudb').get_rec_by_id(id)
|
||||
if bookmark is not None:
|
||||
result = {
|
||||
'url': bookmark[1],
|
||||
@ -100,8 +94,8 @@ def bookmark_api(id):
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'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'])
|
||||
result_flag = getattr(flask.g, 'bukudb').update_rec(
|
||||
id, request.form['url'], request.form.get('title'), request.form['tags'], request.form['description'])
|
||||
if result_flag:
|
||||
return jsonify(response.response_template['success']), status.HTTP_200_OK, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -109,7 +103,7 @@ def bookmark_api(id):
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
else:
|
||||
result_flag = bukudb.delete_rec(id)
|
||||
result_flag = getattr(flask.g, 'bukudb').delete_rec(id)
|
||||
if result_flag:
|
||||
return jsonify(response.response_template['success']), status.HTTP_200_OK, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -118,7 +112,6 @@ def bookmark_api(id):
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
@app.route('/api/bookmarks/<id>/refresh', methods=['POST'])
|
||||
def refresh_bookmark(id):
|
||||
try:
|
||||
id = int(id)
|
||||
@ -126,7 +119,7 @@ def refresh_bookmark(id):
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
if request.method == 'POST':
|
||||
result_flag = bukudb.refreshdb(id, request.form['threads'])
|
||||
result_flag = getattr(flask.g, 'bukudb').refreshdb(id, request.form['threads'])
|
||||
if result_flag:
|
||||
return jsonify(response.response_template['success']), status.HTTP_200_OK, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -135,7 +128,6 @@ def refresh_bookmark(id):
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
@app.route('/api/bookmarks/<id>/tiny', methods=['GET'])
|
||||
def get_tiny_url(id):
|
||||
try:
|
||||
id = int(id)
|
||||
@ -144,7 +136,7 @@ def get_tiny_url(id):
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
if request.method == 'GET':
|
||||
shortened_url = bukudb.tnyfy_url(id)
|
||||
shortened_url = getattr(flask.g, 'bukudb').tnyfy_url(id)
|
||||
if shortened_url is not None:
|
||||
result = {
|
||||
'url': shortened_url
|
||||
@ -155,7 +147,6 @@ def get_tiny_url(id):
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
@app.route('/api/bookmarks/<id>/long', methods=['GET'])
|
||||
def get_long_url(id):
|
||||
try:
|
||||
id = int(id)
|
||||
@ -164,7 +155,7 @@ def get_long_url(id):
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
if request.method == 'GET':
|
||||
bookmark = bukudb.get_rec_by_id(id)
|
||||
bookmark = getattr(flask.g, 'bukudb').get_rec_by_id(id)
|
||||
if bookmark is not None:
|
||||
result = {
|
||||
'url': bookmark[1],
|
||||
@ -175,7 +166,6 @@ def get_long_url(id):
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
@app.route('/api/bookmarks/<starting_id>/<ending_id>', methods=['GET', 'PUT', 'DELETE'])
|
||||
def bookmark_range_operations(starting_id, ending_id):
|
||||
|
||||
try:
|
||||
@ -185,7 +175,7 @@ def bookmark_range_operations(starting_id, ending_id):
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
max_id = bukudb.get_max_id()
|
||||
max_id = getattr(flask.g, 'bukudb').get_max_id()
|
||||
if starting_id > max_id or ending_id > max_id:
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -195,8 +185,8 @@ def bookmark_range_operations(starting_id, ending_id):
|
||||
'bookmarks': {}
|
||||
}
|
||||
for i in range(starting_id, ending_id + 1, 1):
|
||||
bookmark = bukudb.get_rec_by_id(i)
|
||||
bookmarks[i] = {
|
||||
bookmark = getattr(flask.g, 'bukudb').get_rec_by_id(i)
|
||||
result['bookmarks'][i] = {
|
||||
'url': bookmark[1],
|
||||
'title': bookmark[2],
|
||||
'tags': list([_f for _f in bookmark[3].split(',') if _f]),
|
||||
@ -205,7 +195,7 @@ def bookmark_range_operations(starting_id, ending_id):
|
||||
return jsonify(result)
|
||||
elif request.method == 'DELETE':
|
||||
for i in range(starting_id, ending_id + 1, 1):
|
||||
result_flag = bukudb.delete_rec(i)
|
||||
result_flag = getattr(flask.g, 'bukudb').delete_rec(i)
|
||||
if result_flag is False:
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -214,8 +204,8 @@ def bookmark_range_operations(starting_id, ending_id):
|
||||
elif request.method == 'PUT':
|
||||
for i in range(starting_id, ending_id + 1, 1):
|
||||
updated_bookmark = request.form[str(i)]
|
||||
result_flag = bukudb.update_rec(i, updated_bookmark['url'], updated_bookmark['title'],
|
||||
updated_bookmark['tags'], updated_bookmark['description'])
|
||||
result_flag = getattr(flask.g, 'bukudb').update_rec(
|
||||
i, updated_bookmark['url'], updated_bookmark['title'], updated_bookmark['tags'], updated_bookmark['description'])
|
||||
|
||||
if result_flag is False:
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
@ -224,7 +214,6 @@ def bookmark_range_operations(starting_id, ending_id):
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
@app.route('/api/bookmarks/search', methods=['GET', 'DELETE'])
|
||||
def search_bookmarks():
|
||||
keywords = request.form.getlist('keywords')
|
||||
all_keywords = request.form.get('all_keywords')
|
||||
@ -238,7 +227,7 @@ def search_bookmarks():
|
||||
regex = regex if type(regex) == bool else regex.lower() == 'true'
|
||||
|
||||
results = {'bookmarks': []}
|
||||
found_bookmarks = bukudb.searchdb(keywords, all_keywords, deep, regex)
|
||||
found_bookmarks = getattr(flask.g, 'bukudb').searchdb(keywords, all_keywords, deep, regex)
|
||||
|
||||
if request.method == 'GET':
|
||||
if bookmarks is not None:
|
||||
@ -255,7 +244,7 @@ def search_bookmarks():
|
||||
elif request.method == 'DELETE':
|
||||
if found_bookmarks is not None:
|
||||
for bookmark in found_bookmarks:
|
||||
result_flag = bukudb.delete_rec(bookmark[0])
|
||||
result_flag = getattr(flask.g, 'bukudb').delete_rec(bookmark[0])
|
||||
if result_flag is False:
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
@ -263,7 +252,38 @@ def search_bookmarks():
|
||||
{'ContentType': 'application/json'}
|
||||
|
||||
|
||||
def run():
|
||||
app.run(debug=True)
|
||||
def create_app(config_filename=None):
|
||||
"""create app."""
|
||||
app = Flask(__name__)
|
||||
bukudb = BukuDb()
|
||||
app.app_context().push()
|
||||
setattr(flask.g, 'bukudb', bukudb)
|
||||
|
||||
run()
|
||||
@app.shell_context_processor
|
||||
def shell_context():
|
||||
"""Shell context definition."""
|
||||
return {'app': app, 'bukudb': bukudb}
|
||||
|
||||
# routing
|
||||
app.add_url_rule('/api/tags', 'get_tags', get_tags, methods=['GET'])
|
||||
app.add_url_rule('/api/tags/<tag>', 'update_tag', update_tag, methods=['PUT'])
|
||||
app.add_url_rule('/api/bookmarks', 'bookmarks', bookmarks, methods=['GET', 'POST', 'DELETE'])
|
||||
app.add_url_rule('/api/bookmarks/refresh', 'refresh_bookmarks', refresh_bookmarks, methods=['POST'])
|
||||
app.add_url_rule('/api/bookmarks/<id>', 'bookmark_api', refresh_bookmarks, methods=['GET', 'PUT', 'DELETE'])
|
||||
app.add_url_rule('/api/bookmarks/<id>/refresh', 'refresh_bookmark', refresh_bookmark, methods=['POST'])
|
||||
app.add_url_rule('/api/bookmarks/<id>/tiny', 'get_tiny_url', get_tiny_url, methods=['GET'])
|
||||
app.add_url_rule('/api/bookmarks/<id>/long', 'get_long_url', get_long_url, methods=['GET'])
|
||||
app.add_url_rule(
|
||||
'/api/bookmarks/<starting_id>/<ending_id>',
|
||||
'bookmark_range_operations', bookmark_range_operations, methods=['GET', 'PUT', 'DELETE'])
|
||||
app.add_url_rule('/api/bookmarks/search', 'search_bookmarks', search_bookmarks, methods=['GET', 'DELETE'])
|
||||
return app
|
||||
|
||||
|
||||
@click.group(cls=FlaskGroup, create_app=create_app)
|
||||
def cli():
|
||||
"""This is a management script for the wiki application."""
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
|
Loading…
x
Reference in New Issue
Block a user