gruns / furl

🌐 URL parsing and manipulation made easy.
Other
2.6k stars 151 forks source link

Add convenience property method for relative URL #142

Closed echernyavskiy closed 3 years ago

echernyavskiy commented 3 years ago

Not sure this is the best way to implement this, maintainers please chime in.

gruns commented 3 years ago

thank you for this PR, evgeny!

before potentially merging this, let's back up: what, exactly is your intention with a relative URL method?

do you just want the path? or do you want path + query? or path + query + fragment?

and how are you using the relative URL returned by furl.relative_url()? i ask because, for example, it could see it as potentially useful for furl.relative_url() to take a base URL that's used to generate the relative URL. eg

>>> f = furl('https://foo.com/path#fragment')
>>> f.relative_url(base='https://foo.com')
'/path#fragment'
>>> f.relative_url(base='https://foo.com/path')
'#fragment'
>>> f.relative_url(base='https://blah.com')
'https://foo.com/path#fragment'

so i want to understand your intention(s) and how best furl can solve them

echernyavskiy commented 3 years ago

@gruns my specific use case is simply logging the relative URL (path + query + fragment?) on request. I can fetch the fully qualified URL from my framework's request object, and then I was hoping to use furl to fetch the corresponding relative URL. Which is why I only implemented a getter. Re: use cases you proposed for the setter - I don't imagine they'd be highly popular. My thinking is if someone raises an enhancement request to add a setter, we address it then.

gruns commented 3 years ago

in that case, ill add the origin parameter to furl.remove() (and furl.set(), too). that will suffice for your immediate use case and avoid a new method to keep furl's API lean

example:

>>> f = furl('https://foo.com/path#fragment')
>>> relative_url = f.remove(origin=True).url
>>> relative_url
'/path#fragment'

would this satisfy your current use case, where you need the path + query + fragment?

im fine adding to furl's API, like with a new furl.relative_url attribute, but the bar needs to remain high such that new functionality would be frequently useful. so far, youre the only one to request a relative url attribute/method

gruns commented 3 years ago

i just added origin as a param to furl.remove(). so this

>>> f = furl('https://foo.com/path#fragment')
>>> relative_url = f.remove(origin=True).url
>>> relative_url
'/path#fragment'

now works!

let me know if this suffices for your needs of a relative url, for now, and ill package this up and ship this in a new furl version!

echernyavskiy commented 3 years ago

@gruns that's exactly what I needed, much appreciated! When are you planning to tag a new release?

gruns commented 3 years ago

right now :slightly_smiling_face:

update to latest furl v2.1.1 with pip install furl --upgrade. then

>>> f = furl('https://foo.com/path#fragment')
>>> relative_url = f.remove(origin=True).url
>>> relative_url
'/path#fragment'

will work beautifully

future visitors to this PR: if you'd like a furl.relative_url attribute/method and/or furl.remove(origin=True) doesn't suffice for your needs, please comment in this issue!

@echernyavskiy huge thank you for your PR!