Skip to content

Commit 14ae8e6

Browse files
committed
Make library a thin wrapper around the newer ipfshttpclient library
1 parent 15686cf commit 14ae8e6

12 files changed

+106
-3821
lines changed

ipfsapi/client/__init__.py

Lines changed: 89 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,18 @@
1010
import os
1111
import warnings
1212

13+
import ipfshttpclient
14+
1315
DEFAULT_HOST = str(os.environ.get("PY_IPFSAPI_DEFAULT_HOST", 'localhost'))
1416
DEFAULT_PORT = int(os.environ.get("PY_IPFSAPI_DEFAULT_PORT", 5001))
1517
DEFAULT_BASE = str(os.environ.get("PY_IPFSAPI_DEFAULT_BASE", 'api/v0'))
1618

1719
VERSION_MINIMUM = "0.4.3"
1820
VERSION_MAXIMUM = "0.5.0"
1921

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
2523

26-
import ipfsapi.multipart
27-
from .. import utils, exceptions, encoding
24+
from . import base
2825

2926

3027
def assert_version(version, minimum=VERSION_MINIMUM, maximum=VERSION_MAXIMUM):
@@ -54,7 +51,7 @@ def assert_version(version, minimum=VERSION_MINIMUM, maximum=VERSION_MAXIMUM):
5451

5552

5653
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):
5855
"""Create a new :class:`~ipfsapi.Client` instance and connect to the
5956
daemon to validate that its version is supported.
6057
@@ -84,139 +81,89 @@ def connect(host=DEFAULT_HOST, port=DEFAULT_PORT, base=DEFAULT_BASE,
8481
return client
8582

8683

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
220167
def add_pyobj(self, py_obj, **kwargs):
221168
"""Adds a picklable Python object as a file to IPFS.
222169
@@ -280,4 +227,4 @@ def get_pyobj(self, multihash, **kwargs):
280227
"""
281228
warnings.warn("Using `*_pyobj` on untrusted data is a security risk",
282229
DeprecationWarning)
283-
return self.cat(multihash, decoder='pickle', **kwargs)
230+
return encoding.Pickle().parse(self.cat(multihash, **kwargs))

ipfsapi/client/base.py

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,9 @@
33

44
import warnings
55

6-
import ipfsapi.http
7-
import ipfsapi.multipart
8-
96
from . import DEFAULT_HOST, DEFAULT_PORT, DEFAULT_BASE
107

118

12-
class SectionProperty(object):
13-
def __init__(self, cls):
14-
self.cls = cls
15-
self.instance = None
16-
17-
def __get__(self, client_object, type=None):
18-
if not self.instance:
19-
self.instance = self.cls(client_object)
20-
21-
return self.instance
22-
23-
24-
class SectionBase(object):
25-
# Accept parent object from property descriptor
26-
def __init__(self, parent):
27-
self.__parent = parent
28-
29-
# Proxy the parent's properties
30-
@property
31-
def _client(self):
32-
return self.__parent._client
33-
34-
@property
35-
def chunk_size(self):
36-
return self.__parent.chunk_size
37-
38-
@chunk_size.setter
39-
def chunk_size(self, value):
40-
self.__parent.chunk_size = value
41-
42-
43-
class ClientBase(object):
44-
"""A TCP client for interacting with an IPFS daemon.
45-
46-
A :class:`~ipfsapi.Client` instance will not actually establish a
47-
connection to the daemon until at least one of it's methods is called.
48-
49-
Parameters
50-
----------
51-
host : str
52-
Hostname or IP address of the computer running the ``ipfs daemon``
53-
node (defaults to the local system)
54-
port : int
55-
The API port of the IPFS deamon (usually 5001)
56-
base : str
57-
Path of the deamon's API (currently always ``api/v0``)
58-
chunk_size : int
59-
The size of the chunks to break uploaded files and text content into
60-
"""
61-
62-
_clientfactory = ipfsapi.http.HTTPClient
63-
64-
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT,
65-
base=DEFAULT_BASE, chunk_size=ipfsapi.multipart.default_chunk_size,
66-
**defaults):
67-
"""Connects to the API port of an IPFS node."""
68-
69-
self.chunk_size = chunk_size
70-
71-
self._client = self._clientfactory(host, port, base, **defaults)
72-
73-
749
class DeprecatedMethodProperty(object):
7510
def __init__(self, *path, **kwargs):
7611
#PY2: No support for kw-only parameters after glob parameters

0 commit comments

Comments
 (0)