-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplicaServer.py
121 lines (97 loc) · 3.61 KB
/
ReplicaServer.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
##################### Library Imports #####################
import GateDataReplica
from flask import Flask, request, jsonify, abort
from random import randint
# -- ADINT Intermidiate Project
# -- Made by: Diogo Ferreira and Rafael Cordeiro
# ----------------------------------------
# --------------Gate Data Server----------
# ----------------------------------------
#Flask
app = Flask(__name__)
#Data Base Connections
@app.route("/gate/listGates",methods = ['GET'])
def listGatesRequest():
# Call a query to list the active gates
try:
Gates_list = GateDataReplica.getGates()
result = [{'ID':item.id,'Location':item.location,'Secret Number':item.secret_number,'Activations Number':item.activation_number} for item in Gates_list]
except:
return jsonify({'Gates':'', 'StatusCode':'2', 'Description':'Err'})
return jsonify({'Gates':result,'StatusCode':'1','Description':'OK'})
# Database endpoint for adding new gates
@app.route("/gate/newGates",methods = ['POST'])
def newGatesRequest():
# Create a random Secret number to be associated to the new gate to be registered
secret_number = randint(1,2020210)
while(GateDataReplica.SecretExist(secret_number)):
secret_number = randint(1,2020210)
#retrieve data from input JSON body
data = request.json
try:
int(data["gateID"])
data["gateLocation"]
except:
abort(400)
# Call query in GateData to create a new gate and add it do the database
try:
status = GateDataReplica.newGate(int(data["gateID"]),data["gateLocation"],secret_number,0)
except:
return jsonify({'StatusCode':'2', 'Description':'Err'})
if status:
#return the secret number as JSON
return jsonify({'StatusCode':'1', 'Description':'OK'})
else:
# The ID already exists - Not Admitted
return jsonify({'StatusCode':'3', 'Description':'ID Already Exists'})
# Endpoint to increase the gate activation number
@app.route("/gate/<path:id>/admission",methods = ['GET'])
def updateGatesActivation(id):
try:
int(id)
except:
abort(400)
try:
status = GateDataReplica.UpdateGateActCode(int(id))
except:
return jsonify({'StatusCode':'2', 'Description':'Err'})
if status:
return jsonify({'StatusCode':'1','Description':'OK'})
else:
return jsonify({'StatusCode':'3','Description':'Table Error, ID does not Exist'})
# Endpoint to Check Secret in the data base section
@app.route("/gate/GateSecret/<path:id>",methods = ['GET'])
def CheckSecret(id):
#retrieve data from input JSON body
data = request.json
try:
int(id)
data["secret"]
except:
abort(400)
try:
aux = GateDataReplica.CheckSecret(int(id),data["secret"])
except:
return jsonify({'Valid':'0'})
if aux:
return jsonify({'Valid':'1'})
else:
return jsonify({'Valid':'0'})
# Endpint to delete a specific gate
@app.route("/gate/<path:id>/inactive",methods = ['GET'])
def Deletegate(id):
try:
int(id)
except:
abort(400)
try:
status = GateDataReplica.DeleteGate(int(id))
except:
return jsonify({'StatusCode':'2', 'Description':'Err'})
if status:
return jsonify({'StatusCode':'1','Description':'OK'})
else:
return jsonify({'StatusCode':'3','Description':'Table Error, ID does not Exist'})
#Start server
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8004, debug=True)