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

Reduce url re-encoding #314

Merged
merged 3 commits into from
May 4, 2024
Merged

Reduce url re-encoding #314

merged 3 commits into from
May 4, 2024

Conversation

bdraco
Copy link
Contributor

@bdraco bdraco commented Apr 30, 2024

No description provided.

@@ -110,9 +110,9 @@ def __init__(

# Build variables
if use_https:
self._base_url = f"https://{dsm_ip}:{dsm_port}"
self._base_url = URL(f"https://{dsm_ip}:{dsm_port}")
Copy link
Contributor Author

@bdraco bdraco Apr 30, 2024

Choose a reason for hiding this comment

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

By building this here we avoid calling _make_netloc on every request which has to make an ip_address object and than convert it back to a string

@bdraco
Copy link
Contributor Author

bdraco commented Apr 30, 2024

This works fine on my system, but I don't really have a good way to test the encoding is preserved like #261

@bdraco bdraco marked this pull request as ready for review April 30, 2024 16:44
@bdraco bdraco requested a review from mib1185 as a code owner April 30, 2024 16:44
@mib1185
Copy link
Owner

mib1185 commented May 4, 2024

This works fine on my system, but I don't really have a good way to test the encoding is preserved like #261

it was initially added with #260 to fix home-assistant/core#87457
so an easy way to test would be using a username containing spaces

@bdraco
Copy link
Contributor Author

bdraco commented May 4, 2024

import yarl
from urllib.parse import urlencode, quote

params = {'user': 'with spaces'}
query = urlencode(params, safe="?/:@-._~!$'()*,", quote_via=quote)
url = yarl.URL('http://example.com')
url_encoded = url.join(yarl.URL(f"?{query}", encoded=True))
print(url_encoded)

print (url_encoded.query)
http://example.com/?user=with%20spaces
<MultiDictProxy('user': 'with spaces')>

@bdraco
Copy link
Contributor Author

bdraco commented May 4, 2024

behavior is the same as

import yarl
from urllib.parse import urlencode, quote

params = {'user': 'with spaces'}
safe = "?/:@-._~!$'()*,"
url = 'http://example.com'
query = urlencode(params, safe=safe, quote_via=quote)
url_encoded = yarl.URL(str(yarl.URL(url)) + "?" + query, encoded=True)
print(url_encoded)

print (url_encoded.query)

Copy link
Owner

@mib1185 mib1185 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @bdraco 👍

@mib1185 mib1185 merged commit 1b80469 into mib1185:master May 4, 2024
6 checks passed
@mib1185 mib1185 added the enhancement PR that adds new feature label May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PR that adds new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants