Skip to content

Commit 78518d8

Browse files
committed
added support for adding text from generators
1 parent db04355 commit 78518d8

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

ipfsApi/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ def add_str(self, string, **kwargs):
492492
493493
>> ipfs_client.add_str('Mary had a little lamb')
494494
u'QmZfF6C9j4VtoCsTp4KSrhYH47QMd3DNXVZBKaxJdhaPab'
495+
496+
Also accepts and will stream generator objects.
495497
"""
496498
chunk_size = kwargs.pop('chunk_size',
497499
multipart.default_chunk_size)

ipfsApi/http.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ def request(self, path,
7070
res = requests.request(method, url,
7171
params=params, files=files, **kwargs)
7272

73-
if not decoder:
73+
if path == '/cat':
74+
# since <api>/cat only returns the raw data and not an encoded
75+
# object, dont't try to parse it automatically.
76+
ret = res.text
77+
elif not decoder:
7478
try:
7579
ret = self.default_enc.parse(res.text)
7680
except:

ipfsApi/multipart.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import fnmatch
77
import os
8+
from inspect import isgenerator
89
from sys import version_info
910
from uuid import uuid4
1011

@@ -304,7 +305,7 @@ def __init__(self, text, chunk_size=default_chunk_size):
304305
def body(self):
305306
for chunk in self.gen_chunks(self.envelope.file_open(self.name)):
306307
yield chunk
307-
for chunk in self.gen_chunks([self.text]):
308+
for chunk in self.gen_chunks(self.text):
308309
yield chunk
309310
for chunk in self.gen_chunks(self.envelope.file_close()):
310311
yield chunk
@@ -343,6 +344,8 @@ def stream_text(text, chunk_size=default_chunk_size):
343344
Returns a buffered generator which encodes a string as multipart/form-data.
344345
Also retrns the corresponding headers.
345346
"""
347+
if not isgenerator(text):
348+
text = (text,)
346349
stream = TextStream(text, chunk_size=chunk_size)
347350

348351
return stream.body(), stream.headers

0 commit comments

Comments
 (0)