Skip to content

Commit 50bdf5c

Browse files
Merge pull request #3 from ConvertAPI/feature/result_file_io
Allow getting result file io
2 parents e201917 + dfec101 commit 50bdf5c

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

convertapi/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
import convertapi
33

4+
from io import BytesIO
45
from .exceptions import *
56

67
class Client:
@@ -36,6 +37,10 @@ def download(self, url, path):
3637

3738
return path
3839

40+
def download_io(self, url):
41+
response = requests.get(url, timeout = convertapi.download_timeout)
42+
return BytesIO(response.content)
43+
3944
def handle_response(self, r):
4045
try:
4146
r.raise_for_status()

convertapi/result_file.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def filename(self):
1717
def size(self):
1818
return self.info['FileSize']
1919

20+
@property
21+
def io(self):
22+
if not hasattr(self, '_io'):
23+
self._io = convertapi.client.download_io(self.url)
24+
25+
return self._io
26+
2027
def save(self, path):
2128
if os.path.isdir(path):
2229
path = os.path.join(path, self.filename)

tests/test_convertapi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def test_upload_io(self):
3737
result = convertapi.convert('pdf', { 'File': upload_io })
3838
assert result.conversion_cost > 0
3939

40+
def test_download_io(self):
41+
result = convertapi.convert('pdf', { 'Url': 'https://www.w3.org/TR/PNG/iso_8859-1.txt' })
42+
assert len(result.file.io.getvalue()) > 0
43+
4044
def test_zip_files(self):
4145
files = ['examples/files/test.docx', 'examples/files/test.docx']
4246
result = convertapi.convert('zip', { 'Files': files })

0 commit comments

Comments
 (0)