Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to send HEAD request #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion tinys3/connection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

from .auth import S3Auth
from .request_factory import UploadRequest, UpdateMetadataRequest, CopyRequest, DeleteRequest, GetRequest, ListRequest
from .request_factory import UploadRequest, UpdateMetadataRequest, CopyRequest, DeleteRequest, GetRequest, HeadRequest, ListRequest


class Base(object):
Expand Down Expand Up @@ -72,6 +72,29 @@ def get(self, key, bucket=None):

return self.run(r)

def head(self, key, bucket=None):
"""
Get a HEAD response from s3 key

Params:
- key The key to make HEAD HTTP request to

- bucket (Optional) The name of the bucket to use (can be skipped if setting the default_bucket)
option for the connection

Returns:
- A response object from the requests lib or a future that wraps that response object if used with a pool.

Usage:

>>> conn.head('my_awesome_key.zip','sample_bucket')

"""

r = HeadRequest(self, key, self.bucket(bucket))

return self.run(r)

def list(self, prefix='', bucket=None):
"""
List files
Expand Down
15 changes: 15 additions & 0 deletions tinys3/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ def run(self):
return r


class HeadRequest(S3Request):
def __init__(self, conn, key, bucket):
super(HeadRequest, self).__init__(conn)
self.key = key
self.bucket = bucket

def run(self):
url = self.bucket_url(self.key, self.bucket)
r = self.adapter().head(url, auth=self.auth)

r.raise_for_status()

return r


class ListRequest(S3Request):
def __init__(self, conn, prefix, bucket):
super(ListRequest, self).__init__(conn)
Expand Down
16 changes: 15 additions & 1 deletion tinys3/tests/test_non_upload_requests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import unittest
from flexmock import flexmock
from tinys3.request_factory import CopyRequest, S3Request, UpdateMetadataRequest, DeleteRequest, GetRequest
from tinys3.request_factory import CopyRequest, S3Request, UpdateMetadataRequest, DeleteRequest, GetRequest, HeadRequest
from tinys3 import Connection


Expand Down Expand Up @@ -77,6 +77,20 @@ def test_get_request(self):

r.run()

def test_head_request(self):
"""
Test the generation of a head request
"""

r = HeadRequest(self.conn, 'key_to_get', 'bucket')

mock = self._mock_adapter(r)

mock.should_receive('head').with_args('https://s3.amazonaws.com/bucket/key_to_get',
auth=self.conn.auth).and_return(self._mock_response()).once()

r.run()

def test_update_metadata(self):
"""
Test the generation of an update metadata request
Expand Down