Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idiomatic way to extend paths #48

Open
wbolster opened this issue Nov 16, 2017 · 5 comments
Open

idiomatic way to extend paths #48

wbolster opened this issue Nov 16, 2017 · 5 comments

Comments

@wbolster
Copy link
Contributor

it seems hyperlink does not have a nice way to add path components to a base url, which is a very common operation when interacting with rest apis.

for example, consider

>>> from hyperlink import URL
>>> base_url = URL.from_text('https://example.org/api/v2')

let's assume i want to build a url for an endpoint below this base url, e.g. users/search.

this works:

>>> base_url.replace(path=base_url.path + ('users', 'search'))
URL.from_text('https://example.org/api/v2/users/search')

... but let's face it, this is not so nice:

  • users/search (likely copied from some documentation) needs manual splitting into a tuple
  • base_url is referenced twice since the .path tuple is required for the concatenation

making this nicer is not really possible with the current api. for example:

>>> base_url.replace(path=base_url.path + 'users/search'.split('/'))
[...]
TypeError: can only concatenate tuple (not "list") to tuple

oops. well, that's easy to work around:

>>> base_url.replace(path=base_url.path + tuple('users/search'.split('/')))
URL.from_text('https://example.org/api/v2/users/search')

...but the end result is even uglier.

it would be great if a use case like this is handled with a nicer api. thoughts?

@mahmoud
Copy link
Member

mahmoud commented Dec 1, 2017

I think the idiomatic way is with URL.child(). I guess it's a bit hard to discover, but does that help?

@wbolster
Copy link
Contributor Author

wbolster commented Dec 4, 2017

ah yes, that helps, thanks!

no way to add multiple components using a slash separated string in one go?

.child(*"foo/bar".split())

is, ehm, fugly. ;)

@mahmoud
Copy link
Member

mahmoud commented Dec 4, 2017

I could see automatically splitting the argument(s) by slash and basically doing what you've done there internally. The old URL would allow those special characters through so I'm sure some people used the API that way.

@wbolster
Copy link
Contributor Author

wbolster commented Dec 4, 2017

¿

.child("foo/bar", split=True)

?

@mahmoud
Copy link
Member

mahmoud commented Dec 16, 2017

Finally checked on this, older versions of URL have always escaped slashes, so something like a split kwarg would be necessary for backwards compat. Right now I'm thinking I might be able to subsume this into the solution for #44.

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

No branches or pull requests

2 participants