From c96d2b2de0c4f2e5713d0ec19d6c7156ab752d17 Mon Sep 17 00:00:00 2001 From: Xiaonan Shen Date: Thu, 14 May 2020 15:31:03 -0700 Subject: [PATCH] Fix login with space in password (#50) --- synology_dsm/synology_dsm.py | 19 ++++++++++++++++--- tests/__init__.py | 4 ++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/synology_dsm/synology_dsm.py b/synology_dsm/synology_dsm.py index 572824d2..73eaa0e7 100644 --- a/synology_dsm/synology_dsm.py +++ b/synology_dsm/synology_dsm.py @@ -2,6 +2,7 @@ """Class to interact with Synology DSM.""" import socket import urllib3 +import six from requests import Session from requests.exceptions import RequestException from simplejson.errors import JSONDecodeError @@ -24,6 +25,11 @@ from .api.storage.storage import SynoStorage from .const import API_AUTH, API_INFO +if six.PY2: + from future.moves.urllib.parse import quote +else: + from urllib.parse import quote # pylint: disable=import-error,no-name-in-module + class SynologyDSM(object): """Class containing the main Synology DSM functions.""" @@ -232,14 +238,21 @@ def _request( return response - def _execute_request(self, method, url, **kwargs): + def _execute_request(self, method, url, params, **kwargs): """Function to execute and handle a request.""" # Execute Request try: if method == "GET": - resp = self._session.get(url, **kwargs) + if six.PY2: + items = params.iteritems() + else: + items = params.items() + encoded_params = "&".join( + "%s=%s" % (key, quote(str(value))) for key, value in items + ) + resp = self._session.get(url, params=encoded_params, **kwargs) elif method == "POST": - resp = self._session.post(url, **kwargs) + resp = self._session.post(url, pararms=params, **kwargs) self._debuglog("Request url: " + resp.url) self._debuglog("Request status_code: " + str(resp.status_code)) diff --git a/tests/__init__.py b/tests/__init__.py index c1d205fd..c4889bad 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -119,8 +119,8 @@ def __init__( self.disks_redundancy = "RAID" # RAID or SHR[number][_EXPANSION] self.error = False - def _execute_request(self, method, url, **kwargs): - url += urlencode(kwargs["params"]) + def _execute_request(self, method, url, params, **kwargs): + url += urlencode(params) if "no_internet" in url: raise SynologyDSMRequestException(