Skip to content

Commit 7149806

Browse files
author
Laurynas Butkus
committed
Convert url and save result
1 parent c775aff commit 7149806

File tree

8 files changed

+111
-11
lines changed

8 files changed

+111
-11
lines changed

convertapi/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .exceptions import *
44
from .configuration import Configuration
55
from .client import Client
6+
from .api import convert
67

78
configuration = Configuration()
89
client = Client(configuration)

convertapi/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .task import Task
2+
3+
def convert(to_format, params, from_format = None, conversion_timeout = None):
4+
task = Task(from_format, to_format, params, conversion_timeout = conversion_timeout)
5+
return task.run()

convertapi/client.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,24 @@ class Client:
66
def __init__(self, configuration):
77
self.configuration = configuration
88

9-
def get(self, path, params = {}):
10-
r = requests.get(self.url(path), params=params, headers = self.headers())
9+
def get(self, path, params = {}, timeout = None):
10+
r = requests.get(self.url(path), params = params, headers = self.headers(), timeout = timeout)
1111
return self.handle_response(r)
1212

13-
def post(self, path, payload):
14-
r = requests.post(self.url(path), json=payload, headers = self.headers())
13+
def post(self, path, payload, timeout = None):
14+
r = requests.post(self.url(path), data = payload, headers = self.headers(), timeout = timeout)
1515
return self.handle_response(r)
1616

17+
def download(self, url, path):
18+
r = requests.get(url, stream = True, timeout = self.configuration.download_timeout)
19+
20+
with open(path, 'wb') as f:
21+
for chunk in r.iter_content(chunk_size = 1024):
22+
if chunk:
23+
f.write(chunk)
24+
25+
return path
26+
1727
def handle_response(self, r):
1828
json = r.json()
1929

@@ -25,7 +35,7 @@ def handle_response(self, r):
2535
return json
2636

2737
def url(self, path):
28-
return "%s/%s?Secret=%s" % (self.configuration.base_uri, path, self.configuration.api_secret)
38+
return "%s%s?Secret=%s" % (self.configuration.base_uri, path, self.configuration.api_secret)
2939

3040
def headers(self):
3141
return {

convertapi/configuration.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ class Configuration:
44
api_secret = None
55
base_uri = 'https://v2.convertapi.com/'
66
user_agent = 'ConvertAPI-Python/' + convertapi.__version__
7-
connect_timeout = 5
8-
read_timeout = 60
7+
timeout = 60
98
conversion_timeout = 180
109
conversion_timeout_delta = 10
1110
upload_timeout = 600

convertapi/result.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from .result_file import ResultFile
2+
3+
class Result:
4+
def __init__(self, response):
5+
self.response = response
6+
7+
@property
8+
def conversion_cost(self):
9+
return self.response['ConversionCost']
10+
11+
@property
12+
def file(self):
13+
return self.files[0]
14+
15+
@property
16+
def files(self):
17+
return map(ResultFile, self.response['Files'])
18+
19+
def save_files(self, path):
20+
return [file.save(path) for file in self.files]

convertapi/result_file.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import convertapi
2+
import os
3+
4+
class ResultFile:
5+
def __init__(self, info):
6+
self.info = info
7+
8+
@property
9+
def url(self):
10+
return self.info['Url']
11+
12+
@property
13+
def filename(self):
14+
return self.info['FileName']
15+
16+
@property
17+
def size(self):
18+
return self.info['FileSize']
19+
20+
def save(self, path):
21+
if os.path.isdir(path):
22+
path = os.path.join(path, self.filename)
23+
24+
return convertapi.client.download(self.url, path)

convertapi/task.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import convertapi
2+
3+
from .result import Result
4+
5+
class Task:
6+
def __init__(self, from_format, to_format, params, conversion_timeout = None):
7+
self.from_format = from_format
8+
self.to_format = to_format
9+
self.params = params
10+
self.conversion_timeout = conversion_timeout or convertapi.configuration.conversion_timeout
11+
12+
self.default_params = {
13+
'Timeout': self.conversion_timeout,
14+
'StoreFile': True,
15+
}
16+
17+
def run(self):
18+
params = self.__normalize_params()
19+
from_format = self.from_format
20+
timeout = self.conversion_timeout + convertapi.configuration.conversion_timeout_delta
21+
path = "convert/%s/to/%s" % (from_format, self.to_format)
22+
23+
response = convertapi.client.post(path, params, timeout = timeout)
24+
25+
return Result(response)
26+
27+
def __normalize_params(self):
28+
params = self.params.copy()
29+
params.update(self.default_params)
30+
return params

tests/test_convertapi.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
import convertapi
2+
import os
3+
import tempfile
4+
15
from . import utils
26
from nose.tools import eq_
3-
import convertapi
47

58
class TestConvertapi(utils.TestCase):
6-
def testDefaults(self):
9+
def setUp(self):
10+
convertapi.configuration.api_secret = os.environ['CONVERT_API_SECRET']
11+
12+
def test_defaults(self):
713
eq_('https://v2.convertapi.com/', convertapi.configuration.base_uri)
814

9-
def testConfiguration(self):
15+
def test_configuration(self):
1016
convertapi.configuration.api_secret = 'TEST'
11-
eq_('TEST', convertapi.client.configuration.api_secret)
17+
eq_('TEST', convertapi.client.configuration.api_secret)
18+
19+
def test_convert(self):
20+
result = convertapi.convert('pdf', { 'Url': 'http://convertapi.com' }, 'web')
21+
print(result.save_files(tempfile.gettempdir()))
22+

0 commit comments

Comments
 (0)