|
10 | 10 | import os
|
11 | 11 | import warnings
|
12 | 12 |
|
| 13 | +import ipfshttpclient |
| 14 | + |
13 | 15 | DEFAULT_HOST = str(os.environ.get("PY_IPFSAPI_DEFAULT_HOST", 'localhost'))
|
14 | 16 | DEFAULT_PORT = int(os.environ.get("PY_IPFSAPI_DEFAULT_PORT", 5001))
|
15 | 17 | DEFAULT_BASE = str(os.environ.get("PY_IPFSAPI_DEFAULT_BASE", 'api/v0'))
|
16 | 18 |
|
17 | 19 | VERSION_MINIMUM = "0.4.3"
|
18 | 20 | VERSION_MAXIMUM = "0.5.0"
|
19 | 21 |
|
20 |
| -from . import files |
21 |
| -from . import graph |
22 |
| -from . import crypto |
23 |
| -from . import network |
24 |
| -from . import node |
| 22 | +from .. import exceptions, encoding |
25 | 23 |
|
26 |
| -import ipfsapi.multipart |
27 |
| -from .. import utils, exceptions, encoding |
| 24 | +from . import base |
28 | 25 |
|
29 | 26 |
|
30 | 27 | def assert_version(version, minimum=VERSION_MINIMUM, maximum=VERSION_MAXIMUM):
|
@@ -54,7 +51,7 @@ def assert_version(version, minimum=VERSION_MINIMUM, maximum=VERSION_MAXIMUM):
|
54 | 51 |
|
55 | 52 |
|
56 | 53 | def connect(host=DEFAULT_HOST, port=DEFAULT_PORT, base=DEFAULT_BASE,
|
57 |
| - chunk_size=ipfsapi.multipart.default_chunk_size, **defaults): |
| 54 | + chunk_size=4096, **defaults): |
58 | 55 | """Create a new :class:`~ipfsapi.Client` instance and connect to the
|
59 | 56 | daemon to validate that its version is supported.
|
60 | 57 |
|
@@ -84,139 +81,89 @@ def connect(host=DEFAULT_HOST, port=DEFAULT_PORT, base=DEFAULT_BASE,
|
84 | 81 | return client
|
85 | 82 |
|
86 | 83 |
|
87 |
| -class Client( |
88 |
| - files.FilesBase, |
89 |
| - graph.GraphBase, |
90 |
| - crypto.CryptoBase, |
91 |
| - network.NetworkBase, |
92 |
| - node.NodeBase |
93 |
| -): |
94 |
| - def dns(self, domain_name, recursive=False, **kwargs): |
95 |
| - """Resolves DNS links to the referenced object. |
96 |
| -
|
97 |
| - Multihashes are hard to remember, but domain names are usually easy to |
98 |
| - remember. To create memorable aliases for multihashes, DNS TXT records |
99 |
| - can point to other DNS links, IPFS objects, IPNS keys, etc. |
100 |
| - This command resolves those links to the referenced object. |
101 |
| -
|
102 |
| - For example, with this DNS TXT record:: |
103 |
| -
|
104 |
| - >>> import dns.resolver |
105 |
| - >>> a = dns.resolver.query("ipfs.io", "TXT") |
106 |
| - >>> a.response.answer[0].items[0].to_text() |
107 |
| - '"dnslink=/ipfs/QmTzQ1JRkWErjk39mryYw2WVaphAZNAREyMchXzYQ7c15n"' |
108 |
| -
|
109 |
| - The resolver will give:: |
110 |
| -
|
111 |
| - >>> c.dns("ipfs.io") |
112 |
| - {'Path': '/ipfs/QmTzQ1JRkWErjk39mryYw2WVaphAZNAREyMchXzYQ7c15n'} |
113 |
| -
|
114 |
| - Parameters |
115 |
| - ---------- |
116 |
| - domain_name : str |
117 |
| - The domain-name name to resolve |
118 |
| - recursive : bool |
119 |
| - Resolve until the name is not a DNS link |
120 |
| -
|
121 |
| - Returns |
122 |
| - ------- |
123 |
| - dict : Resource were a DNS entry points to |
124 |
| - """ |
125 |
| - kwargs.setdefault("opts", {"recursive": recursive}) |
126 |
| - |
127 |
| - args = (domain_name,) |
128 |
| - return self._client.request('/dns', args, decoder='json', **kwargs) |
129 |
| - |
130 |
| - ########### |
131 |
| - # HELPERS # |
132 |
| - ########### |
133 |
| - |
134 |
| - @utils.return_field('Hash') |
135 |
| - def add_bytes(self, data, **kwargs): |
136 |
| - """Adds a set of bytes as a file to IPFS. |
137 |
| -
|
138 |
| - .. code-block:: python |
139 |
| -
|
140 |
| - >>> c.add_bytes(b"Mary had a little lamb") |
141 |
| - 'QmZfF6C9j4VtoCsTp4KSrhYH47QMd3DNXVZBKaxJdhaPab' |
142 |
| -
|
143 |
| - Also accepts and will stream generator objects. |
144 |
| -
|
145 |
| - Parameters |
146 |
| - ---------- |
147 |
| - data : bytes |
148 |
| - Content to be added as a file |
149 |
| -
|
150 |
| - Returns |
151 |
| - ------- |
152 |
| - str : Hash of the added IPFS object |
153 |
| - """ |
154 |
| - body, headers = ipfsapi.multipart.stream_bytes(data, self.chunk_size) |
155 |
| - return self._client.request('/add', decoder='json', |
156 |
| - data=body, headers=headers, **kwargs) |
157 |
| - |
158 |
| - @utils.return_field('Hash') |
159 |
| - def add_str(self, string, **kwargs): |
160 |
| - """Adds a Python string as a file to IPFS. |
161 |
| -
|
162 |
| - .. code-block:: python |
163 |
| -
|
164 |
| - >>> c.add_str(u"Mary had a little lamb") |
165 |
| - 'QmZfF6C9j4VtoCsTp4KSrhYH47QMd3DNXVZBKaxJdhaPab' |
166 |
| -
|
167 |
| - Also accepts and will stream generator objects. |
168 |
| -
|
169 |
| - Parameters |
170 |
| - ---------- |
171 |
| - string : str |
172 |
| - Content to be added as a file |
173 |
| -
|
174 |
| - Returns |
175 |
| - ------- |
176 |
| - str : Hash of the added IPFS object |
177 |
| - """ |
178 |
| - body, headers = ipfsapi.multipart.stream_text(string, self.chunk_size) |
179 |
| - return self._client.request('/add', decoder='json', |
180 |
| - data=body, headers=headers, **kwargs) |
181 |
| - |
182 |
| - def add_json(self, json_obj, **kwargs): |
183 |
| - """Adds a json-serializable Python dict as a json file to IPFS. |
184 |
| -
|
185 |
| - .. code-block:: python |
186 |
| -
|
187 |
| - >>> c.add_json({'one': 1, 'two': 2, 'three': 3}) |
188 |
| - 'QmVz9g7m5u3oHiNKHj2CJX1dbG1gtismRS3g9NaPBBLbob' |
189 |
| -
|
190 |
| - Parameters |
191 |
| - ---------- |
192 |
| - json_obj : dict |
193 |
| - A json-serializable Python dictionary |
194 |
| -
|
195 |
| - Returns |
196 |
| - ------- |
197 |
| - str : Hash of the added IPFS object |
198 |
| - """ |
199 |
| - return self.add_bytes(encoding.Json().encode(json_obj), **kwargs) |
200 |
| - |
201 |
| - def get_json(self, multihash, **kwargs): |
202 |
| - """Loads a json object from IPFS. |
203 |
| -
|
204 |
| - .. code-block:: python |
205 |
| -
|
206 |
| - >>> c.get_json('QmVz9g7m5u3oHiNKHj2CJX1dbG1gtismRS3g9NaPBBLbob') |
207 |
| - {'one': 1, 'two': 2, 'three': 3} |
208 |
| -
|
209 |
| - Parameters |
210 |
| - ---------- |
211 |
| - multihash : str |
212 |
| - Multihash of the IPFS object to load |
213 |
| -
|
214 |
| - Returns |
215 |
| - ------- |
216 |
| - object : Deserialized IPFS JSON object value |
217 |
| - """ |
218 |
| - return self.cat(multihash, decoder='json', **kwargs) |
219 |
| - |
| 84 | +class Client(ipfshttpclient.Client): |
| 85 | + # Aliases for previous method names |
| 86 | + key_gen = base.DeprecatedMethodProperty("key", "gen") |
| 87 | + key_list = base.DeprecatedMethodProperty("key", "list") |
| 88 | + key_rename = base.DeprecatedMethodProperty("key", "rename") |
| 89 | + key_rm = base.DeprecatedMethodProperty("key", "rm") |
| 90 | + |
| 91 | + block_get = base.DeprecatedMethodProperty("block", "get") |
| 92 | + block_put = base.DeprecatedMethodProperty("block", "put") |
| 93 | + block_stat = base.DeprecatedMethodProperty("block", "stat") |
| 94 | + |
| 95 | + files_cp = base.DeprecatedMethodProperty("files", "cp") |
| 96 | + files_ls = base.DeprecatedMethodProperty("files", "ls") |
| 97 | + files_mkdir = base.DeprecatedMethodProperty("files", "mkdir") |
| 98 | + files_stat = base.DeprecatedMethodProperty("files", "stat") |
| 99 | + files_rm = base.DeprecatedMethodProperty("files", "rm") |
| 100 | + files_read = base.DeprecatedMethodProperty("files", "read") |
| 101 | + files_write = base.DeprecatedMethodProperty("files", "write") |
| 102 | + files_mv = base.DeprecatedMethodProperty("files", "mv") |
| 103 | + |
| 104 | + object_data = base.DeprecatedMethodProperty("object", "data") |
| 105 | + object_get = base.DeprecatedMethodProperty("object", "get") |
| 106 | + object_links = base.DeprecatedMethodProperty("object", "links") |
| 107 | + object_new = base.DeprecatedMethodProperty("object", "new") |
| 108 | + object_put = base.DeprecatedMethodProperty("object", "put") |
| 109 | + object_stat = base.DeprecatedMethodProperty("object", "stat") |
| 110 | + object_patch_add_link = base.DeprecatedMethodProperty("object", "patch", "add_link") |
| 111 | + object_patch_append_data = base.DeprecatedMethodProperty("object", "patch", "append_data") |
| 112 | + object_patch_rm_link = base.DeprecatedMethodProperty("object", "patch", "rm_link") |
| 113 | + object_patch_set_data = base.DeprecatedMethodProperty("object", "patch", "set_data") |
| 114 | + |
| 115 | + pin_add = base.DeprecatedMethodProperty("pin", "add") |
| 116 | + pin_ls = base.DeprecatedMethodProperty("pin", "ls") |
| 117 | + pin_rm = base.DeprecatedMethodProperty("pin", "rm") |
| 118 | + pin_update = base.DeprecatedMethodProperty("pin", "update") |
| 119 | + pin_verify = base.DeprecatedMethodProperty("pin", "verify") |
| 120 | + |
| 121 | + refs = base.DeprecatedMethodProperty("unstable", "refs") |
| 122 | + refs_local = base.DeprecatedMethodProperty("unstable", "refs", "local") |
| 123 | + |
| 124 | + bootstrap_add = base.DeprecatedMethodProperty("bootstrap", "add") |
| 125 | + bootstrap_list = base.DeprecatedMethodProperty("bootstrap", "list") |
| 126 | + bootstrap_rm = base.DeprecatedMethodProperty("bootstrap", "rm") |
| 127 | + |
| 128 | + bitswap_stat = base.DeprecatedMethodProperty("bitswap", "stat") |
| 129 | + bitswap_wantlist = base.DeprecatedMethodProperty("bitswap", "wantlist") |
| 130 | + |
| 131 | + dht_findpeer = base.DeprecatedMethodProperty("dht", "findpeer") |
| 132 | + dht_findprovs = base.DeprecatedMethodProperty("dht", "findproves") |
| 133 | + dht_get = base.DeprecatedMethodProperty("dht", "get") |
| 134 | + dht_put = base.DeprecatedMethodProperty("dht", "put") |
| 135 | + dht_query = base.DeprecatedMethodProperty("dht", "query") |
| 136 | + |
| 137 | + pubsub_ls = base.DeprecatedMethodProperty("pubsub", "ls") |
| 138 | + pubsub_peers = base.DeprecatedMethodProperty("pubsub", "peers") |
| 139 | + pubsub_pub = base.DeprecatedMethodProperty("pubsub", "publish") |
| 140 | + pubsub_sub = base.DeprecatedMethodProperty("pubsub", "subscribe") |
| 141 | + |
| 142 | + swarm_addrs = base.DeprecatedMethodProperty("swarm", "addrs") |
| 143 | + swarm_connect = base.DeprecatedMethodProperty("swarm", "connect") |
| 144 | + swarm_disconnect = base.DeprecatedMethodProperty("swarm", "disconnect") |
| 145 | + swarm_peers = base.DeprecatedMethodProperty("swarm", "peers") |
| 146 | + swarm_filters_add = base.DeprecatedMethodProperty("swarm", "filters", "add") |
| 147 | + swarm_filters_rm = base.DeprecatedMethodProperty("swarm", "filters", "rm") |
| 148 | + |
| 149 | + name_publish = base.DeprecatedMethodProperty("name", "publish") |
| 150 | + name_resolve = base.DeprecatedMethodProperty("name", "resolve") |
| 151 | + |
| 152 | + repo_gc = base.DeprecatedMethodProperty("repo", "gc") |
| 153 | + repo_stat = base.DeprecatedMethodProperty("repo", "stat") |
| 154 | + |
| 155 | + config = base.DeprecatedMethodProperty("config", "set") |
| 156 | + config_show = base.DeprecatedMethodProperty("config", "get") |
| 157 | + config_replace = base.DeprecatedMethodProperty("config", "replace") |
| 158 | + |
| 159 | + log_level = base.DeprecatedMethodProperty("unstable", "log", "level") |
| 160 | + log_ls = base.DeprecatedMethodProperty("unstable", "log", "ls") |
| 161 | + log_tail = base.DeprecatedMethodProperty("unstable", "log", "tail") |
| 162 | + |
| 163 | + shutdown = base.DeprecatedMethodProperty("stop") |
| 164 | + |
| 165 | + |
| 166 | + # Dropped utility methods |
220 | 167 | def add_pyobj(self, py_obj, **kwargs):
|
221 | 168 | """Adds a picklable Python object as a file to IPFS.
|
222 | 169 |
|
@@ -280,4 +227,4 @@ def get_pyobj(self, multihash, **kwargs):
|
280 | 227 | """
|
281 | 228 | warnings.warn("Using `*_pyobj` on untrusted data is a security risk",
|
282 | 229 | DeprecationWarning)
|
283 |
| - return self.cat(multihash, decoder='pickle', **kwargs) |
| 230 | + return encoding.Pickle().parse(self.cat(multihash, **kwargs)) |
0 commit comments