Skip to content

Commit da76c9c

Browse files
Merge pull request #34 from adrpar/fix_request_to_use_proxy
use requests library everywhere
2 parents 0e5856e + be73ec2 commit da76c9c

File tree

2 files changed

+18
-33
lines changed

2 files changed

+18
-33
lines changed

tests/test_request.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def test_perform(self):
1717
self.request.perform()
1818
with open('LICENSE', 'rb') as f:
1919
headers = {
20-
'upload-offset': 0,
20+
'upload-offset': '0',
2121
'Content-Type': 'application/offset+octet-stream'
2222
}
2323
headers.update(self.uploader.headers)
24-
mock_.request.assert_called_with(
25-
'PATCH', '/files/15acd89eabdf5738ffc',
26-
f.read(), headers)
24+
mock_.patch.assert_called_with(
25+
'http://master.tus.io/files/15acd89eabdf5738ffc',
26+
data=f.read(), headers=headers)
2727

2828
def test_perform_checksum(self):
2929
self.uploader.upload_checksum = True
@@ -34,15 +34,15 @@ def test_perform_checksum(self):
3434
with open('LICENSE', 'rb') as f:
3535
license = f.read()
3636
headers = {
37-
'upload-offset': 0,
37+
'upload-offset': '0',
3838
'Content-Type': 'application/offset+octet-stream'
3939
}
4040
headers.update(self.uploader.headers)
4141
headers["upload-checksum"] = "sha1 " + \
4242
base64.standard_b64encode(hashlib.sha1(license).digest()).decode("ascii")
43-
mock_.request.assert_called_with(
44-
'PATCH', '/files/15acd89eabdf5738ffc',
45-
license, headers)
43+
mock_.patch.assert_called_with(
44+
'http://master.tus.io/files/15acd89eabdf5738ffc',
45+
data=license, headers=headers)
4646

4747
def test_close(self):
4848
with mock.patch.object(self.request, 'handle') as mock_:

tusclient/request.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import http.client
21
import errno
32
import base64
4-
from future.moves.urllib.parse import urlparse
3+
import requests
54

65
from 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

Comments
 (0)