-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
63 lines (51 loc) · 1.62 KB
/
api.py
File metadata and controls
63 lines (51 loc) · 1.62 KB
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
from flask import Flask
from flask import jsonify
from flask import request
from flask_pymongo import PyMongo
import json
from bson.json_util import dumps
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'references'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/references'
mongo = PyMongo(app)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/test', methods=['GET'])
def get_landing():
""" testing landing page
:return:
"""
return jsonify({'result': "This is the landing page of the Credibility API"})
@app.route('/all', methods=['GET'])
def get_all():
""" Get all data from the database
:return:
"""
references = mongo.db.all
output = []
for reference in references.find():
data = {}
if 'title' in reference:
data['title'] = reference['title']
data.update({'page': reference['page'], 'qid': reference['qid'], 'url': reference['url'],
'url_domain': reference['url_domain']})
output.append(data)
return jsonify({'result': output})
@app.route('/api/<query>', methods=['GET'])
def get_query(query):
""" Return result for any mongodb find query
:param query: mongodb query, prefixed with query=
:return:
"""
parsed_query = json.loads(query.replace('query=', ''))
print(parsed_query)
references = mongo.db.all
query_result = references.find(parsed_query)
if query_result:
output = json.loads(dumps(query_result))
else:
output = "This query yields no results"
return jsonify({'result': output})
if __name__ == '__main__':
app.run(debug=True)