-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (51 loc) · 1.92 KB
/
main.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
from flask import Flask, render_template, request
import requests
import json
# App config
app = Flask(__name__)
@app.route("/", methods=['GET'])
def start():
return render_template('index.html')
@app.route("/test", methods=['GET'])
def test():
return render_template('test.html')
@app.route("/code", methods=['GET'])
def code():
with open('main.py', 'r') as f:
data = f.read().replace('<', '<').replace('>', '>')
return render_template('code.html', data=data)
@app.route("/suggest", methods=['GET'])
def suggest():
query = request.args.get('q')
token = request.args.get('t')
endpoint = request.args.get('e')
username = request.args.get('u')
password = request.args.get('p')
url = endpoint + "?q=" + query
session = requests.Session()
session.trust_env = False # bypass proxies
if token != "":
headers = {"Authorization": "Bearer " + token}
r = session.get(url, headers=headers, verify=False)
elif username != "" and password != "":
r = session.get(url, auth=(username, password), verify=False)
else:
r = session.get(url, verify=False)
if r.status_code != 200:
err = str(r.status_code) + " error on backend request"
return str([err]).replace("\'", "\"")
r_json = json.loads(r.text)
r_list = []
if "suggest" in r_json:
# use this when parsing Solr SUGGEST queries
for x in r_json["suggest"]["Suggestions"][query]["suggestions"]:
r_list.append(x["term"])
elif "response" in r_json:
# use this when parsing Solr SELECT queries
for x in r_json["response"]["docs"]:
r_list.append(x["query"]) # 'query' should match the field name returned in the JSON docs list
else:
return str(["unable to parse Fusion response"])
return str(r_list).replace("\'", "\"")
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8080, debug=True)