diff --git a/README.rst b/README.rst index 1cf481a..8ef3314 100644 --- a/README.rst +++ b/README.rst @@ -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 ------- diff --git a/hammock.py b/hammock.py index f00a4d1..859afcc 100644 --- a/hammock.py +++ b/hammock.py @@ -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): """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 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 @@ -35,6 +37,7 @@ def _spawn(self, name): """ child = copy.copy(self) child._name = name + child._name = child._name.strip('/') child._parent = self return child diff --git a/setup.py b/setup.py index 1f827e8..002c720 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/test_hammock.py b/test_hammock.py index a38851c..f44325f 100644 --- a/test_hammock.py +++ b/test_hammock.py @@ -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()