neuronum 2.0.8__py3-none-any.whl → 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of neuronum might be problematic. Click here for more details.
- cli/main.py +205 -146
- neuronum/neuronum.py +394 -349
- neuronum-3.0.0.dist-info/METADATA +101 -0
- neuronum-3.0.0.dist-info/RECORD +12 -0
- neuronum-2.0.8.dist-info/METADATA +0 -385
- neuronum-2.0.8.dist-info/RECORD +0 -12
- {neuronum-2.0.8.dist-info → neuronum-3.0.0.dist-info}/WHEEL +0 -0
- {neuronum-2.0.8.dist-info → neuronum-3.0.0.dist-info}/entry_points.txt +0 -0
- {neuronum-2.0.8.dist-info → neuronum-3.0.0.dist-info}/licenses/LICENSE +0 -0
- {neuronum-2.0.8.dist-info → neuronum-3.0.0.dist-info}/top_level.txt +0 -0
neuronum/neuronum.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
from typing import Optional, Generator
|
|
1
|
+
import aiohttp
|
|
2
|
+
from typing import Optional, AsyncGenerator
|
|
4
3
|
import ssl
|
|
5
|
-
|
|
4
|
+
import websockets
|
|
6
5
|
import json
|
|
6
|
+
import asyncio
|
|
7
|
+
from bleak import BleakScanner
|
|
7
8
|
|
|
8
9
|
class Cell:
|
|
9
10
|
def __init__(self, host: str, password: str, network: str, synapse: str):
|
|
@@ -11,7 +12,7 @@ class Cell:
|
|
|
11
12
|
self.password = password
|
|
12
13
|
self.network = network
|
|
13
14
|
self.synapse = synapse
|
|
14
|
-
self.
|
|
15
|
+
self.queue = asyncio.Queue()
|
|
15
16
|
|
|
16
17
|
def to_dict(self) -> dict:
|
|
17
18
|
return {
|
|
@@ -22,17 +23,9 @@ class Cell:
|
|
|
22
23
|
|
|
23
24
|
def __repr__(self) -> str:
|
|
24
25
|
return f"Cell(host={self.host}, password={self.password}, network={self.network}, synapse={self.synapse})"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def authenticate(self, stx: Optional[str] = None):
|
|
28
|
-
credentials = f"{self.host}\n{self.password}\n{self.synapse}\n{stx}\n"
|
|
29
|
-
self.sock.sendall(credentials.encode('utf-8'))
|
|
30
|
-
|
|
31
|
-
response = self.sock.recv(1024).decode('utf-8')
|
|
32
|
-
return "Authentication successful" in response
|
|
33
|
-
|
|
26
|
+
|
|
34
27
|
|
|
35
|
-
def create_tx(self, descr: str, key_values: dict, stx: str, label: str, partners: list):
|
|
28
|
+
async def create_tx(self, descr: str, key_values: dict, stx: str, label: str, partners: list):
|
|
36
29
|
url = f"https://{self.network}/api/create_tx"
|
|
37
30
|
|
|
38
31
|
TX = {
|
|
@@ -44,23 +37,20 @@ class Cell:
|
|
|
44
37
|
"cell": self.to_dict()
|
|
45
38
|
}
|
|
46
39
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
url,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
response.raise_for_status()
|
|
54
|
-
|
|
55
|
-
return response.json()["txID"]
|
|
40
|
+
async with aiohttp.ClientSession() as session:
|
|
41
|
+
try:
|
|
42
|
+
async with session.post(url, json=TX) as response:
|
|
43
|
+
response.raise_for_status()
|
|
44
|
+
data = await response.json()
|
|
45
|
+
return data["txID"]
|
|
56
46
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
except aiohttp.ClientError as e:
|
|
48
|
+
print(f"Error sending request: {e}")
|
|
49
|
+
except Exception as e:
|
|
50
|
+
print(f"Unexpected error: {e}")
|
|
61
51
|
|
|
62
52
|
|
|
63
|
-
def delete_tx(self, txID: str):
|
|
53
|
+
async def delete_tx(self, txID: str):
|
|
64
54
|
url = f"https://{self.network}/api/delete_tx"
|
|
65
55
|
|
|
66
56
|
TX = {
|
|
@@ -68,23 +58,22 @@ class Cell:
|
|
|
68
58
|
"cell": self.to_dict()
|
|
69
59
|
}
|
|
70
60
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
url,
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
print(f"Response from Neuronum: {response.json()}")
|
|
61
|
+
async with aiohttp.ClientSession() as session:
|
|
62
|
+
try:
|
|
63
|
+
async with session.post(url, json=TX) as response:
|
|
64
|
+
response.raise_for_status()
|
|
65
|
+
data = await response.json()
|
|
66
|
+
print(f"Response from Neuronum: {data}")
|
|
67
|
+
return data
|
|
80
68
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
69
|
+
except aiohttp.ClientError as e:
|
|
70
|
+
print(f"Error sending request: {e}")
|
|
71
|
+
except Exception as e:
|
|
72
|
+
print(f"Unexpected error: {e}")
|
|
85
73
|
|
|
86
74
|
|
|
87
|
-
def activate_tx(self, txID: str, data: dict):
|
|
75
|
+
async def activate_tx(self, txID: str, data: dict):
|
|
76
|
+
"""Asynchronously sends TX activation request to the API."""
|
|
88
77
|
url = f"https://{self.network}/api/activate_tx/{txID}"
|
|
89
78
|
|
|
90
79
|
TX = {
|
|
@@ -92,23 +81,21 @@ class Cell:
|
|
|
92
81
|
"cell": self.to_dict()
|
|
93
82
|
}
|
|
94
83
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
url,
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
print(f"Response from Neuronum: {response.json()}")
|
|
84
|
+
async with aiohttp.ClientSession() as session:
|
|
85
|
+
try:
|
|
86
|
+
async with session.post(url, json=TX) as response:
|
|
87
|
+
response.raise_for_status()
|
|
88
|
+
data = await response.json()
|
|
89
|
+
print(f"Response from Neuronum: {data}")
|
|
90
|
+
return data
|
|
104
91
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
92
|
+
except aiohttp.ClientError as e:
|
|
93
|
+
print(f"Error sending request: {e}")
|
|
94
|
+
except Exception as e:
|
|
95
|
+
print(f"Unexpected error: {e}")
|
|
109
96
|
|
|
110
97
|
|
|
111
|
-
def create_ctx(self, descr: str, partners: list):
|
|
98
|
+
async def create_ctx(self, descr: str, partners: list):
|
|
112
99
|
url = f"https://{self.network}/api/create_ctx"
|
|
113
100
|
|
|
114
101
|
CTX = {
|
|
@@ -117,23 +104,20 @@ class Cell:
|
|
|
117
104
|
"cell": self.to_dict()
|
|
118
105
|
}
|
|
119
106
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
url,
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
response.raise_for_status()
|
|
127
|
-
|
|
128
|
-
return response.json()["ctxID"]
|
|
107
|
+
async with aiohttp.ClientSession() as session:
|
|
108
|
+
try:
|
|
109
|
+
async with session.post(url, json=CTX) as response:
|
|
110
|
+
response.raise_for_status()
|
|
111
|
+
data = await response.json()
|
|
112
|
+
return data["ctxID"]
|
|
129
113
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
114
|
+
except aiohttp.ClientError as e:
|
|
115
|
+
print(f"Error sending request: {e}")
|
|
116
|
+
except Exception as e:
|
|
117
|
+
print(f"Unexpected error: {e}")
|
|
134
118
|
|
|
135
119
|
|
|
136
|
-
def delete_ctx(self, ctxID: str):
|
|
120
|
+
async def delete_ctx(self, ctxID: str):
|
|
137
121
|
url = f"https://{self.network}/api/delete_ctx"
|
|
138
122
|
|
|
139
123
|
CTX = {
|
|
@@ -141,23 +125,21 @@ class Cell:
|
|
|
141
125
|
"cell": self.to_dict()
|
|
142
126
|
}
|
|
143
127
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
url,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
print(f"Response from Neuronum: {response.json()}")
|
|
128
|
+
async with aiohttp.ClientSession() as session:
|
|
129
|
+
try:
|
|
130
|
+
async with session.post(url, json=CTX) as response:
|
|
131
|
+
response.raise_for_status()
|
|
132
|
+
data = await response.json()
|
|
133
|
+
print(f"Response from Neuronum: {data}")
|
|
134
|
+
return data
|
|
153
135
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
136
|
+
except aiohttp.ClientError as e:
|
|
137
|
+
print(f"Error sending request: {e}")
|
|
138
|
+
except Exception as e:
|
|
139
|
+
print(f"Unexpected error: {e}")
|
|
158
140
|
|
|
159
141
|
|
|
160
|
-
def create_stx(self, descr: str, partners: list):
|
|
142
|
+
async def create_stx(self, descr: str, partners: list):
|
|
161
143
|
url = f"https://{self.network}/api/create_stx"
|
|
162
144
|
|
|
163
145
|
STX = {
|
|
@@ -166,23 +148,20 @@ class Cell:
|
|
|
166
148
|
"cell": self.to_dict()
|
|
167
149
|
}
|
|
168
150
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
url,
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
response.raise_for_status()["stxID"]
|
|
176
|
-
|
|
177
|
-
return response.json()
|
|
151
|
+
async with aiohttp.ClientSession() as session:
|
|
152
|
+
try:
|
|
153
|
+
async with session.post(url, json=STX) as response:
|
|
154
|
+
response.raise_for_status()
|
|
155
|
+
data = await response.json()
|
|
156
|
+
return data["stxID"]
|
|
178
157
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
158
|
+
except aiohttp.ClientError as e:
|
|
159
|
+
print(f"Error sending request: {e}")
|
|
160
|
+
except Exception as e:
|
|
161
|
+
print(f"Unexpected error: {e}")
|
|
183
162
|
|
|
184
163
|
|
|
185
|
-
def delete_stx(self, stxID: str):
|
|
164
|
+
async def delete_stx(self, stxID: str):
|
|
186
165
|
url = f"https://{self.network}/api/delete_stx"
|
|
187
166
|
|
|
188
167
|
STX = {
|
|
@@ -190,213 +169,236 @@ class Cell:
|
|
|
190
169
|
"cell": self.to_dict()
|
|
191
170
|
}
|
|
192
171
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
url,
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
print(f"Response from Neuronum: {response.json()}")
|
|
172
|
+
async with aiohttp.ClientSession() as session:
|
|
173
|
+
try:
|
|
174
|
+
async with session.post(url, json=STX) as response:
|
|
175
|
+
response.raise_for_status()
|
|
176
|
+
data = await response.json()
|
|
177
|
+
print(f"Response from Neuronum: {data}")
|
|
178
|
+
return data
|
|
202
179
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
180
|
+
except aiohttp.ClientError as e:
|
|
181
|
+
print(f"Error sending request: {e}")
|
|
182
|
+
except Exception as e:
|
|
183
|
+
print(f"Unexpected error: {e}")
|
|
207
184
|
|
|
208
185
|
|
|
209
|
-
def list_cells(self):
|
|
186
|
+
async def list_cells(self):
|
|
210
187
|
full_url = f"https://{self.network}/api/list_cells"
|
|
211
|
-
|
|
212
|
-
|
|
188
|
+
|
|
189
|
+
list_cells_payload = {
|
|
213
190
|
"cell": self.to_dict()
|
|
214
191
|
}
|
|
215
192
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
193
|
+
async with aiohttp.ClientSession() as session:
|
|
194
|
+
try:
|
|
195
|
+
async with session.get(full_url, json=list_cells_payload) as response:
|
|
196
|
+
response.raise_for_status()
|
|
197
|
+
data = await response.json()
|
|
198
|
+
return data.get("Cells", [])
|
|
199
|
+
|
|
200
|
+
except aiohttp.ClientError as e:
|
|
201
|
+
print(f"Error sending request: {e}")
|
|
202
|
+
except Exception as e:
|
|
203
|
+
print(f"Unexpected error: {e}")
|
|
224
204
|
|
|
225
205
|
|
|
226
|
-
def list_tx(self):
|
|
206
|
+
async def list_tx(self):
|
|
227
207
|
full_url = f"https://{self.network}/api/list_tx"
|
|
228
|
-
|
|
229
|
-
|
|
208
|
+
|
|
209
|
+
list_tx_payload = {
|
|
230
210
|
"cell": self.to_dict()
|
|
231
211
|
}
|
|
232
212
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
213
|
+
async with aiohttp.ClientSession() as session:
|
|
214
|
+
try:
|
|
215
|
+
async with session.get(full_url, json=list_tx_payload) as response:
|
|
216
|
+
response.raise_for_status()
|
|
217
|
+
data = await response.json()
|
|
218
|
+
return data.get("Transmitters", [])
|
|
219
|
+
|
|
220
|
+
except aiohttp.ClientError as e:
|
|
221
|
+
print(f"Error sending request: {e}")
|
|
222
|
+
except Exception as e:
|
|
223
|
+
print(f"Unexpected error: {e}")
|
|
241
224
|
|
|
242
225
|
|
|
243
|
-
def list_ctx(self):
|
|
226
|
+
async def list_ctx(self):
|
|
244
227
|
full_url = f"https://{self.network}/api/list_ctx"
|
|
245
|
-
|
|
246
|
-
|
|
228
|
+
|
|
229
|
+
list_ctx_payload = {
|
|
247
230
|
"cell": self.to_dict()
|
|
248
231
|
}
|
|
249
232
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
except Exception as e:
|
|
257
|
-
print(f"Unexpected error: {e}")
|
|
233
|
+
async with aiohttp.ClientSession() as session:
|
|
234
|
+
try:
|
|
235
|
+
async with session.get(full_url, json=list_ctx_payload) as response:
|
|
236
|
+
response.raise_for_status()
|
|
237
|
+
data = await response.json()
|
|
238
|
+
return data.get("Circuits", [])
|
|
258
239
|
|
|
240
|
+
except aiohttp.ClientError as e:
|
|
241
|
+
print(f"Error sending request: {e}")
|
|
242
|
+
except Exception as e:
|
|
243
|
+
print(f"Unexpected error: {e}")
|
|
259
244
|
|
|
260
|
-
|
|
245
|
+
|
|
246
|
+
async def list_stx(self):
|
|
261
247
|
full_url = f"https://{self.network}/api/list_stx"
|
|
262
|
-
|
|
263
|
-
|
|
248
|
+
|
|
249
|
+
list_stx_payload = {
|
|
264
250
|
"cell": self.to_dict()
|
|
265
251
|
}
|
|
266
252
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
except Exception as e:
|
|
274
|
-
print(f"Unexpected error: {e}")
|
|
253
|
+
async with aiohttp.ClientSession() as session:
|
|
254
|
+
try:
|
|
255
|
+
async with session.get(full_url, json=list_stx_payload) as response:
|
|
256
|
+
response.raise_for_status()
|
|
257
|
+
data = await response.json()
|
|
258
|
+
return data.get("Streams", [])
|
|
275
259
|
|
|
260
|
+
except aiohttp.ClientError as e:
|
|
261
|
+
print(f"Error sending request: {e}")
|
|
262
|
+
except Exception as e:
|
|
263
|
+
print(f"Unexpected error: {e}")
|
|
276
264
|
|
|
277
|
-
|
|
265
|
+
|
|
266
|
+
async def list_nodes(self):
|
|
278
267
|
full_url = f"https://{self.network}/api/list_nodes"
|
|
279
|
-
|
|
280
|
-
|
|
268
|
+
|
|
269
|
+
list_nodes_payload = {
|
|
281
270
|
"cell": self.to_dict()
|
|
282
271
|
}
|
|
283
272
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
273
|
+
async with aiohttp.ClientSession() as session:
|
|
274
|
+
try:
|
|
275
|
+
async with session.get(full_url, json=list_nodes_payload) as response:
|
|
276
|
+
response.raise_for_status()
|
|
277
|
+
data = await response.json()
|
|
278
|
+
return data.get("Nodes", [])
|
|
279
|
+
|
|
280
|
+
except aiohttp.ClientError as e:
|
|
281
|
+
print(f"Error sending request: {e}")
|
|
282
|
+
except Exception as e:
|
|
283
|
+
print(f"Unexpected error: {e}")
|
|
292
284
|
|
|
293
285
|
|
|
294
|
-
def store(self, label: str, data: dict, ctx: Optional[str] = None):
|
|
295
|
-
if ctx
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
full_url = f"https://{self.network}/api/store"
|
|
299
|
-
|
|
300
|
-
store = {
|
|
286
|
+
async def store(self, label: str, data: dict, ctx: Optional[str] = None):
|
|
287
|
+
full_url = f"https://{self.network}/api/store_in_ctx/{ctx}" if ctx else f"https://{self.network}/api/store"
|
|
288
|
+
|
|
289
|
+
store_payload = {
|
|
301
290
|
"label": label,
|
|
302
291
|
"data": data,
|
|
303
|
-
"cell": self.to_dict()
|
|
292
|
+
"cell": self.to_dict()
|
|
304
293
|
}
|
|
305
294
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
295
|
+
async with aiohttp.ClientSession() as session:
|
|
296
|
+
try:
|
|
297
|
+
async with session.post(full_url, json=store_payload) as response:
|
|
298
|
+
response.raise_for_status()
|
|
299
|
+
data = await response.json()
|
|
300
|
+
print(f"Response from Neuronum: {data}")
|
|
301
|
+
return data
|
|
302
|
+
|
|
303
|
+
except aiohttp.ClientError as e:
|
|
304
|
+
print(f"Error sending request: {e}")
|
|
305
|
+
except Exception as e:
|
|
306
|
+
print(f"Unexpected error: {e}")
|
|
314
307
|
|
|
315
308
|
|
|
316
|
-
def load(self, label: str, ctx: Optional[str] = None):
|
|
317
|
-
if ctx
|
|
318
|
-
full_url = f"https://{self.network}/api/load_from_ctx/{ctx}"
|
|
319
|
-
else:
|
|
320
|
-
full_url = f"https://{self.network}/api/load"
|
|
309
|
+
async def load(self, label: str, ctx: Optional[str] = None):
|
|
310
|
+
full_url = f"https://{self.network}/api/load_from_ctx/{ctx}" if ctx else f"https://{self.network}/api/load"
|
|
321
311
|
|
|
322
|
-
|
|
312
|
+
load_payload = {
|
|
323
313
|
"label": label,
|
|
324
|
-
"cell": self.to_dict()
|
|
314
|
+
"cell": self.to_dict()
|
|
325
315
|
}
|
|
326
316
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
except Exception as e:
|
|
334
|
-
print(f"Unexpected error: {e}")
|
|
317
|
+
async with aiohttp.ClientSession() as session:
|
|
318
|
+
try:
|
|
319
|
+
async with session.post(full_url, json=load_payload) as response:
|
|
320
|
+
response.raise_for_status()
|
|
321
|
+
data = await response.json()
|
|
322
|
+
return data
|
|
335
323
|
|
|
324
|
+
except aiohttp.ClientError as e:
|
|
325
|
+
print(f"Error sending request: {e}")
|
|
326
|
+
except Exception as e:
|
|
327
|
+
print(f"Unexpected error: {e}")
|
|
336
328
|
|
|
337
|
-
def delete(self, label: str, ctx: Optional[str] = None):
|
|
338
|
-
if ctx:
|
|
339
|
-
full_url = f"https://{self.network}/api/delete_from_ctx/{ctx}"
|
|
340
|
-
else:
|
|
341
|
-
full_url = f"https://{self.network}/api/delete"
|
|
342
329
|
|
|
343
|
-
|
|
330
|
+
async def delete(self, label: str, ctx: Optional[str] = None):
|
|
331
|
+
full_url = f"https://{self.network}/api/delete_from_ctx/{ctx}" if ctx else f"https://{self.network}/api/delete"
|
|
332
|
+
|
|
333
|
+
delete_payload = {
|
|
344
334
|
"label": label,
|
|
345
|
-
"cell": self.to_dict()
|
|
335
|
+
"cell": self.to_dict()
|
|
346
336
|
}
|
|
347
337
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
338
|
+
async with aiohttp.ClientSession() as session:
|
|
339
|
+
try:
|
|
340
|
+
async with session.post(full_url, json=delete_payload) as response:
|
|
341
|
+
response.raise_for_status()
|
|
342
|
+
data = await response.json()
|
|
343
|
+
print(f"Response from Neuronum: {data}")
|
|
344
|
+
return data
|
|
345
|
+
|
|
346
|
+
except aiohttp.ClientError as e:
|
|
347
|
+
print(f"Error sending request: {e}")
|
|
348
|
+
except Exception as e:
|
|
349
|
+
print(f"Unexpected error: {e}")
|
|
356
350
|
|
|
357
351
|
|
|
358
|
-
def clear(self, ctx: Optional[str] = None):
|
|
359
|
-
if ctx
|
|
360
|
-
full_url = f"https://{self.network}/api/clear_ctx/{ctx}"
|
|
361
|
-
else:
|
|
362
|
-
full_url = f"https://{self.network}/api/clear"
|
|
352
|
+
async def clear(self, ctx: Optional[str] = None):
|
|
353
|
+
full_url = f"https://{self.network}/api/clear_ctx/{ctx}" if ctx else f"https://{self.network}/api/clear"
|
|
363
354
|
|
|
364
|
-
|
|
365
|
-
"cell": self.to_dict()
|
|
355
|
+
clear_payload = {
|
|
356
|
+
"cell": self.to_dict()
|
|
366
357
|
}
|
|
367
358
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
359
|
+
async with aiohttp.ClientSession() as session:
|
|
360
|
+
try:
|
|
361
|
+
async with session.post(full_url, json=clear_payload) as response:
|
|
362
|
+
response.raise_for_status()
|
|
363
|
+
data = await response.json()
|
|
364
|
+
print(f"Response from Neuronum: {data}")
|
|
365
|
+
return data
|
|
366
|
+
|
|
367
|
+
except aiohttp.ClientError as e:
|
|
368
|
+
print(f"Error sending request: {e}")
|
|
369
|
+
except Exception as e:
|
|
370
|
+
print(f"Unexpected error: {e}")
|
|
376
371
|
|
|
377
372
|
|
|
378
|
-
def stream(self, label: str, data: dict, stx: Optional[str] = None):
|
|
373
|
+
async def stream(self, label: str, data: dict, stx: Optional[str] = None):
|
|
379
374
|
context = ssl.create_default_context()
|
|
380
375
|
context.check_hostname = True
|
|
381
376
|
context.verify_mode = ssl.CERT_REQUIRED
|
|
382
377
|
|
|
383
|
-
raw_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
384
|
-
self.sock = context.wrap_socket(raw_sock, server_hostname=self.network)
|
|
385
|
-
|
|
386
378
|
try:
|
|
387
|
-
|
|
379
|
+
reader, writer = await asyncio.open_connection(self.network, 55555, ssl=context, server_hostname=self.network)
|
|
380
|
+
|
|
381
|
+
credentials = f"{self.host}\n{self.password}\n{self.synapse}\n{stx}\n"
|
|
382
|
+
writer.write(credentials.encode("utf-8"))
|
|
383
|
+
await writer.drain()
|
|
388
384
|
|
|
389
|
-
|
|
390
|
-
|
|
385
|
+
response = await reader.read(1024)
|
|
386
|
+
response_text = response.decode("utf-8")
|
|
387
|
+
|
|
388
|
+
if "Authentication successful" not in response_text:
|
|
389
|
+
print("Authentication failed")
|
|
390
|
+
writer.close()
|
|
391
|
+
await writer.wait_closed()
|
|
391
392
|
return
|
|
392
393
|
|
|
393
|
-
|
|
394
|
+
stream_payload = {
|
|
394
395
|
"label": label,
|
|
395
396
|
"data": data,
|
|
396
397
|
}
|
|
397
398
|
|
|
398
|
-
|
|
399
|
-
|
|
399
|
+
writer.write(json.dumps(stream_payload).encode("utf-8"))
|
|
400
|
+
await writer.drain()
|
|
401
|
+
print(f"Sent: {stream_payload}")
|
|
400
402
|
|
|
401
403
|
except ssl.SSLError as e:
|
|
402
404
|
print(f"SSL error occurred: {e}")
|
|
@@ -405,174 +407,217 @@ class Cell:
|
|
|
405
407
|
print(f"An unexpected error occurred: {e}")
|
|
406
408
|
|
|
407
409
|
finally:
|
|
408
|
-
|
|
410
|
+
writer.close()
|
|
411
|
+
await writer.wait_closed()
|
|
412
|
+
|
|
409
413
|
|
|
410
414
|
|
|
411
|
-
def sync(self, stx: Optional[str] = None) ->
|
|
412
|
-
|
|
415
|
+
async def sync(self, stx: Optional[str] = None) -> AsyncGenerator[str, None]:
|
|
416
|
+
full_url = f"wss://{self.network}/sync/{stx}"
|
|
417
|
+
|
|
418
|
+
auth_payload = {
|
|
413
419
|
"host": self.host,
|
|
414
420
|
"password": self.password,
|
|
415
421
|
"synapse": self.synapse,
|
|
416
422
|
}
|
|
417
423
|
|
|
418
424
|
try:
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
425
|
+
async with websockets.connect(full_url) as ws:
|
|
426
|
+
await ws.send(json.dumps(auth_payload))
|
|
427
|
+
print("Listening to Stream...")
|
|
422
428
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
print("Stream-Synchronization ended!")
|
|
443
|
-
ws.close()
|
|
444
|
-
print("Connection closed. Goodbye!")
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
def sign_contract(self, contractID: str):
|
|
448
|
-
full_url = f"https://{self.network}/api/sign_contract"
|
|
449
|
-
|
|
450
|
-
sign_contract = {
|
|
451
|
-
"contractID": contractID,
|
|
452
|
-
"cell": self.to_dict()
|
|
453
|
-
}
|
|
429
|
+
try:
|
|
430
|
+
while True:
|
|
431
|
+
try:
|
|
432
|
+
raw_operation = await ws.recv()
|
|
433
|
+
operation = json.loads(raw_operation)
|
|
434
|
+
yield operation
|
|
435
|
+
|
|
436
|
+
except asyncio.TimeoutError:
|
|
437
|
+
print("No initial data received. Continuing to listen...")
|
|
438
|
+
continue
|
|
439
|
+
|
|
440
|
+
except asyncio.CancelledError:
|
|
441
|
+
print("Connection closed.")
|
|
442
|
+
|
|
443
|
+
except websockets.exceptions.WebSocketException as e:
|
|
444
|
+
print(f"WebSocket error occurred: {e}")
|
|
445
|
+
|
|
446
|
+
except Exception as e:
|
|
447
|
+
print(f"An unexpected error occurred: {e}")
|
|
454
448
|
|
|
449
|
+
|
|
450
|
+
async def sign_contract(self, contractID: str):
|
|
451
|
+
full_url = f"https://{self.network}/api/sign_contract"
|
|
452
|
+
|
|
453
|
+
sign_contract_payload = {
|
|
454
|
+
"contractID": contractID,
|
|
455
|
+
"cell": self.to_dict()
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
async with aiohttp.ClientSession() as session:
|
|
455
459
|
try:
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
+
async with session.post(full_url, json=sign_contract_payload) as response:
|
|
461
|
+
response.raise_for_status()
|
|
462
|
+
data = await response.json()
|
|
463
|
+
return data.get("token")
|
|
464
|
+
|
|
465
|
+
except aiohttp.ClientError as e:
|
|
460
466
|
print(f"Error sending request: {e}")
|
|
461
467
|
except Exception as e:
|
|
462
468
|
print(f"Unexpected error: {e}")
|
|
463
469
|
|
|
464
470
|
|
|
465
|
-
def validate_token(self, token: str, cp: str, contractID: str):
|
|
471
|
+
async def validate_token(self, token: str, cp: str, contractID: str):
|
|
466
472
|
full_url = f"https://{self.network}/api/validate_token"
|
|
467
|
-
|
|
468
|
-
|
|
473
|
+
|
|
474
|
+
validate_payload = {
|
|
469
475
|
"token": token,
|
|
470
476
|
"cp": cp,
|
|
471
477
|
"contractID": contractID,
|
|
472
|
-
"cell": self.to_dict()
|
|
478
|
+
"cell": self.to_dict()
|
|
473
479
|
}
|
|
474
480
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
except Exception as e:
|
|
482
|
-
print(f"Unexpected error: {e}")
|
|
481
|
+
async with aiohttp.ClientSession() as session:
|
|
482
|
+
try:
|
|
483
|
+
async with session.post(full_url, json=validate_payload) as response:
|
|
484
|
+
response.raise_for_status()
|
|
485
|
+
data = await response.json()
|
|
486
|
+
return data.get("validity")
|
|
483
487
|
|
|
488
|
+
except aiohttp.ClientError as e:
|
|
489
|
+
print(f"Error sending request: {e}")
|
|
490
|
+
except Exception as e:
|
|
491
|
+
print(f"Unexpected error: {e}")
|
|
484
492
|
|
|
485
|
-
|
|
493
|
+
|
|
494
|
+
async def request_token(self, cp: str, contractID: str):
|
|
486
495
|
full_url = f"https://{self.network}/api/request_token"
|
|
487
|
-
|
|
488
|
-
|
|
496
|
+
|
|
497
|
+
request_token_payload = {
|
|
489
498
|
"cp": cp,
|
|
490
499
|
"contractID": contractID,
|
|
491
|
-
"cell": self.to_dict()
|
|
500
|
+
"cell": self.to_dict()
|
|
492
501
|
}
|
|
493
502
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
503
|
+
async with aiohttp.ClientSession() as session:
|
|
504
|
+
try:
|
|
505
|
+
async with session.post(full_url, json=request_token_payload) as response:
|
|
506
|
+
response.raise_for_status()
|
|
507
|
+
data = await response.json()
|
|
508
|
+
print(f"Response from Neuronum: {data}")
|
|
509
|
+
return data
|
|
510
|
+
|
|
511
|
+
except aiohttp.ClientError as e:
|
|
512
|
+
print(f"Error sending request: {e}")
|
|
513
|
+
except Exception as e:
|
|
514
|
+
print(f"Unexpected error: {e}")
|
|
502
515
|
|
|
503
516
|
|
|
504
|
-
def present_token(self, token: str, cp: str, contractID: str):
|
|
517
|
+
async def present_token(self, token: str, cp: str, contractID: str):
|
|
505
518
|
full_url = f"https://{self.network}/api/present_token"
|
|
506
|
-
|
|
507
|
-
|
|
519
|
+
|
|
520
|
+
present_token_payload = {
|
|
508
521
|
"token": token,
|
|
509
522
|
"cp": cp,
|
|
510
523
|
"contractID": contractID,
|
|
511
|
-
"cell": self.to_dict()
|
|
524
|
+
"cell": self.to_dict()
|
|
512
525
|
}
|
|
513
526
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
527
|
+
async with aiohttp.ClientSession() as session:
|
|
528
|
+
try:
|
|
529
|
+
async with session.post(full_url, json=present_token_payload) as response:
|
|
530
|
+
response.raise_for_status()
|
|
531
|
+
data = await response.json()
|
|
532
|
+
print(f"Response from Neuronum: {data}")
|
|
533
|
+
return data
|
|
534
|
+
|
|
535
|
+
except aiohttp.ClientError as e:
|
|
536
|
+
print(f"Error sending request: {e}")
|
|
537
|
+
except Exception as e:
|
|
538
|
+
print(f"Unexpected error: {e}")
|
|
522
539
|
|
|
523
540
|
|
|
524
|
-
def create_contract(self, descr: str, details: dict, partners: list):
|
|
541
|
+
async def create_contract(self, descr: str, details: dict, partners: list):
|
|
525
542
|
full_url = f"https://{self.network}/api/create_contract"
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
"cell": self.to_dict(),
|
|
543
|
+
|
|
544
|
+
create_contract_payload = {
|
|
545
|
+
"cell": self.to_dict(),
|
|
529
546
|
"descr": descr,
|
|
530
547
|
"details": details,
|
|
531
548
|
"partners": partners
|
|
532
549
|
}
|
|
533
|
-
try:
|
|
534
|
-
response = requests.post(full_url, json=create_contract)
|
|
535
|
-
response.raise_for_status()
|
|
536
|
-
return response.json()["contractID"]
|
|
537
|
-
except requests.exceptions.RequestException as e:
|
|
538
|
-
print(f"Error sending request: {e}")
|
|
539
|
-
except Exception as e:
|
|
540
|
-
print(f"Unexpected error: {e}")
|
|
541
550
|
|
|
551
|
+
async with aiohttp.ClientSession() as session:
|
|
552
|
+
try:
|
|
553
|
+
async with session.post(full_url, json=create_contract_payload) as response:
|
|
554
|
+
response.raise_for_status()
|
|
555
|
+
data = await response.json()
|
|
556
|
+
return data.get("contractID")
|
|
542
557
|
|
|
543
|
-
|
|
558
|
+
except aiohttp.ClientError as e:
|
|
559
|
+
print(f"Error sending request: {e}")
|
|
560
|
+
except Exception as e:
|
|
561
|
+
print(f"Unexpected error: {e}")
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
async def delete_contract(self, contractID: str):
|
|
544
565
|
full_url = f"https://{self.network}/api/delete_contract"
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
"cell": self.to_dict(),
|
|
566
|
+
|
|
567
|
+
request_payload = {
|
|
568
|
+
"cell": self.to_dict(),
|
|
548
569
|
"contractID": contractID
|
|
549
570
|
}
|
|
550
571
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
572
|
+
async with aiohttp.ClientSession() as session:
|
|
573
|
+
try:
|
|
574
|
+
async with session.post(full_url, json=request_payload) as response:
|
|
575
|
+
response.raise_for_status()
|
|
576
|
+
data = await response.json()
|
|
577
|
+
print(f"Response from Neuronum: {data}")
|
|
578
|
+
return data
|
|
579
|
+
|
|
580
|
+
except aiohttp.ClientError as e:
|
|
581
|
+
print(f"Error sending request: {e}")
|
|
582
|
+
except Exception as e:
|
|
583
|
+
print(f"Unexpected error: {e}")
|
|
559
584
|
|
|
560
585
|
|
|
561
|
-
def list_contracts(self):
|
|
586
|
+
async def list_contracts(self):
|
|
562
587
|
full_url = f"https://{self.network}/api/list_contracts"
|
|
563
|
-
|
|
564
|
-
|
|
588
|
+
|
|
589
|
+
list_contracts_payload = {
|
|
565
590
|
"cell": self.to_dict()
|
|
566
591
|
}
|
|
567
592
|
|
|
593
|
+
async with aiohttp.ClientSession() as session:
|
|
594
|
+
try:
|
|
595
|
+
async with session.get(full_url, json=list_contracts_payload) as response:
|
|
596
|
+
response.raise_for_status()
|
|
597
|
+
data = await response.json()
|
|
598
|
+
return data.get("Contracts", [])
|
|
599
|
+
|
|
600
|
+
except aiohttp.ClientError as e:
|
|
601
|
+
print(f"Error sending request: {e}")
|
|
602
|
+
except Exception as e:
|
|
603
|
+
print(f"Unexpected error: {e}")
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
def device_found(self, device, advertisement_data):
|
|
607
|
+
if device.name and (device.name.endswith("::cell") or device.name.endswith("::node")):
|
|
608
|
+
asyncio.create_task(self.queue.put(f"{device.name} - {device.address}"))
|
|
609
|
+
|
|
610
|
+
async def scan(self):
|
|
611
|
+
print("Scanning for Neuronum Cells & Nodes")
|
|
612
|
+
|
|
613
|
+
scanner = BleakScanner(self.device_found)
|
|
614
|
+
await scanner.start()
|
|
615
|
+
|
|
568
616
|
try:
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
print(f"Error sending request: {e}")
|
|
574
|
-
except Exception as e:
|
|
575
|
-
print(f"Unexpected error: {e}")
|
|
617
|
+
while True:
|
|
618
|
+
yield await self.queue.get()
|
|
619
|
+
except asyncio.CancelledError:
|
|
620
|
+
await scanner.stop()
|
|
576
621
|
|
|
577
622
|
|
|
578
623
|
__all__ = ['Cell']
|