macdev
quadrismegistus 4 years ago
parent 7ea1fdf034
commit a1a94eea0d

@ -115,7 +115,6 @@ class Api(object):
if type(key_or_keys) in {list,tuple,dict}: if type(key_or_keys) in {list,tuple,dict}:
keys = key_or_keys keys = key_or_keys
res = []
res = await asyncio.gather(*[node.get(key) for key in keys]) res = await asyncio.gather(*[node.get(key) for key in keys])
#log('RES?',res) #log('RES?',res)
else: else:
@ -125,31 +124,15 @@ class Api(object):
#node.stop() #node.stop()
return res return res
# return asyncio.run(_get())
#res = await asyncio.create_task(_get())
#self.log('RES =',res)
return await _get() return await _get()
# return loop.create_task(_get())
async def get_json(self,key_or_keys):
res = await self.get(key_or_keys)
self.log('GET_JSON',res)
if type(res)==list:
# self.log('is a list!',json.loads(res[0]))
return [None if x is None else json.loads(x) for x in res]
else:
#log('RES!!!',res)
return None if res is None else json.loads(res)
async def set(self,key_or_keys,value_or_values): async def set(self,key_or_keys,value_or_values):
async def _go(): async def _set():
# self.log('async _set()',self.node) # self.log('async _set()',self.node)
# node=self.node # node=self.node
#node=await _getdb(self) #node=await _getdb(self)
node=await self.node node=await self.node
if type(key_or_keys) in {list,tuple,dict}: if type(key_or_keys) in {list,tuple,dict}:
keys = key_or_keys keys = key_or_keys
values = value_or_values values = value_or_values
@ -159,42 +142,29 @@ class Api(object):
else: else:
key = key_or_keys key = key_or_keys
value = value_or_values value = value_or_values
res = await node.set(key,value) #'this is a test') res = await node.set(key,value)
#node.stop() #node.stop()
return res return res
# loop=asyncio.get_event_loop() return await _set()
# loop.create_task(_set())
# async def _set(key,value): async def get_json(self,key_or_keys):
# import asyncio res = await self.get(key_or_keys)
# from kademlia.network import Server self.log('GET_JSON',res)
if type(res)==list:
# # Create a node and start listening on port 5678 # self.log('is a list!',json.loads(res[0]))
# node = Server() return [None if x is None else json.loads(x) for x in res]
# await node.listen(5678) else:
#log('RES!!!',res)
# # Bootstrap the node by connecting to other known nodes, in this case return None if res is None else json.loads(res)
# # replace 123.123.123.123 with the IP of another node and optionally
# # give as many ip/port combos as you can for other nodes.
# await node.bootstrap(NODES_PRIME)
# # set a value for the key "my-key" on the network
# await node.set(key, value)
# # get the value associated with "my-key" from the network
# result = await node.get(key)
# print(result)
# return result
return _go()
async def set_json(self,key,value): async def set_json(self,key,value):
value_json = jsonify(value) value_json = jsonify(value)
# self.log('OH NO!',sys.getsizeof(value_json)) # self.log('OH NO!',sys.getsizeof(value_json))
return self.set(key,value_json) return await self.set(key,value_json)
def has(self,key): def has(self,key):
return self.get(key) is not None return self.get(key) is not None
@ -345,11 +315,11 @@ class Api(object):
self.log('Api.post() got data back from set_json():',res) self.log('Api.post() got data back from set_json():',res)
# ## add to channels # ## add to channels
self.append_json('/posts/channel/earth', post_id) await self.append_json('/posts/channel/earth', post_id)
# ## add to user # ## add to user
un=data.get('author') un=data.get('author')
if un: self.append_json('/posts/author/'+un, post_id) if un: await self.append_json('/posts/author/'+un, post_id)
if res: if res:
return {'success':'Posted! %s' % post_id, 'post_id':post_id} return {'success':'Posted! %s' % post_id, 'post_id':post_id}

Loading…
Cancel
Save