-
Notifications
You must be signed in to change notification settings - Fork 200
/
block.py
84 lines (62 loc) · 2 KB
/
block.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from . import base
from .. import multipart
class Section(base.SectionBase):
"""
Functions for interacting with raw IPFS blocks.
"""
def get(self, cid, **kwargs):
r"""Returns the raw contents of a block.
.. code-block:: python
>>> client.block.get('QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D')
b'\x121\n"\x12 \xdaW>\x14\xe5\xc1\xf6\xe4\x92\xd1 … \n\x02\x08\x01'
Parameters
----------
cid : Union[str, cid.CIDv0, cid.CIDv1]
The CID of an existing block to get
Returns
-------
bytes
Contents of the requested block
"""
args = (str(cid),)
return self._client.request('/block/get', args, **kwargs)
@base.returns_single_item
def put(self, file, **kwargs):
"""Stores the contents of the given file object as an IPFS block.
.. code-block:: python
>>> client.block.put(io.BytesIO(b'Mary had a little lamb'))
{'Key': 'QmeV6C6XVt1wf7V7as7Yak3mxPma8jzpqyhtRtCvpKcfBb',
'Size': 22}
Parameters
----------
file : Union[str, bytes, os.PathLike, io.IOBase, int]
The data to be stored as an IPFS block
Returns
-------
dict
Information about the new block
See :meth:`~ipfshttpclient.Client.block.stat`
"""
body, headers = multipart.stream_files(file, self.chunk_size)
return self._client.request('/block/put', decoder='json', data=body,
headers=headers, **kwargs)
@base.returns_single_item
def stat(self, cid, **kwargs):
"""Returns a dict with the size of the block with the given hash.
.. code-block:: python
>>> client.block.stat('QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D')
{'Key': 'QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D',
'Size': 258}
Parameters
----------
cid : Union[str, cid.CIDv0, cid.CIDv1]
The CID of an existing block to stat
Returns
-------
dict
Information about the requested block
"""
args = (str(cid),)
return self._client.request('/block/stat', args, decoder='json', **kwargs)