Skip to content

Allow the use of resource uris#13

Open
twigs wants to merge 3 commits into
kadirpekel:masterfrom
twigs:master
Open

Allow the use of resource uris#13
twigs wants to merge 3 commits into
kadirpekel:masterfrom
twigs:master

Conversation

@twigs

@twigs twigs commented May 28, 2013

Copy link
Copy Markdown

Some rest based APIs use resource URIs to point to specific, usually related resources. These URIs are usually absolute paths for the domain hosting the API and are returned by previous API calls.
At the moment there is no way to use those URIs directly with hammock. You either have to modify the uri by removing leading slashes or to use requests directly.

This proposed patch adds a new keyword strip_slash which will remove leading and trailing slashes from the name when spawning new Hammock instances. This allows to use the URIs directly.

current behavior::

import hammock
api = hammock.Hammock('http://localhost:8000')
my_ressource_uri = '/api/v1/users/4711/'
print api(my_ressource_uri) # note the double slash after domain:port
http://localhost:8000//api/v1/users/4711/  

with strip_slash::

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

can be used with append_slash::

import hammock
api = hammock.Hammock('http://localhost:8000', strip_slash=True, append_slash=True)
my_ressource_uri = '/api/v1/users/4711/'
print api(my_ressource_uri) 
http://localhost:8000/api/v1/users/4711/

@twigs

twigs commented May 29, 2013

Copy link
Copy Markdown
Author

I simplified the statement as suggested by @fatiherikli

@twigs

twigs commented Jun 18, 2013

Copy link
Copy Markdown
Author

Are you considering to merge the pull request, and is there a new Pypi release planned?

@kadirpekel

Copy link
Copy Markdown
Owner

I'm not certain and sure about for exposing this functionality though an optional argument. I prefer this to be default behavior or not exist at all. What you think?

@vitormazzi

Copy link
Copy Markdown

I believe it should be the default behavior. I don't see any situation when I would intentionally have double slashes in a url.

@twigs

twigs commented Jun 24, 2013

Copy link
Copy Markdown
Author

I agree that it should be the default. I made it optional to ensure backward compatibility. I will update the patch accordingly

@mesuutt

mesuutt commented Nov 5, 2013

Copy link
Copy Markdown

@twigs could you please make it default behavior and update patch ?

@twigs

twigs commented Nov 5, 2013

Copy link
Copy Markdown
Author

Sorry that it took me so long to make the simple change, totally forgot about this.

@twigs

twigs commented Nov 7, 2013

Copy link
Copy Markdown
Author

@kadirpekel Is it possible to merge this pull request and release an updated PYPI version?

E-G-C added a commit to E-G-C/hammock that referenced this pull request Nov 4, 2019
Added custom  sessions 
- see kadirpekel#14
- removing redundant slashes see kadirpekel#13
- better unicode as per https://github.com/bepress/hammock/blob/master/hammock.py

@E-G-C E-G-C left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Althought the strip_slash was added, it was never used. Also it should be defaulted to True

Comment thread hammock.py
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

Comment thread hammock.py
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

Comment thread hammock.py
"""
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants