66
77class 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