-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_api.py
179 lines (154 loc) · 8.44 KB
/
client_api.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import json
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
from labchain.util.cryptoHelper import CryptoHelper
from labchain.network.networking import ClientNetworkInterface, JsonRpcClient
from labchain.util.TransactionFactory import TransactionFactory
from labchain.util.TasksManager import TasksManager
wallet_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'wallet.json'))
def create_app():
app = Flask(__name__)
CORS(app)
with open(wallet_file_path, 'r') as file:
app.wallet = json.load(file)[0]['wallet']
#logging.basicConfig(level=logging.DEBUG)
app.cases_map = {} # for each controller or hospital we keep a counter to generate new case ID
app.crypto_helper = CryptoHelper.instance()
app.network_interface = ClientNetworkInterface(JsonRpcClient(), {'localhost': { '8080': {}}})
return app
app = create_app()
@app.route('/createCase',methods=['POST'])
def createCase():
data = request.get_json(force=True)
case_ID = app.network_interface.get_highest_workflow_ID()[0]
case_ID += 1
controller_public_key = app.wallet[data['controller']]['public_key']
controller_private_key = app.wallet[data['controller']]['private_key']
physician_public_key = app.wallet[data['physician']]['public_key']
doctor_public_key = app.wallet[data['doctor']]['public_key']
chef_public_key = app.wallet[data['chef']]['public_key']
transaction = TransactionFactory.create_case_transaction(case_ID,controller_public_key,physician_public_key,doctor_public_key,chef_public_key, data['doctor'], data['chef'])
transaction.sign_transaction(app.crypto_helper, controller_private_key)
try:
app.network_interface.sendTransaction(transaction)
return jsonify(message='success',case_ID=case_ID)
except Exception as e:
return jsonify(message='fail', description=str(e))
@app.route('/sendAssumedDiagnosis', methods=['POST'])
def send_assumed_diagnosis():
if request.method == 'POST':
data = request.get_json(force=True)
case_ID = data['case_id'] if 'case_id' in data else '0'
physician_private_key = app.wallet[data['physician']]['private_key']
physician_public_key = app.wallet[data['physician']]['public_key']
doctor_public_key = app.wallet[data['doctor']]['public_key']
workflow_transaction = data['workflow_transaction']
previous_transaction = data['previous_transaction']
assumed_diagnosis = data['diagnosis']
try:
transaction = TransactionFactory.create_assumed_diagnosis_transaction(case_ID,physician_public_key,
doctor_public_key,
assumed_diagnosis,
workflow_transaction,
previous_transaction)
transaction.sign_transaction(app.crypto_helper, physician_private_key)
app.network_interface.sendTransaction(transaction)
return jsonify(message='success')
except Exception as e:
return jsonify(message='fail', description=str(e))
@app.route('/sendRealDiagnosis', methods=['POST'])
def send_real_diagnosis():
if request.method == 'POST':
data = request.get_json(force=True)
case_ID = data['case_id'] if 'case_id' in data else '0'
doctor_private_key = app.wallet[data['doctor']]['private_key']
doctor_public_key = app.wallet[data['doctor']]['public_key']
chef_public_key = app.wallet[data['chef']]['public_key']
workflow_transaction = data['workflow_transaction']
previous_transaction = data['previous_transaction']
real_diagnosis = data['diagnosis']
try:
transaction = TransactionFactory.create_real_diagnosis_transaction(case_ID,doctor_public_key,
chef_public_key,
real_diagnosis,
workflow_transaction,
previous_transaction)
transaction.sign_transaction(app.crypto_helper, doctor_private_key)
app.network_interface.sendTransaction(transaction)
return jsonify(message='success')
except Exception as e:
return jsonify(message='fail', description=str(e))
@app.route('/showAllDiagnosis', methods=['POST'])
def show_all_diagnosis():
data = request.get_json(force=True)
public_key = app.wallet[data['username']]['public_key']
try:
real_diagnosis_transactions = app.network_interface.search_transaction_from_receiver(public_key)
diaggnosis_list = []
for real_diagnosis_transaction in real_diagnosis_transactions:
previous_transaction = real_diagnosis_transaction.payload['previous_transaction']
assumed_diagnossis_transaction = app.network_interface.requestTransaction(previous_transaction)[0]
diaggnosis = {}
diaggnosis['workflow_id'] = real_diagnosis_transaction.payload['workflow_id']
diaggnosis['real_diagnosis'] = real_diagnosis_transaction.payload['document']['real_diagnosis']
diaggnosis['assumed_diagnosis'] = assumed_diagnossis_transaction.payload['document']['assumed_diagnosis']
diaggnosis_list.append(diaggnosis)
return json.dumps(diaggnosis_list)
except Exception as e:
return jsonify(message='fail', description=str(e))
@app.route('/showDiagnosisWithPhysicianID', methods=['POST'])
def show_all_diagnosis_with_physicianID():
data = request.get_json(force=True)
public_key = app.wallet[data['username']]['public_key'] # physicainID
true_diagnosis_transactions = []
cases = {}
output = []
try:
assumed_diagnosis_transactions = app.network_interface.search_transaction_from_sender(public_key)
for assumed_diagnosis_transaction in assumed_diagnosis_transactions:
doctorID = assumed_diagnosis_transaction.receiver
true_diagnosis_transactions.extend(app.network_interface.search_transaction_from_sender(doctorID))
# Match the case ID between assumed diagnosis
for assumed_diagnosis_transaction in assumed_diagnosis_transactions:
case_id = assumed_diagnosis_transaction.payload['workflow_id']
if case_id not in cases:
cases[case_id] = [assumed_diagnosis_transaction.payload['document']['assumed_diagnosis'],'Pending']
else:
cases[case_id][0] = assumed_diagnosis_transaction.payload['document']['assumed_diagnosis']
for true_diagnosis_transaction in true_diagnosis_transactions:
case_id_assumed = true_diagnosis_transaction.payload['workflow_id']
if case_id_assumed == case_id:
if case_id not in cases:
cases[case_id] = ['Pending',true_diagnosis_transaction.payload['document']['real_diagnosis']]
else:
cases[case_id][1] = true_diagnosis_transaction.payload['document']['real_diagnosis']
for case_id in cases:
output.append({
'case_id': case_id,
'assumed_diagnosis': cases[case_id][0],
'true_diagnosis': cases[case_id][1],
})
return json.dumps(output)
except Exception as e:
return jsonify(message='fail', description=str(e))
@app.route('/checkTasks',methods=['POST'])
def checkTasks():
data = request.get_json(force=True)
public_key = app.wallet[data['username']]['public_key']
try:
transactions = TasksManager.check_tasks(app.network_interface, public_key)
tasks = TasksManager.get_tasks_objects_from_task_transactions(app.network_interface, transactions)
return json.dumps([ob.__dict__ for ob in tasks])
except Exception as e:
return jsonify(message='fail', description=str(e))
def genrate_case_ID(controller):
if (controller in app.cases_map):
current_case_id = app.cases_map[controller]
app.cases_map[controller] = current_case_id + 1
return current_case_id
else:
app.cases_map[controller] = 1
return 0
if __name__ == '__main__':
app.run(debug=True)