From 3d40eb38af6a15e5f2ad40be000b875dd43d35b8 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Fri, 27 Nov 2020 14:26:45 +0100 Subject: [PATCH 1/4] Add schedule API and refactor info/config/stat apis in DownloadStation --- .../api/download_station/__init__.py | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/synology_dsm/api/download_station/__init__.py b/src/synology_dsm/api/download_station/__init__.py index 94866dd5..42f28589 100644 --- a/src/synology_dsm/api/download_station/__init__.py +++ b/src/synology_dsm/api/download_station/__init__.py @@ -7,6 +7,7 @@ class SynoDownloadStation: API_KEY = "SYNO.DownloadStation.*" INFO_API_KEY = "SYNO.DownloadStation.Info" + SCHEDULE_API_KEY = "SYNO.DownloadStation.Schedule" STAT_API_KEY = "SYNO.DownloadStation.Statistic" TASK_API_KEY = "SYNO.DownloadStation.Task" @@ -14,6 +15,10 @@ def __init__(self, dsm): """Initialize a Download Station.""" self._dsm = dsm self._tasks_by_id = {} + self._schedule_config = {} + self._stat = {} + self._info = {} + self._config = {} self.additionals = [ "detail", "file", @@ -32,17 +37,50 @@ def update(self): self._tasks_by_id[task_data["id"]] = SynoDownloadTask(task_data) # Global + def update_info(self): + """Update info about the Download Station instance.""" + self._info = self._dsm.get(self.INFO_API_KEY, "GetInfo")["data"] + def get_info(self): """Return general informations about the Download Station instance.""" - return self._dsm.get(self.INFO_API_KEY, "GetInfo") + return self._info + + def update_config(self): + """Update configuration about the Download Station instance.""" + self._config = self._dsm.get(self.INFO_API_KEY, "GetConfig")["data"] def get_config(self): """Return configuration about the Download Station instance.""" - return self._dsm.get(self.INFO_API_KEY, "GetConfig") + return self._config + + def update_schedule_config(self): + """Update schedule configuration about the Download Station instance.""" + self._schedule_config = self._dsm.get(self.SCHEDULE_API_KEY, "GetConfig")[ + "data" + ] + + def get_schedule_config(self): + """Return schedule configuration about the Download Station instance.""" + return self._schedule_config + + def set_schedule_config(self, enabled: bool = None, emule_enabled: bool = None): + """Set schedule configuration about the Download Station instance.""" + config = {} + if enabled != None: + config["enabled"] = enabled + if emule_enabled != None: + config["emule_enabled"] = emule_enabled + res = self._dsm.get(self.SCHEDULE_API_KEY, "SetConfig", config) + self.update_schedule_config() + return res + + def update_stat(self): + """Update statistic about the Download Station instance.""" + self._stat = self._dsm.get(self.STAT_API_KEY, "GetInfo")["data"] def get_stat(self): """Return statistic about the Download Station instance.""" - return self._dsm.get(self.STAT_API_KEY, "GetInfo") + return self._stat # Downloads def get_all_tasks(self): From 19f5af856cb2f713f31798ed9c9c6949b10de646 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Fri, 27 Nov 2020 15:14:36 +0100 Subject: [PATCH 2/4] Fix/Add tests --- README.rst | 14 +++++++++++++- .../api/download_station/__init__.py | 4 +--- tests/__init__.py | 4 ++++ tests/api_data/dsm_6/__init__.py | 4 ++++ tests/test_synology_dsm.py | 16 +++++++++++++--- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index b03430d3..3e1ecfd0 100644 --- a/README.rst +++ b/README.rst @@ -139,10 +139,22 @@ Download Station usage api = SynologyDSM("", "", "", "") if "SYNO.DownloadStation.Info" in api.apis: - + + api.download_station.update_info() api.download_station.get_info() + + api.download_station.update_config() api.download_station.get_config() + api.download_station.update_stat() + api.download_station.get_stat() + + api.download_station.update_schedule_config() + api.download_station.get_schedule_config() + # set_schedule_config(self, enabled: bool = None, emule_enabled: bool = None) + api.download_station.set_schedule_config(True, False) + + # The download list will be updated after each of the following functions: # You should have the right on the (default) directory that the download will be saved, or you will get a 403 or 406 error api.download_station.create("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4") diff --git a/src/synology_dsm/api/download_station/__init__.py b/src/synology_dsm/api/download_station/__init__.py index 42f28589..a7ed3b68 100644 --- a/src/synology_dsm/api/download_station/__init__.py +++ b/src/synology_dsm/api/download_station/__init__.py @@ -55,9 +55,7 @@ def get_config(self): def update_schedule_config(self): """Update schedule configuration about the Download Station instance.""" - self._schedule_config = self._dsm.get(self.SCHEDULE_API_KEY, "GetConfig")[ - "data" - ] + self._schedule_config = self._dsm.get(self.SCHEDULE_API_KEY, "GetConfig")["data"] def get_schedule_config(self): """Return schedule configuration about the Download Station instance.""" diff --git a/tests/__init__.py b/tests/__init__.py index b42188ac..41e9c1a2 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -29,6 +29,7 @@ from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_INFO_CONFIG from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_INFO_INFO from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_STAT_INFO +from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_SCHEDULE_CONFIG from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_TASK_LIST from .api_data.dsm_6 import DSM_6_DSM_INFORMATION from .api_data.dsm_6 import DSM_6_DSM_NETWORK_2LAN_1PPPOE @@ -254,6 +255,9 @@ def _execute_request(self, method, url, params, **kwargs): if SynoDownloadStation.TASK_API_KEY in url: if "List" in url: return DSM_6_DOWNLOAD_STATION_TASK_LIST + if SynoDownloadStation.SCHEDULE_API_KEY in url: + if "GetConfig" in url: + return DSM_6_DOWNLOAD_STATION_SCHEDULE_CONFIG if SynoStorage.API_KEY in url: return API_SWITCHER[self.dsm_version]["STORAGE_STORAGE"][ diff --git a/tests/api_data/dsm_6/__init__.py b/tests/api_data/dsm_6/__init__.py index 394e7587..c6b260c5 100644 --- a/tests/api_data/dsm_6/__init__.py +++ b/tests/api_data/dsm_6/__init__.py @@ -21,6 +21,9 @@ from .download_station.const_6_download_station_stat import ( DSM_6_DOWNLOAD_STATION_STAT_INFO, ) +from .download_station.const_6_download_station_schedule_config import ( + DSM_6_DOWNLOAD_STATION_SCHEDULE_CONFIG, +) from .download_station.const_6_download_station_task import ( DSM_6_DOWNLOAD_STATION_TASK_LIST, ) @@ -77,6 +80,7 @@ "DSM_6_DOWNLOAD_STATION_INFO_CONFIG", "DSM_6_DOWNLOAD_STATION_INFO_INFO", "DSM_6_DOWNLOAD_STATION_STAT_INFO", + "DSM_6_DOWNLOAD_STATION_SCHEDULE_CONFIG", "DSM_6_DOWNLOAD_STATION_TASK_LIST", "DSM_6_DSM_INFORMATION", "DSM_6_DSM_NETWORK_2LAN_1PPPOE", diff --git a/tests/test_synology_dsm.py b/tests/test_synology_dsm.py index d24023a9..aa8cb808 100644 --- a/tests/test_synology_dsm.py +++ b/tests/test_synology_dsm.py @@ -869,9 +869,19 @@ def test_download_station(self): assert self.api.download_station assert not self.api.download_station.get_all_tasks() - assert self.api.download_station.get_info()["data"]["version"] - assert self.api.download_station.get_config()["data"]["default_destination"] - assert self.api.download_station.get_stat()["data"]["speed_download"] + self.api.download_station.update_info() + assert self.api.download_station.get_info()["version"] + + self.api.download_station.update_config() + assert self.api.download_station.get_config()["default_destination"] + + self.api.download_station.update_stat() + assert self.api.download_station.get_stat()["speed_download"] + + self.api.download_station.update_schedule_config() + assert self.api.download_station.get_schedule_config()["enabled"] is True + assert self.api.download_station.get_schedule_config()["emule_enabled"] is False + self.api.download_station.update() assert self.api.download_station.get_all_tasks() assert len(self.api.download_station.get_all_tasks()) == 8 From 15199a7f5f5049181760c4318ded2da38d3597ab Mon Sep 17 00:00:00 2001 From: Sylvain Date: Fri, 27 Nov 2020 15:16:40 +0100 Subject: [PATCH 3/4] ScheduleConfig API data --- .../const_6_download_station_schedule_config.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/api_data/dsm_6/download_station/const_6_download_station_schedule_config.py diff --git a/tests/api_data/dsm_6/download_station/const_6_download_station_schedule_config.py b/tests/api_data/dsm_6/download_station/const_6_download_station_schedule_config.py new file mode 100644 index 00000000..3d6ca5ce --- /dev/null +++ b/tests/api_data/dsm_6/download_station/const_6_download_station_schedule_config.py @@ -0,0 +1,6 @@ +"""DSM 6 SYNO.DownloadStation.Schedule data.""" + +DSM_6_DOWNLOAD_STATION_SCHEDULE_CONFIG = { + "data": {"enabled": True, "emule_enabled": False}, + "success": True, +} \ No newline at end of file From 89ba53ab6f60736f7447a1fc7e412787d497c4dc Mon Sep 17 00:00:00 2001 From: sylvaindd Date: Fri, 27 Nov 2020 16:10:09 +0100 Subject: [PATCH 4/4] Prettier Use pre-commit https://github.com/pre-commit/mirrors-prettier v2.2.0 --- .pre-commit-config.yaml | 4 ++-- README.rst | 4 ++-- src/synology_dsm/api/download_station/__init__.py | 10 ++++++---- tests/__init__.py | 2 +- tests/api_data/dsm_6/__init__.py | 6 +++--- .../const_6_download_station_schedule_config.py | 2 +- tests/test_synology_dsm.py | 6 +++--- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b6652b19..78fc7f39 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -45,7 +45,7 @@ repos: language: system types: [text] stages: [commit, push, manual] - - repo: https://github.com/prettier/prettier - rev: 2.1.2 + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v2.2.0 hooks: - id: prettier diff --git a/README.rst b/README.rst index 3e1ecfd0..5d16d625 100644 --- a/README.rst +++ b/README.rst @@ -139,7 +139,7 @@ Download Station usage api = SynologyDSM("", "", "", "") if "SYNO.DownloadStation.Info" in api.apis: - + api.download_station.update_info() api.download_station.get_info() @@ -153,7 +153,7 @@ Download Station usage api.download_station.get_schedule_config() # set_schedule_config(self, enabled: bool = None, emule_enabled: bool = None) api.download_station.set_schedule_config(True, False) - + # The download list will be updated after each of the following functions: # You should have the right on the (default) directory that the download will be saved, or you will get a 403 or 406 error diff --git a/src/synology_dsm/api/download_station/__init__.py b/src/synology_dsm/api/download_station/__init__.py index a7ed3b68..bca12145 100644 --- a/src/synology_dsm/api/download_station/__init__.py +++ b/src/synology_dsm/api/download_station/__init__.py @@ -40,7 +40,7 @@ def update(self): def update_info(self): """Update info about the Download Station instance.""" self._info = self._dsm.get(self.INFO_API_KEY, "GetInfo")["data"] - + def get_info(self): """Return general informations about the Download Station instance.""" return self._info @@ -55,7 +55,9 @@ def get_config(self): def update_schedule_config(self): """Update schedule configuration about the Download Station instance.""" - self._schedule_config = self._dsm.get(self.SCHEDULE_API_KEY, "GetConfig")["data"] + self._schedule_config = self._dsm.get(self.SCHEDULE_API_KEY, "GetConfig")[ + "data" + ] def get_schedule_config(self): """Return schedule configuration about the Download Station instance.""" @@ -64,9 +66,9 @@ def get_schedule_config(self): def set_schedule_config(self, enabled: bool = None, emule_enabled: bool = None): """Set schedule configuration about the Download Station instance.""" config = {} - if enabled != None: + if enabled is not None: config["enabled"] = enabled - if emule_enabled != None: + if emule_enabled is not None: config["emule_enabled"] = emule_enabled res = self._dsm.get(self.SCHEDULE_API_KEY, "SetConfig", config) self.update_schedule_config() diff --git a/tests/__init__.py b/tests/__init__.py index 41e9c1a2..ea1e6f67 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -28,8 +28,8 @@ from .api_data.dsm_6 import DSM_6_CORE_UTILIZATION_ERROR_1055 from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_INFO_CONFIG from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_INFO_INFO -from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_STAT_INFO from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_SCHEDULE_CONFIG +from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_STAT_INFO from .api_data.dsm_6 import DSM_6_DOWNLOAD_STATION_TASK_LIST from .api_data.dsm_6 import DSM_6_DSM_INFORMATION from .api_data.dsm_6 import DSM_6_DSM_NETWORK_2LAN_1PPPOE diff --git a/tests/api_data/dsm_6/__init__.py b/tests/api_data/dsm_6/__init__.py index c6b260c5..3ab4be7f 100644 --- a/tests/api_data/dsm_6/__init__.py +++ b/tests/api_data/dsm_6/__init__.py @@ -18,12 +18,12 @@ from .download_station.const_6_download_station_info import ( DSM_6_DOWNLOAD_STATION_INFO_INFO, ) -from .download_station.const_6_download_station_stat import ( - DSM_6_DOWNLOAD_STATION_STAT_INFO, -) from .download_station.const_6_download_station_schedule_config import ( DSM_6_DOWNLOAD_STATION_SCHEDULE_CONFIG, ) +from .download_station.const_6_download_station_stat import ( + DSM_6_DOWNLOAD_STATION_STAT_INFO, +) from .download_station.const_6_download_station_task import ( DSM_6_DOWNLOAD_STATION_TASK_LIST, ) diff --git a/tests/api_data/dsm_6/download_station/const_6_download_station_schedule_config.py b/tests/api_data/dsm_6/download_station/const_6_download_station_schedule_config.py index 3d6ca5ce..3b7d2479 100644 --- a/tests/api_data/dsm_6/download_station/const_6_download_station_schedule_config.py +++ b/tests/api_data/dsm_6/download_station/const_6_download_station_schedule_config.py @@ -3,4 +3,4 @@ DSM_6_DOWNLOAD_STATION_SCHEDULE_CONFIG = { "data": {"enabled": True, "emule_enabled": False}, "success": True, -} \ No newline at end of file +} diff --git a/tests/test_synology_dsm.py b/tests/test_synology_dsm.py index aa8cb808..892ba68d 100644 --- a/tests/test_synology_dsm.py +++ b/tests/test_synology_dsm.py @@ -877,10 +877,10 @@ def test_download_station(self): self.api.download_station.update_stat() assert self.api.download_station.get_stat()["speed_download"] - + self.api.download_station.update_schedule_config() - assert self.api.download_station.get_schedule_config()["enabled"] is True - assert self.api.download_station.get_schedule_config()["emule_enabled"] is False + assert self.api.download_station.get_schedule_config()["enabled"] + assert not self.api.download_station.get_schedule_config()["emule_enabled"] self.api.download_station.update() assert self.api.download_station.get_all_tasks()