-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (54 loc) · 2.41 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
from flask import Flask, request, jsonify
from api.functions.GetClientIP import GetClientIP
from api.service.ListRecommendationsService import ListRecommendationsService
from api.service.UploadFileService import UploadFileService
from api.service.DeleteFileService import DeleteFileService
from api.service.ListFilesService import ListFilesService
app = Flask(__name__)
class RecommendationsController:
@app.route('/')
def index():
return 'Recommendations API'
@app.route('/upload', methods=['POST'])
def uploadFile():
if 'file' not in request.files:
return jsonify({'error': 'Missing file parameter'}), 400
file = request.files['file']
id_column_name = request.form.get('id_column_name')
title_column_name = request.form.get('title_column_name')
user_id = request.remote_addr
result = UploadFileService.uploadFile(file, id_column_name, title_column_name, user_id)
if 'error' in result:
return jsonify(result), 400
return jsonify(result), 200
@app.route('/recommend', methods=['GET'])
def getRecommendations():
data = request.get_json()
id = data.get('id')
quantity = data.get('quantity')
file_name = data.get('file_name')
user_id = GetClientIP.getClientIP(request)
if user_id is None:
return jsonify({'error': 'Unable to get user IP'}), 400
recommendations = ListRecommendationsService.listRecommendations(id, quantity, user_id, file_name)
if 'error' in recommendations:
return jsonify(recommendations), 400
return jsonify({'recommendations': recommendations}), 200
@app.route('/files', methods=['GET'])
def listFiles():
user_id = GetClientIP.getClientIP(request)
list_files = ListFilesService.listFiles(user_id)
if 'error' in list_files:
return jsonify(list_files), 404
return jsonify(list_files), 200
@app.route('/delete', methods=['DELETE'])
def deleteFile():
data = request.get_json()
file_name = data.get('file_name')
user_id = GetClientIP.getClientIP(request)
result = DeleteFileService.deleteFile(user_id, file_name)
if 'error' in result:
return jsonify(result), 400
return jsonify(result), 200
if __name__ == '__main__':
app.run(debug=True)