api_wrapper

This commit is contained in:
James Turk 2011-08-02 18:33:36 -04:00
parent f6072cdb9b
commit c2a60aea94

View File

@ -1,5 +1,6 @@
import json import json
import datetime import datetime
import functools
import flask import flask
import pymongo.objectid import pymongo.objectid
@ -17,37 +18,54 @@ class JSONEncoder(json.JSONEncoder):
return super(JSONEncoder, self).default(obj) return super(JSONEncoder, self).default(obj)
def api_wrapper(template=None):
def wrapper(func):
@functools.wraps(func)
def newfunc(*args, **kwargs):
data = func(*args, **kwargs)
if 'json' in flask.request.args or not template:
return json.dumps(data, cls=JSONEncoder)
else:
return flask.render_template(template, **data)
return newfunc
return wrapper
app = flask.Flask('oyster') app = flask.Flask('oyster')
client = Client() client = Client()
@app.route('/status/') @app.route('/status/')
@api_wrapper()
def doc_list(): def doc_list():
status = { status = {
'queue_size': app.work_queue.qsize(), 'queue_size': app.work_queue.qsize(),
'tracking': client.db.tracked.count(), 'tracking': client.db.tracked.count(),
'need_update': client.get_update_queue_size(), 'need_update': client.get_update_queue_size(),
} }
return json.dumps(status) return status
@app.route('/log/') @app.route('/log/')
@api_wrapper('logs.html')
def log_view(): def log_view():
offset = int(flask.request.args.get('offset', 0)) offset = int(flask.request.args.get('offset', 0))
size = 100 size = 100
prev_offset = max(offset - size, 0) prev_offset = max(offset - size, 0)
next_offset = offset + size next_offset = offset + size
logs = client.db.logs.find().sort('$natural', -1).skip(offset).limit(size) logs = client.db.logs.find().sort('$natural', -1).skip(offset).limit(size)
return flask.render_template('logs.html', logs=logs, return dict(logs=logs, prev_offset=prev_offset,
prev_offset=prev_offset, next_offset=next_offset, offset=offset)
next_offset=next_offset,
offset=offset)
@app.route('/tracked/') @app.route('/tracked/')
@api_wrapper()
def tracked(): def tracked():
tracked = list(client.db.tracked.find()) tracked = list(client.db.tracked.find())
return json.dumps(tracked, cls=JSONEncoder) return json.dumps(tracked, cls=JSONEncoder)
@app.route('/tracked/<path:url>') @app.route('/tracked/<path:url>')
def tracked_view(url): def tracked_view(url):
doc = client.db.tracked.find_one({'url': url}) doc = client.db.tracked.find_one({'url': url})