-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
149 lines (130 loc) · 4.59 KB
/
server.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Questo modulo utilizza Flask per realizzare un web server. L'applicazione può essere eseguita in vari modi
# FLASK_APP=server.py FLASK_ENV=development flask run
# python server.py se aggiungiamo a questo file app.run()
from flask import Flask, request
import user
from enum import Enum
import message
import jwt
# viene creata l'applicazione con il nome del modulo corrente.
app = Flask(__name__)
class Result(Enum):
OK = 1
NOT_FOUND = 2
NOT_AUTHORIZED = 3
DUPLICATED = 4
#Never save the private key in your code, but in this case is "Carino"
private_key = "gaiaiswatchingyou"
# getErrorCode è una funzione di utilità che mappa i valori ritornati dal modulo user con quelli del
# protocollo HTTP in caso di errore.
# 404 - Not Found: una risorsa non è stata trovata sul server;
# 403 - Forbidden: accesso negato;
# 409 - Conflict: è violato un vincolo di unicità. Ad esempio, esiste già un utente con la stessa mail registrata;
# Come ultima spiaggia è buona norma ritornare "500 - Internal Server Error" per indicare che qualcosa è andato storto
def getErrorCode(result: user.Result)->int:
if result is user.Result.NOT_FOUND:
code = 404
elif result is user.Result.NOT_AUTHORIZED:
code = 403
elif result is user.Result.DUPLICATED:
code = 409
else:
code = 500
return code
@app.route('/user', methods=['POST'])
def createUser():
data = request.get_json()
name = data['name']
surname = data['surname']
email = data['mail']
password = data['password']
result, u = user.SaveUser(name, surname, email, password)
if result is not user.Result.OK:
code = getErrorCode(result)
return '', code
else:
return u, 201
@app.route('/user/find', methods=['GET'])
def findUser ():
data = request.get_json()
credential = request.args.get('cred')
#Auth here
u = user.findUserByEmail(credential)
#Check response
#First email than password
if u is None:
u = user.findUserByID(credential)
if u is None:
code = 404
return 'User not found', code
else:
return 'User {0}{1} found'.format(u["name"], u["surname"]), 201
else:
return 'User {0}{1} found'.format(u["name"], u["surname"]), 201
@app.route('/user/login', methods=["POST"])
def login ():
data = request.get_json()
email = data['email']
password = data['password']
result, u = user.Login(email,password)
if result is not user.Result.OK:
code = getErrorCode(result)
return "Sorry we don't know you", code
else:
try:
credentials = {"email": u["email"], "password":u["password"]}
encoded_jwt = jwt.encode(credentials, private_key, algorithm="HS256")
return encoded_jwt, 200
except:
return "An error occurred", 500
@app.route('/user/delete', methods=["DELETE"])
def deleteUser():
data = request.get_json()
user_id = request.args.get('id')
result = user.DeleteUser(user_id)
if result is not user.Result.OK:
code = getErrorCode(result)
return '', code
else:
return '', 200
@app.route('/user/create_message', methods=["POST"])
def createMessage():
data = request.get_json()
user = Auth()
#Insert Authorization
try:
receiverEmail = data["receiver"]
receiver = findUserByEmail(receiver)
if receiver is None:
return "Receiver not found", 404
else:
senderID = user["id"]
receiverID = receiver["id"]
content = data["content"]
text = {'receiver': receiverID, 'sender': senderID, 'content': content}
message.saveMessage(text)
return "Message sent successfully", 201
except:
return 'Something went wrong, check your fields', 500
@app.route('/user/inbox', methods=["GET"])
def retrieveConversation():
data = request.get_json()
user = Auth()
#Get mail from sender and receiver
receiverEmail = data.args.get('receiver')
receiver = user.findUserByEmail(receiverEmail)
if receiver is not None:
receiver_id = receiver["id"]
sender_id = user["id"]
try:
message = message.retrieveConversation(receiverEmail, senderEmail)
if message is None:
return "Conversation is Empty", 200
else:
return message, 200
except:
return "A problem occured during creation"
else:
return "User not Found", 404
if __name__ == '__main__':
app.run(host='localhost',port=5000,debug=True)