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.

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,22 @@ 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):
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
- 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()}")
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
- except requests.exceptions.RequestException as e:
106
- print(f"Error sending request: {e}")
107
- except Exception as e:
108
- print(f"Unexpected error: {e}")
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
- try:
121
- response = requests.post(
122
- url,
123
- json=CTX,
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
- except requests.exceptions.RequestException as e:
131
- print(f"Error sending request: {e}")
132
- except Exception as e:
133
- print(f"Unexpected error: {e}")
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
- 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()}")
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
- except requests.exceptions.RequestException as e:
155
- print(f"Error sending request: {e}")
156
- except Exception as e:
157
- print(f"Unexpected error: {e}")
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
- try:
170
- response = requests.post(
171
- url,
172
- json=STX,
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
- except requests.exceptions.RequestException as e:
180
- print(f"Error sending request: {e}")
181
- except Exception as e:
182
- print(f"Unexpected error: {e}")
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
- 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()}")
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
- except requests.exceptions.RequestException as e:
204
- print(f"Error sending request: {e}")
205
- except Exception as e:
206
- print(f"Unexpected error: {e}")
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
- list_cells = {
188
+
189
+ list_cells_payload = {
213
190
  "cell": self.to_dict()
214
191
  }
215
192
 
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}")
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
- list_tx = {
208
+
209
+ list_tx_payload = {
230
210
  "cell": self.to_dict()
231
211
  }
232
212
 
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}")
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
- list_ctx = {
228
+
229
+ list_ctx_payload = {
247
230
  "cell": self.to_dict()
248
231
  }
249
232
 
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}")
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
- def list_stx(self):
245
+
246
+ async def list_stx(self):
261
247
  full_url = f"https://{self.network}/api/list_stx"
262
-
263
- list_stx = {
248
+
249
+ list_stx_payload = {
264
250
  "cell": self.to_dict()
265
251
  }
266
252
 
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}")
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
- def list_nodes(self):
265
+
266
+ async def list_nodes(self):
278
267
  full_url = f"https://{self.network}/api/list_nodes"
279
-
280
- list_nodes = {
268
+
269
+ list_nodes_payload = {
281
270
  "cell": self.to_dict()
282
271
  }
283
272
 
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}")
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
- 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 = {
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
- 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}")
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
- load = {
312
+ load_payload = {
323
313
  "label": label,
324
- "cell": self.to_dict()
314
+ "cell": self.to_dict()
325
315
  }
326
316
 
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}")
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
- delete = {
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
- 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}")
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
- clear = {
365
- "cell": self.to_dict()
355
+ clear_payload = {
356
+ "cell": self.to_dict()
366
357
  }
367
358
 
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}")
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
- self.sock.connect((self.network, 55555))
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
- if not self.authenticate(stx):
390
- print("Authentication failed. Cannot stream.")
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
- stream = {
394
+ stream_payload = {
394
395
  "label": label,
395
396
  "data": data,
396
397
  }
397
398
 
398
- self.sock.sendall(json.dumps(stream).encode('utf-8'))
399
- print(f"Sent: {stream}")
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
- self.sock.close()
410
+ writer.close()
411
+ await writer.wait_closed()
412
+
409
413
 
410
414
 
411
- def sync(self, stx: Optional[str] = None) -> Generator[str, None, None]:
412
- auth = {
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
- ws = create_connection(f"wss://{self.network}/sync/{stx}")
420
- ws.send(json.dumps(auth))
421
- print("Listening to Stream...")
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
- 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
- }
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
- 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:
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
- validate = {
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
- 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}")
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
- def request_token(self, cp: str, contractID: str):
493
+
494
+ async def request_token(self, cp: str, contractID: str):
486
495
  full_url = f"https://{self.network}/api/request_token"
487
-
488
- request_token = {
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
- 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}")
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
- present_token = {
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
- 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}")
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
- create_contract = {
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
- def delete_contract(self, contractID: str):
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
- request = {
547
- "cell": self.to_dict(),
566
+
567
+ request_payload = {
568
+ "cell": self.to_dict(),
548
569
  "contractID": contractID
549
570
  }
550
571
 
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}")
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
- list_contracts = {
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
- 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}")
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']