-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain_endpoints.py
More file actions
71 lines (56 loc) · 1.82 KB
/
Copy pathblockchain_endpoints.py
File metadata and controls
71 lines (56 loc) · 1.82 KB
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
# ---------------------------------------------------
# Communication with the Blockchain
# ---------------------------------------------------
import requests
import json
# VPN is needed to connect to NCSRD infrastructure
url = "http://10.160.3.213:3001"
# ---------------------------------------------------
# Function to obtain the JWT token needed for authentication
# Note: Token is valid for one day
def obtain_jwt_token(url):
# URL containing the issue name
url_token = url + "/issueJwtToken"
payload = {}
headers = {
'Authorization': 'Bearer {{jwtEverything}}'
}
response = requests.request("GET", url_token, headers=headers, data=payload)
# Extract the token from the received data
response_json = response.json()
token = response_json.get("data")
return token
# ---------------------------------------------------
# Function to upload a json structure to the Blockchain
def push_data(json_structure, url, jwt_token):
# URL containing the issue name
url_push_data = url + "/api/transactions/storeFPGAData"
# Header for pushing data
header = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {jwt_token}'
}
# print("post")
# Push the data to the Blockchain
response = requests.request(
"POST",
url_push_data,
headers=header,
data=json_structure,
timeout=10
)
# print("debug" + response.status_code)
# print(response.text)
# print(response.text)
# ---------------------------------------------------
# Obtain data from the Blockchain using FPGA-ID
def obtain_data(fpga_id, url):
# URL containing the issue name
url_obtain = url + "/api/transactions/GetByFPGAID?FPGAID=" + fpga_id
payload = {}
header = {
'Authorization': f'Bearer {jwt_token}'
}
# Obtain data from the Blockchain
response = requests.request("GET", url_obtain, headers=header, data=payload)
return response