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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions src/synology_dsm/synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

else:
self._base_url = f"http://{dsm_ip}:{dsm_port}"
self._base_url = URL(f"http://{dsm_ip}:{dsm_port}")

def _debuglog(self, message: str) -> None:
"""Outputs message if debug mode is enabled."""
Expand All @@ -132,15 +132,13 @@ def _is_weird_api_url(self, api: str) -> bool:
and int(self._information.version) < 7321 # < DSM 6
)

def _build_url(self, api: str) -> str:
if self._is_weird_api_url(api):
if api == SynoStorage.API_KEY:
return (
f"{self._base_url}/webman/modules/StorageManager/"
f"storagehandler.cgi?"
)
def _build_url(self, api: str) -> URL:
if self._is_weird_api_url(api) and api == SynoStorage.API_KEY:
return self._base_url.join(
URL("/webman/modules/StorageManager/storagehandler.cgi?")
)

return f"{self._base_url}/webapi/{self.apis[api]['path']}?"
return self._base_url.join(URL(f"/webapi/{self.apis[api]['path']}?"))

async def discover_apis(self) -> None:
"""Retreives available API infos from the NAS."""
Expand Down Expand Up @@ -259,7 +257,7 @@ async def _prepare_request(
method: str,
params: dict | None = None,
**kwargs: Any,
) -> tuple[str, dict, dict]:
) -> tuple[URL, dict, dict]:
"""Prepare the url and parameters for a request."""
# Discover existing APIs
if api != API_INFO:
Expand Down Expand Up @@ -333,18 +331,17 @@ async def _request(
return response

async def _execute_request(
self, method: str, url: str, params: dict | None, **kwargs: Any
self, method: str, url: URL, params: dict | None, **kwargs: Any
) -> bytes | dict | str:
"""Function to execute and handle a request."""
if params:
# special handling for spaces in parameters
# because yarl.URL does encode a space as + instead of %20
# safe extracted from yarl.URL._QUERY_PART_QUOTER
safe = "?/:@-._~!$'()*,"
query = urlencode(params, safe=safe, quote_via=quote)
url_encoded = URL(str(URL(url)) + "?" + query, encoded=True)
query = urlencode(params, safe="?/:@-._~!$'()*,", quote_via=quote)
url_encoded = url.join(URL(f"?{query}", encoded=True))
else:
url_encoded = URL(url)
url_encoded = url

try:
if method == "GET":
Expand Down
Loading