From 9f32be53f4a72220db7323456221f74a595c4d9d Mon Sep 17 00:00:00 2001 From: Stephen O'Connor Date: Mon, 10 Jul 2017 15:05:17 -0400 Subject: [PATCH] Cleanup tests, add missing files. --- tinys3/connection.py | 3 ++- tinys3/request_factory.py | 33 ++++++++++++++++++++------------- tinys3/tests/test_http_args.py | 26 -------------------------- 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/tinys3/connection.py b/tinys3/connection.py index 7e73559..bb67639 100644 --- a/tinys3/connection.py +++ b/tinys3/connection.py @@ -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 @@ -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): """ diff --git a/tinys3/request_factory.py b/tinys3/request_factory.py index 0c77f35..efb8bb6 100644 --- a/tinys3/request_factory.py +++ b/tinys3/request_factory.py @@ -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""" @@ -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 @@ -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')): @@ -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')): @@ -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')): @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -428,7 +434,7 @@ def run(self): data += "" # 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 @@ -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 @@ -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 diff --git a/tinys3/tests/test_http_args.py b/tinys3/tests/test_http_args.py index 33b8a16..baf80f4 100644 --- a/tinys3/tests/test_http_args.py +++ b/tinys3/tests/test_http_args.py @@ -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() - \ No newline at end of file