Skip to content

Commit a4ccb67

Browse files
authored
Merge pull request #55 from fredthomsen/addBitSwapCmd
Add bitswap commands
2 parents db6afe1 + 4860500 commit a4ccb67

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

ipfsApi/client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class Client(object):
2828
block_stat -- returns a dict with the size of the block with the given hash
2929
block_get -- returns the raw contents of a block
3030
block_put -- stores input as an IPFS block
31+
bitswap_wantlist -- show blocks currently on the wantlist
32+
bitswap_stat -- show some diagnostic information on the bitswap agent
33+
bitswap_unwant -- remove a given block from wantlist
3134
object_data -- returns the raw bytes in an IPFS object
3235
object_new -- creates a new object from an ipfs template
3336
object_links -- returns the links pointed to by the specified object
@@ -121,6 +124,10 @@ def __init__(self, host=None, port=None,
121124
self._block_get = ArgCommand('/block/get')
122125
self._block_put = FileCommand('/block/put')
123126
self._object_new = ArgCommand('/object/new')
127+
self._bitswap_wantlist = ArgCommand('/bitswap/wantlist')
128+
self._bitswap_stat = Command('/bitswap/stat')
129+
self._bitswap_unwant = ArgCommand('/bitswap/unwant')
130+
124131
self._object_data = ArgCommand('/object/data')
125132
self._object_links = ArgCommand('/object/links')
126133
self._object_get = ArgCommand('/object/get')
@@ -318,6 +325,30 @@ def block_put(self, file, **kwargs):
318325
"""
319326
return self._block_put.request(self._client, (), file, **kwargs)
320327

328+
def bitswap_wantlist(self, peer=None, **kwargs):
329+
"""
330+
Show blocks currently on the wantlist.
331+
332+
:param peer: Peer to show wantlist for.
333+
"""
334+
return self._bitswap_wantlist.request(self._client, peer, **kwargs)
335+
336+
def bitswap_stat(self, **kwargs):
337+
"""
338+
Show some diagnostic information on the bitswap agent.
339+
"""
340+
341+
return self._bitswap_stat.request(self._client, **kwargs)
342+
343+
def bitswap_unwant(self, key, **kwargs):
344+
"""
345+
Remove a given block from wantlist.
346+
347+
:param key: Key to remove from wantlist.
348+
"""
349+
350+
return self._bitswap_unwant.request(self._client, key, **kwargs)
351+
321352
def object_data(self, multihash, **kwargs):
322353
r"""Returns the raw bytes in an IPFS object.
323354

test/functional/tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,5 +536,28 @@ def test_object_patch_set_data(self):
536536
self.assertEqual(result,
537537
{'Hash': 'QmV4QR7MCBj5VTi6ddHmXPyjWGzbaKEtX2mx7axA5PA13G'})
538538

539+
@skipIfOffline()
540+
class IpfsApiBitswapTest(unittest.TestCase):
541+
542+
def setUp(self):
543+
self.api = ipfsApi.Client()
544+
545+
def test_bitswap_wantlist(self):
546+
result = self.api.bitswap_wantlist(peer='QmdkJZUWnVkEc6yfptVu4LWY8nHkEnGwsxqQ233QSGj8UP')
547+
self.assertTrue(result and type(result) is dict and 'Keys' in result)
548+
549+
def test_bitswap_stat(self):
550+
result = self.api.bitswap_stat()
551+
self.assertTrue(result and type(result) is dict and 'Wantlist' in result)
552+
553+
def test_bitswap_unwant(self):
554+
"""
555+
Cannot ensure what is present in the wantlist prior to execution, so just ensure
556+
something comes back.
557+
"""
558+
559+
result = self.api.bitswap_unwant(key='QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V')
560+
self.assertTrue(result is not None)
561+
539562
if __name__ == "__main__":
540563
unittest.main()

0 commit comments

Comments
 (0)