-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
74 lines (55 loc) · 2.32 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import argparse
import json
import logging
from flask import Flask, Response, request, jsonify
from flask.ext.cors import CORS
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from util import errors
#logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)
app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
CORS(app, resources={r"/api/*" : {"origins" : "*"}})
@app.errorhandler(errors.CustomAPIError)
def handle_invalid_usage(error):
response = error.to_dict()
return response
@app.route('/api/<tasktype>/<taskname>', methods=['GET'])
def run_task_get(tasktype, taskname):
raise errors.CustomAPIError('Method not allowed', status_code=405, payload={'method':'GET'})
@app.route('/api/<tasktype>/<taskname>', methods=['POST'])
def run_task(tasktype, taskname):
"""Run named task on a document fed as POST data.
The POST data should have Content-type application/json.
"""
try:
tasktype = str(tasktype)
taskname = str(taskname)
nlp = getattr(__import__("nlp", fromlist=[tasktype]), tasktype)
func = getattr(nlp, taskname)
except AttributeError:
raise errors.CustomAPIError('Cannot locate endpoint', status_code=404, payload={'endpoint':'api/%s/%s'%(tasktype,taskname)})
else:
content_type = request.headers['Content-Type']
if content_type == 'application/json':
kwargs = request.get_json()
result = func(**kwargs)
return jsonify(result=result, status=200)
raise errors.CustomAPIError('Unsupported content-type', status_code=415, payload={'content-type':content_type})
@app.route('/<path:path>', methods=['GET','POST'])
def catchall(path):
raise errors.CustomAPIError('Cannot locate endpoint', status_code=404, payload={'endpoint':path})
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="ctTrakr web server")
parser.add_argument('--host', dest='host', default='127.0.0.1',
help='Host to listen on.')
parser.add_argument('--port', dest='port', default=5000, type=int,
help='Port to listen on.')
parser.add_argument('--threads', dest='threads', default=1, type=int,
help='Number of threads.')
args = parser.parse_args()
http_server = HTTPServer(WSGIContainer(app))
http_server.bind(args.port, address=args.host)
http_server.start(args.threads)
IOLoop.instance().start()