1- import http .client
21import errno
32import base64
4- from future . moves . urllib . parse import urlparse
3+ import requests
54
65from tusclient .exceptions import TusUploadFailed
76
@@ -16,27 +15,23 @@ class TusRequest(object):
1615 on instantiation.
1716
1817 :Attributes:
19- - handle (<http.client.HTTPConnection >)
18+ - handle (<requests.Session >)
2019 - response_headers (dict)
2120 - file (file):
2221 The file that is being uploaded.
2322 """
2423
2524 def __init__ (self , uploader ):
26- url = urlparse (uploader .url )
27- if url .scheme == 'https' :
28- self .handle = http .client .HTTPSConnection (url .hostname , url .port )
29- else :
30- self .handle = http .client .HTTPConnection (url .hostname , url .port )
31- self ._url = url
25+ self .handle = requests .Session ()
26+ self ._url = uploader .url
3227
3328 self .response_headers = {}
3429 self .status_code = None
3530 self .file = uploader .get_file_stream ()
3631 self .file .seek (uploader .offset )
3732
3833 self ._request_headers = {
39- 'upload-offset' : uploader .offset ,
34+ 'upload-offset' : str ( uploader .offset ) ,
4035 'Content-Type' : 'application/offset+octet-stream'
4136 }
4237 self ._request_headers .update (uploader .headers )
@@ -51,16 +46,13 @@ def response_content(self):
5146 """
5247 Return response data
5348 """
54- return self ._response .read ()
49+ return self ._response .content
5550
5651 def perform (self ):
5752 """
5853 Perform actual request.
5954 """
6055 try :
61- host = '{}://{}' .format (self ._url .scheme , self ._url .netloc )
62- path = self ._url .geturl ().replace (host , '' , 1 )
63-
6456 chunk = self .file .read (self ._content_length )
6557 if self ._upload_checksum :
6658 self ._request_headers ["upload-checksum" ] = \
@@ -70,18 +62,11 @@ def perform(self):
7062 self ._checksum_algorithm (chunk ).digest ()
7163 ).decode ("ascii" ),
7264 ))
73- self .handle .request ("PATCH" , path , chunk , self ._request_headers )
74- self ._response = self .handle .getresponse ()
75- self .status_code = self ._response .status
76- self .response_headers = {k .lower (): v for k , v in self ._response .getheaders ()}
77- except http .client .HTTPException as e :
65+ self ._response = self .handle .patch (self ._url , data = chunk , headers = self ._request_headers )
66+ self .status_code = self ._response .status_code
67+ self .response_headers = {k .lower (): v for k , v in self ._response .headers .items ()}
68+ except requests .exceptions .RequestException as e :
7869 raise TusUploadFailed (e )
79- # wrap connection related errors not raised by the http.client.HTTP(S)Connection
80- # as TusUploadFailed exceptions to enable retries
81- except OSError as e :
82- if e .errno in (errno .EPIPE , errno .ESHUTDOWN , errno .ECONNABORTED , errno .ECONNREFUSED , errno .ECONNRESET ):
83- raise TusUploadFailed (e )
84- raise e
8570
8671 def close (self ):
8772 """
0 commit comments