Skip to content
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
9 changes: 7 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,23 @@ auth credentials through several http requests::
>>> print(watched)

Also keep in mind that if you want a trailing slash at the end of URLs generated by ``Hammock``
you should pass ``append_slash`` kewyword argument as ``True`` while constructing ``Hammock``.
you should pass ``append_slash`` keyword argument as ``True`` while constructing ``Hammock``.
For example::

>>> api = hammock.Hammock('http://localhost:8000', append_slash=True)
>>> print (api.foo.bar) # Note that trailing slash
>>> print (api.foo.bar) # Note the trailing slash
'http://localhost:8000/foo/bar/'


For APIs returning resource uris ``Hammock`` will strip leading and trailing slashes so they can be used directly.


Contributors
------------

* @maraujop (Miguel Araujo)
* @rubik (Michele Lacchia)
* @twigs (Florian Pfaff)

Licence
-------
Expand Down
5 changes: 4 additions & 1 deletion hammock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ class Hammock(object):

HTTP_METHODS = ['get', 'options', 'head', 'post', 'put', 'patch', 'delete']

def __init__(self, name=None, parent=None, append_slash=False, **kwargs):
def __init__(self, name=None, parent=None, append_slash=False, strip_slash=False, **kwargs):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strip_slash should be defaulted to True

strip_slash=True

"""Constructor

Arguments:
name -- name of node
parent -- parent node for chaining
append_slash -- flag if you want a trailing slash in urls
strip_slashes -- flag if you want to strip leading and trailing slashes from arguments
**kwargs -- `requests` session be initiated with if any available
"""
self._name = name

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for strip_slash value
self._name = name.strip('/') if strip_slash else name

self._parent = parent
self._append_slash = append_slash
self._strip_slash = strip_slash
self._session = requests.session()
for k, v in kwargs.items():
orig = getattr(self._session, k) # Let it throw exception
Expand All @@ -35,6 +37,7 @@ def _spawn(self, name):
"""
child = copy.copy(self)
child._name = name
child._name = child._name.strip('/')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check strip_slash value

child._name = name.strip('/') if self._strip_slash else name

child._parent = self
return child

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='hammock',
py_modules=['hammock'],
version='0.2.4',
version='0.2.5',
description='rest like a boss',
author='Kadir Pekel',
author_email='kadirpekel@gmail.com',
Expand Down
13 changes: 13 additions & 0 deletions test_hammock.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,18 @@ def test_session(self):
self.assertIn('Accept', request.headers)
self.assertEqual(request.headers.get('Accept'), ACCEPT_HEADER)

@httprettified
def test_strip_slash(self):
"""
test if leading and trailing slashes are removed (e.g. from resource uris)
"""
HTTPretty.register_uri(HTTPretty.GET, self.URL)
client = Hammock(self.BASE_URL)
resp = client(self.PATH).GET()
self.assertEqual(HTTPretty.last_request.path, self.PATH)
resp = client(self.PATH+'/').GET()
self.assertEqual(HTTPretty.last_request.path, self.PATH)


if __name__ == '__main__':
unittest.main()