-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
120 lines (105 loc) · 4.03 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#*******************************************************************************
# Copyright (C) 2021-2022 CERTH
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#*******************************************************************************
from flask import Flask, jsonify, request
from flask_cors import CORS
from waitress import serve
from test import all_together
import json
from bson import ObjectId
#from utils import import_to_database, objectid_to_string
# Create the Flask app
app = Flask(__name__)
# Enable CORS
CORS(app)
#===============================================================================
# database_results ()
#===============================================================================
@app.route('/DataBase', methods=['POST','GET'])
def database_results(requirement=None,pattern=None, technology=None, language=None):
"""
API Call to database
Arguments:
requirement: Required (sent as URL query parameter from API Call)
pattern: Optional (sent as URL query parameter from API Call)
technology: Optional (sent as URL query parameter from API Call)
language: Optional (sent as URL query parameter from API Call)
Returns:
A JSON containing the results collected from the database.
"""
# Parse URL-encoded parameters
requirement = request.args.get('requirement', type=str) # Required: if key doesn't exist, returns None
pattern = request.args.get('pattern', default=None,type=str) # Optional: if key doesn't exist, returns None
technology = request.args.get('technology', default=None, type=str) # Optional: if key doesn't exist, returns None
language = request.args.get('language', default=None, type=str) # Optional: if key doesn't exist, returns None
if requirement is None:
return unprocessable_entity()
else:
results=all_together(requirement,pattern,technology,language)
results=JSONEncoder().encode(results)
#print(results)
# Compose and jsonify respond
#message = {'status': 200,'message': 'The request was fulfilled.','results': results}
#resp = message
#resp.status_code = 200
#import json
#results = json.dumps(results.json(), indent=4)
return results
import json
from bson import ObjectId
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
return json.JSONEncoder.default(self, o)
#JSONEncoder().encode(analytics)
def objectid_to_string(dict_obj):
"""
Convert all fields of dict that are ObjectIds to str.
Arguments:
dict_obj: A dict object.
Returns:
A dict object with Pymongo ObjectId as string.
"""
for key in dict_obj:
if isinstance(dict_obj[key], ObjectId):
dict_obj[key] = str(dict_obj[key])
return dict_obj
#===============================================================================
# errorhandler ()
#===============================================================================
@app.errorhandler(400)
def bad_request(error=None):
message = {
'status': 400,
'message': 'Bad Request: ' + request.url + ' --> Please check your data payload.',
}
resp = jsonify(message)
resp.status_code = 400
return resp
@app.errorhandler(422)
def unprocessable_entity(error=None):
message = {
'status': 400,
'message': 'Unprocessable Entity: ' + request.url + ' --> Missing or invalid parameters.',
}
resp = jsonify(message)
resp.status_code = 400
return resp
@app.errorhandler(500)
def internal_server_error(error=None):
message = {
'status': 500,
'message': 'The server encountered an internal error and was unable to complete your request. ' + error,
}
resp = jsonify(message)
resp.status_code = 500
return resp
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)