neuronum 2.0.8__py3-none-any.whl → 3.0.1__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.

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