hammock
hammock copied to clipboard
Allow the use of resource uris
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/
I simplified the statement as suggested by @fatiherikli
Are you considering to merge the pull request, and is there a new Pypi release planned?
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?
I believe it should be the default behavior. I don't see any situation when I would intentionally have double slashes in a url.
I agree that it should be the default. I made it optional to ensure backward compatibility. I will update the patch accordingly
@twigs could you please make it default behavior and update patch ?
Sorry that it took me so long to make the simple change, totally forgot about this.
@kadirpekel Is it possible to merge this pull request and release an updated PYPI version?