Skip to content

Commit

Permalink
Cleanup tests, add missing files.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevarino committed Jul 10, 2017
1 parent 388f9ee commit 9f32be5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
3 changes: 2 additions & 1 deletion tinys3/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Base(object):
"""

def __init__(self, access_key, secret_key, default_bucket=None, tls=False,
endpoint="s3.amazonaws.com"):
endpoint="s3.amazonaws.com", http_params=None):
"""
Creates a new S3 connection
Expand All @@ -35,6 +35,7 @@ def __init__(self, access_key, secret_key, default_bucket=None, tls=False,
self.auth = S3Auth(access_key, secret_key)
self.tls = tls
self.endpoint = endpoint
self.http_params = http_params

def bucket(self, bucket):
"""
Expand Down
33 changes: 20 additions & 13 deletions tinys3/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@


class S3Request(object):
def __init__(self, conn, params=None):
def __init__(self, conn, params=None, http_params=None):
self.auth = conn.auth
self.tls = conn.tls
self.endpoint = conn.endpoint
self.params = params
self.http_params = {}
if conn.http_params is not None:
self.http_params = conn.http_params

def bucket_url(self, key, bucket):
"""Function to generate the request URL. Is used by every request"""
Expand Down Expand Up @@ -82,7 +85,8 @@ def __init__(self, conn, key, bucket, headers=None):

def run(self):
url = self.bucket_url(self.key, self.bucket)
r = self.adapter().get(url, auth=self.auth, headers=self.headers)
r = self.adapter().get(url, auth=self.auth, headers=self.headers,
**self.http_params)
r.raise_for_status()
return r

Expand Down Expand Up @@ -113,7 +117,7 @@ def __iter__(self):
resp = self.adapter().get(url, auth=self.auth, params={
'prefix': self.prefix,
'marker': marker,
})
}, **self.http_params)
resp.raise_for_status()
root = ET.fromstring(resp.content)
for tag in root.findall(k('Contents')):
Expand Down Expand Up @@ -171,7 +175,7 @@ def __iter__(self):
'key-marker': self.key_marker,
'prefix': self.prefix,
'upload-id-marker': self.upload_id_marker
})
}, **self.http_params)
resp.raise_for_status()
root = ET.fromstring(resp.content)
for tag in root.findall(k('Upload')):
Expand Down Expand Up @@ -215,7 +219,7 @@ def __iter__(self):
'encoding-type': self.encoding,
'max-parts': self.max_parts,
'part-number-marker': self.part_number_marker
})
}, **self.http_params)
resp.raise_for_status()
root = ET.fromstring(resp.content)
for tag in root.findall(k('Part')):
Expand Down Expand Up @@ -247,7 +251,7 @@ def run(self, data=None):
import lxml.etree as ET
except ImportError:
import xml.etree.ElementTree as ET
r = self.adapter().post(url, auth=self.auth)
r = self.adapter().post(url, auth=self.auth, **self.http_params)
r.raise_for_status()
root = ET.fromstring(r.content)
return root.find(k('UploadId')).text
Expand All @@ -261,7 +265,7 @@ def __init__(self, conn, key, bucket):

def run(self):
url = self.bucket_url(self.key, self.bucket)
r = self.adapter().delete(url, auth=self.auth)
r = self.adapter().delete(url, auth=self.auth, **self.http_params)
r.raise_for_status()
return r

Expand All @@ -275,7 +279,7 @@ def __init__(self, conn, bucket, key='', headers=None):

def run(self):
url = self.bucket_url(self.key, self.bucket)
r = self.adapter().head(url, auth=self.auth, headers=self.headers)
r = self.adapter().head(url, auth=self.auth, headers=self.headers, **self.http_params)
r.raise_for_status()
return r

Expand Down Expand Up @@ -343,7 +347,8 @@ def run(self):
r = self.adapter().put(self.bucket_url(self.key, self.bucket),
data=data,
headers=headers,
auth=self.auth)
auth=self.auth,
**self.http_params)
r.raise_for_status()
finally:
# if close is set, try to close the fp like object
Expand Down Expand Up @@ -396,7 +401,8 @@ def run(self):
r = self.adapter().put(self.bucket_url(self.key, self.bucket),
data=data,
headers=self.headers,
auth=self.auth)
auth=self.auth,
**self.http_params)
r.raise_for_status()
finally:
# if close is set, try to close the fp like object
Expand Down Expand Up @@ -428,7 +434,7 @@ def run(self):
data += "</CompleteMultipartUpload>"
# POST /ObjectName?uploadId=UploadId
url = self.bucket_url(self.key, self.bucket)
r = self.adapter().post(url, auth=self.auth, data=data)
r = self.adapter().post(url, auth=self.auth, data=data, **self.http_params)
r.raise_for_status()
return r

Expand All @@ -444,7 +450,7 @@ def __init__(self, conn, key, bucket, uploadId):
def run(self):
# DELETE /ObjectName?uploadId=UploadId
url = self.bucket_url(self.key, self.bucket)
r = self.adapter().delete(url, auth=self.auth)
r = self.adapter().delete(url, auth=self.auth, **self.http_params)
r.raise_for_status()
return r

Expand Down Expand Up @@ -478,7 +484,8 @@ def run(self):
if self.metadata:
headers.update(self.metadata)
r = self.adapter().put(self.bucket_url(self.to_key, self.to_bucket),
auth=self.auth, headers=headers)
auth=self.auth, headers=headers,
**self.http_params)
r.raise_for_status()
return r

Expand Down
26 changes: 0 additions & 26 deletions tinys3/tests/test_http_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,3 @@ def test_simple_list_request(self):
"""
self.setup_adapter('', '\n'.join(self.files), False)
self.assertEquals(list(self.r.run()), self.parsed_files)


# def test_set_retry(self):
# parent_self = self

# class FakeRequests(object):
# def __init__(self):
# raise AssertionError()
# def get(self, *arg, **kwargs):
# raise AssertionError()
# parent_self.assertTrue('retrffadfadsy' in kwargs)
# parent_self.assertEquals(kwargs['retry'], 30)

# class Fake(ListRequest):
# def adapter(self):
# parent_self.assertFalse(True)
# return FakeRequests()

# conn = Connection("TEST_ACCESS_KEY", "TEST_SECRET_KEY", tls=True,
# http_params={"timeout": 30})
# request = Fake(conn, 'prefix', 'bucket')

# self.assertIn("timeout", conn.http_params)
# self.assertIn("timeout", request.http_params)
# request.run()

0 comments on commit 9f32be5

Please sign in to comment.