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
10 changes: 8 additions & 2 deletions hydrawiser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import time
from hydrawiser.helpers import customer_details, status_schedule, set_zones
from requests.exceptions import ConnectTimeout, HTTPError


class Hydrawiser():
Expand Down Expand Up @@ -48,8 +49,13 @@ def update_controller_info(self):
"""

# Read the controller information.
self.controller_info = customer_details(self._user_token)
self.controller_status = status_schedule(self._user_token)
try:
self.controller_info = customer_details(self._user_token)
self.controller_status = status_schedule(self._user_token)
except HTTPError as err:
# Handling the newly introduced rate limit
if err.response.status_code == 429:
raise HTTPError(err.response.text)

if self.controller_info is None or self.controller_status is None:
return False
Expand Down
8 changes: 5 additions & 3 deletions hydrawiser/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def status_schedule(token):
:rtype: string or None
"""

url = 'https://app.hydrawise.com/api/v1/statusschedule.php'
url = 'https://api.hydrawise.com/api/v1/statusschedule.php'

payload = {
'api_key': token}

get_response = requests.get(url, params=payload, timeout=REQUESTS_TIMEOUT)
get_response.raise_for_status()

if get_response.status_code == 200 and \
'error_msg' not in get_response.json():
Expand All @@ -45,13 +46,14 @@ def customer_details(token):
:rtype: string or None.
"""

url = 'https://app.hydrawise.com/api/v1/customerdetails.php'
url = 'https://api.hydrawise.com/api/v1/customerdetails.php'

payload = {
'api_key': token,
'type': 'controllers'}

get_response = requests.get(url, params=payload, timeout=REQUESTS_TIMEOUT)
get_response.raise_for_status()

if get_response.status_code == 200 and \
'error_msg' not in get_response.json():
Expand Down Expand Up @@ -116,7 +118,7 @@ def set_zones(token, action, relay=None, time=None):
if action in ['stop', 'run', 'suspend'] and relay is None:
return None

get_response = requests.get('https://app.hydrawise.com/api/v1/'
get_response = requests.get('https://api.hydrawise.com/api/v1/'
'setzone.php?'
'&api_key={}'
'&action={}{}{}{}'
Expand Down
20 changes: 10 additions & 10 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_status_schedule():
with requests_mock.Mocker() as m:

# Test a valid api_key.
m.get('https://app.hydrawise.com/api/v1/statusschedule.php?'
m.get('https://api.hydrawise.com/api/v1/statusschedule.php?'
'api_key={}'
.format(GOOD_API_KEY),
text=good_string)
Expand All @@ -19,7 +19,7 @@ def test_status_schedule():
assert return_value is not None

# Test an invalid api_key.
m.get('https://app.hydrawise.com/api/v1/statusschedule.php?'
m.get('https://api.hydrawise.com/api/v1/statusschedule.php?'
'api_key={}'
.format(BAD_API_KEY),
text=unauthorized_string)
Expand All @@ -33,7 +33,7 @@ def test_customer_details():
with requests_mock.Mocker() as m:

# Test a valid api_key.
m.get('https://app.hydrawise.com/api/v1/customerdetails.php?'
m.get('https://api.hydrawise.com/api/v1/customerdetails.php?'
'api_key={}'
'&type={}'
.format(GOOD_API_KEY, 'controllers'),
Expand All @@ -43,7 +43,7 @@ def test_customer_details():
assert return_value is not None

# Test an invalid api_key.
m.get('https://app.hydrawise.com/api/v1/customerdetails.php?'
m.get('https://api.hydrawise.com/api/v1/customerdetails.php?'
'api_key={}'
'&type={}'
.format(BAD_API_KEY, 'controllers'),
Expand All @@ -67,7 +67,7 @@ def test_set_zones():
error_string = '{"message": "not ok", "message_type": "error"}'

# Test positive use of the stop command.
m.get('https://app.hydrawise.com/api/v1/setzone.php?'
m.get('https://api.hydrawise.com/api/v1/setzone.php?'
'api_key={}'
'&action={}'
'&relay_id={}'
Expand All @@ -78,7 +78,7 @@ def test_set_zones():
assert return_value is not None

# Test negative use of the stop command.
m.get('https://app.hydrawise.com/api/v1/setzone.php?'
m.get('https://api.hydrawise.com/api/v1/setzone.php?'
'api_key={}'
'&action={}'
.format(GOOD_API_KEY, 'stop'),
Expand All @@ -88,7 +88,7 @@ def test_set_zones():
assert return_value is None

# Test positive use of stopall.
m.get('https://app.hydrawise.com/api/v1/setzone.php?'
m.get('https://api.hydrawise.com/api/v1/setzone.php?'
'api_key={}'
'&action={}'
.format(GOOD_API_KEY, 'stopall'),
Expand All @@ -98,7 +98,7 @@ def test_set_zones():
assert return_value is not None

# Test positive use of run.
m.get('https://app.hydrawise.com/api/v1/setzone.php?'
m.get('https://api.hydrawise.com/api/v1/setzone.php?'
'api_key={}'
'&action={}'
'&relay_id={}'
Expand All @@ -118,7 +118,7 @@ def test_set_zones():
assert return_value is None

# Test positive use of runall.
m.get('https://app.hydrawise.com/api/v1/setzone.php?'
m.get('https://api.hydrawise.com/api/v1/setzone.php?'
'api_key={}'
'&action={}'
'&period_id={}'
Expand All @@ -138,7 +138,7 @@ def test_set_zones():
assert return_value is None

# Test bad api_key.
m.get('https://app.hydrawise.com/api/v1/setzone.php?'
m.get('https://api.hydrawise.com/api/v1/setzone.php?'
'api_key={}'
'&action={}'
'&period_id={}'
Expand Down