Skip to content

Commit fd6e407

Browse files
author
Laurynas Butkus
committed
Allow disabling SSL certificate verification
1 parent f269524 commit fd6e407

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

convertapi/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
upload_timeout = 1800
1717
download_timeout = 1800
1818
max_parallel_uploads = 10
19+
verify_ssl = True
1920

2021
client = Client()

convertapi/client.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@
66

77
class Client:
88
def get(self, path, params = {}, timeout = None):
9+
url = self.__url(path)
910
timeout = timeout or convertapi.timeout
10-
r = requests.get(self.url(path), params = params, headers = self.headers(), timeout = timeout)
11-
return self.handle_response(r)
11+
r = self.__session().get(url, params = params, timeout = timeout)
12+
return self.__handle_response(r)
1213

1314
def post(self, path, payload, timeout = None):
15+
url = self.__url(path)
1416
timeout = timeout or convertapi.timeout
15-
r = requests.post(self.url(path), data = payload, headers = self.headers(), timeout = timeout)
16-
return self.handle_response(r)
17+
r = self.__session().post(url, data = payload, timeout = timeout)
18+
return self.__handle_response(r)
1719

1820
def upload(self, io, filename):
1921
url = convertapi.base_uri + 'upload'
2022
encoded_filename = requests.utils.quote(filename)
2123

22-
headers = self.headers()
23-
headers.update({
24+
headers = {
2425
'Content-Disposition': "attachment; filename*=UTF-8''" + encoded_filename,
25-
})
26+
}
2627

27-
r = requests.post(url, data = io, headers = headers, timeout = convertapi.upload_timeout)
28-
return self.handle_response(r)
28+
r = self.__session().post(url, data = io, headers = headers, timeout = convertapi.upload_timeout)
29+
return self.__handle_response(r)
2930

3031
def download(self, url, path):
31-
r = requests.get(url, stream = True, timeout = convertapi.download_timeout)
32+
r = self.__session().get(url, stream = True, timeout = convertapi.download_timeout)
3233

3334
with open(path, 'wb') as f:
3435
for chunk in r.iter_content(chunk_size = 1024):
@@ -38,10 +39,10 @@ def download(self, url, path):
3839
return path
3940

4041
def download_io(self, url):
41-
response = requests.get(url, timeout = convertapi.download_timeout)
42+
response = self.__session().get(url, timeout = convertapi.download_timeout)
4243
return BytesIO(response.content)
4344

44-
def handle_response(self, r):
45+
def __handle_response(self, r):
4546
try:
4647
r.raise_for_status()
4748
except requests.RequestException as e:
@@ -52,10 +53,12 @@ def handle_response(self, r):
5253

5354
return r.json()
5455

55-
def url(self, path):
56+
def __url(self, path):
5657
return "%s%s?Secret=%s" % (convertapi.base_uri, path, convertapi.api_secret)
5758

58-
def headers(self):
59-
return {
60-
'User-Agent': convertapi.user_agent,
61-
}
59+
def __session(self):
60+
s = requests.Session()
61+
s.headers.update({ 'User-Agent': convertapi.user_agent })
62+
s.verify = convertapi.verify_ssl
63+
64+
return s

0 commit comments

Comments
 (0)