From ae728be7abac130951da4b918d46c51c66b1d9d4 Mon Sep 17 00:00:00 2001 From: twigs Date: Tue, 28 May 2013 09:31:45 +0200 Subject: [PATCH 1/3] Added new keyword 'strip_slash' to allow the use or REST resource uris --- README.rst | 17 +++++++++++++++-- hammock.py | 6 +++++- setup.py | 2 +- test_hammock.py | 16 ++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 1cf481a..e04f272 100644 --- a/README.rst +++ b/README.rst @@ -133,18 +133,31 @@ 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/' + +If the API you are interfacing with returns resource uris the ``strip_slash`` keyword will strip leading and trailing +slashes from arguments so they can be used directly. If you need trailing slash in the final url ``append_slash`` and +``strip_slash`` can be combined:: + + >>> import hammock + >>> api = hammock.Hammock('http://localhost:8000', strip_slash=True) + >>> my_ressource_uri = '/api/v1/users/4711/' + >>> print api(my_ressource_uri) + http://localhost:8000/api/v1/users/4711 + + Contributors ------------ * @maraujop (Miguel Araujo) * @rubik (Michele Lacchia) +* @twigs (Florian Pfaff) Licence ------- diff --git a/hammock.py b/hammock.py index f00a4d1..a95a8e0 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,8 @@ def _spawn(self, name): """ child = copy.copy(self) child._name = name + if self._strip_slash: + child._name = child._name.lstrip('/').rstrip('/') 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..c03192b 100644 --- a/test_hammock.py +++ b/test_hammock.py @@ -93,5 +93,21 @@ def test_session(self): self.assertIn('Accept', request.headers) self.assertEqual(request.headers.get('Accept'), ACCEPT_HEADER) + @httprettified + def test_strip_slash_option(self): + HTTPretty.register_uri(HTTPretty.GET, self.URL) + client = Hammock(self.BASE_URL, strip_slash=True) + 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) + + @httprettified + def test_append_and_strip_slash_option(self): + HTTPretty.register_uri(HTTPretty.GET, self.URL + '/') + client = Hammock(self.BASE_URL, strip_slash=True, append_slash='/') + resp = client(self.PATH).GET() + self.assertEqual(HTTPretty.last_request.path, self.PATH + '/') + if __name__ == '__main__': unittest.main() From 3bec43ed238432fd315160591df8e68363eedb31 Mon Sep 17 00:00:00 2001 From: twigs Date: Wed, 29 May 2013 12:01:14 +0200 Subject: [PATCH 2/3] simplified strip statement --- hammock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hammock.py b/hammock.py index a95a8e0..03aa7b5 100644 --- a/hammock.py +++ b/hammock.py @@ -38,7 +38,7 @@ def _spawn(self, name): child = copy.copy(self) child._name = name if self._strip_slash: - child._name = child._name.lstrip('/').rstrip('/') + child._name = child._name.strip('/') child._parent = self return child From 06bf0c982182624a55290def1587e96225d4f840 Mon Sep 17 00:00:00 2001 From: Twigs Date: Tue, 5 Nov 2013 13:19:10 +0100 Subject: [PATCH 3/3] removed strip_slash option and made it default behavior --- README.rst | 10 +--------- hammock.py | 3 +-- test_hammock.py | 13 +++++-------- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/README.rst b/README.rst index e04f272..8ef3314 100644 --- a/README.rst +++ b/README.rst @@ -141,15 +141,7 @@ For example:: 'http://localhost:8000/foo/bar/' -If the API you are interfacing with returns resource uris the ``strip_slash`` keyword will strip leading and trailing -slashes from arguments so they can be used directly. If you need trailing slash in the final url ``append_slash`` and -``strip_slash`` can be combined:: - - >>> import hammock - >>> api = hammock.Hammock('http://localhost:8000', strip_slash=True) - >>> my_ressource_uri = '/api/v1/users/4711/' - >>> print api(my_ressource_uri) - http://localhost:8000/api/v1/users/4711 +For APIs returning resource uris ``Hammock`` will strip leading and trailing slashes so they can be used directly. Contributors diff --git a/hammock.py b/hammock.py index 03aa7b5..859afcc 100644 --- a/hammock.py +++ b/hammock.py @@ -37,8 +37,7 @@ def _spawn(self, name): """ child = copy.copy(self) child._name = name - if self._strip_slash: - child._name = child._name.strip('/') + child._name = child._name.strip('/') child._parent = self return child diff --git a/test_hammock.py b/test_hammock.py index c03192b..f44325f 100644 --- a/test_hammock.py +++ b/test_hammock.py @@ -94,20 +94,17 @@ def test_session(self): self.assertEqual(request.headers.get('Accept'), ACCEPT_HEADER) @httprettified - def test_strip_slash_option(self): + 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, strip_slash=True) + 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) - @httprettified - def test_append_and_strip_slash_option(self): - HTTPretty.register_uri(HTTPretty.GET, self.URL + '/') - client = Hammock(self.BASE_URL, strip_slash=True, append_slash='/') - resp = client(self.PATH).GET() - self.assertEqual(HTTPretty.last_request.path, self.PATH + '/') if __name__ == '__main__': unittest.main()