Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions qnapstats/qnap_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ def _execute_get_url(self, url, append_sid=True, **kwargs):
resp = self._session.get(url, timeout=self._timeout, verify=self._verify_ssl)
return self._handle_response(resp, **kwargs)

def _post_url(self, url, data, retry_on_error=False, **kwargs):
"""High-level function for making POST requests."""
self._init_session()

result = self._execute_post_url(url, data, **kwargs)
if (self._session_error or result is None) and retry_on_error:
self._debuglog("Error occured, retrying...")
self._get_url(url, False, **kwargs)

return result

def _execute_post_url(self, url, data, append_sid=True, **kwargs):
"""Low-level function to execute a POST request."""
url = self._base_url + url
Expand Down Expand Up @@ -376,3 +387,17 @@ def get_storage_information_on_external_device(self):
return None

return disk_vol

def put_in_sleep_mode(self):
"""Put the qnap in sleep mode."""
data = {
"subfunc": "power_mgmt",
"apply": "s3",
}
# It is expected to end in read timeout
try:
resp = self._post_url("sys/sysRequest.cgi", data=data)
except requests.exceptions.ReadTimeout:
return None

return resp
11 changes: 10 additions & 1 deletion tests/test-models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import os
import qnapstats
import requests
import responses


Expand Down Expand Up @@ -92,13 +93,19 @@ def add_mock_responses(rsps, directory):
status=200,
content_type='text/xml')

# Sleep mode succeeds but fails in timeout
rsps.add(responses.POST,
'http://localhost:8080/cgi-bin/sys/sysRequest.cgi',
body=requests.exceptions.ReadTimeout(),
content_type='text/xml')


def file_get_contents(directory, file):
file = os.path.join(response_directory, directory, file)
if not os.path.exists(file):
return None

with open(file, 'r') as myfile:
with open(file, 'r', encoding='utf-8') as myfile:
return myfile.read()


Expand Down Expand Up @@ -130,3 +137,5 @@ def file_get_contents(directory, file):
firmwareupdate = file_get_contents(model_directory, 'firmwareupdate.json')
if firmwareupdate is not None:
assert json.dumps(qnap.get_firmware_update(), sort_keys=True) == firmwareupdate

assert qnap.put_in_sleep_mode() is None
Loading