Skip to content

Commit 78416a0

Browse files
committed
Add support for async mode operation
1 parent 832c28b commit 78416a0

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

convertapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .exceptions import *
44
from .client import Client
55
from .upload_io import UploadIO
6-
from .api import convert, user
6+
from .api import convert, user, async_convert, async_poll
77

88
# configuration
99

convertapi/api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import convertapi
22

33
from .task import Task
4+
from .result import Result
45

56
def convert(to_format, params, from_format = None, timeout = None):
67
task = Task(from_format, to_format, params, timeout = timeout)
78
return task.run()
89

10+
911
def user(timeout = None):
1012
return convertapi.client.get('user', timeout = timeout)
13+
14+
15+
def async_convert(to_format, params, from_format = None, timeout = None):
16+
task = Task(from_format, to_format, params, timeout = timeout, is_async = True)
17+
return task.run()
18+
19+
20+
def async_poll(job_id):
21+
response = convertapi.client.get('async/job/%s' % job_id)
22+
return Result(response)

convertapi/task.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
DEFAULT_URL_FORMAT = 'web'
77

88
class Task:
9-
def __init__(self, from_format, to_format, params, timeout = None):
9+
def __init__(self, from_format, to_format, params, timeout = None, is_async = False):
1010
self.from_format = from_format
1111
self.to_format = to_format
1212
self.params = params
1313
self.timeout = timeout or convertapi.conversion_timeout
14+
self.is_async = is_async
1415

1516
self.default_params = {
1617
'Timeout': self.timeout,
@@ -21,7 +22,8 @@ def run(self):
2122
params = self.__normalize_params()
2223
from_format = self.from_format or self.__detect_format()
2324
timeout = self.timeout + convertapi.conversion_timeout_delta if self.timeout else None
24-
path = "convert/%s/to/%s" % (from_format, self.to_format)
25+
base_path = 'convert' if not self.is_async else 'async/convert'
26+
path = "%s/%s/to/%s" % (base_path, from_format, self.to_format)
2527

2628
if 'converter' in params:
2729
path += "/converter/%s" % (params['converter'])

tests/test_convertapi.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import io
44
import tempfile
5+
import time
56
import requests
67

78
from . import utils
@@ -74,3 +75,29 @@ def test_api_error(self):
7475
def test_user_info(self):
7576
user_info = convertapi.user()
7677
assert user_info['Active']
78+
79+
class TestAsyncConvertapi(utils.TestCase):
80+
def setUp(self):
81+
convertapi.api_secret = os.environ['CONVERT_API_SECRET']
82+
convertapi.max_parallel_uploads = 10
83+
84+
def test_async_convert_file(self):
85+
convert_result = convertapi.async_convert('pdf', { 'File': 'examples/files/test.docx' })
86+
assert convert_result.response['JobId']
87+
88+
poll_result = get_poll_result(convert_result.response['JobId'])
89+
assert poll_result.save_files(tempfile.gettempdir())
90+
assert poll_result.conversion_cost > 0
91+
92+
93+
def get_poll_result(job_id, retry_count=5):
94+
try:
95+
result = convertapi.async_poll(job_id)
96+
except Exception as error:
97+
if retry_count > 0:
98+
time.sleep(0.1)
99+
return get_poll_result(job_id, retry_count=retry_count - 1)
100+
else:
101+
raise error
102+
else:
103+
return result

0 commit comments

Comments
 (0)