Skip to content

Commit

Permalink
Tidier weathercloud response handling
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Aug 30, 2018
1 parent 2d5166d commit 1ca6652
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/pywws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '18.8.0'
_release = '1614'
_commit = '8830155'
_release = '1615'
_commit = '2d5166d'
27 changes: 12 additions & 15 deletions src/pywws/service/weathercloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self, context, check_params=True):
def session(self):
with requests.Session() as session:
yield session

def prepare_data(self, data):
prepared_data = super(ToService, self).prepare_data(data)
for key in ('tempin', 'temp', 'chill', 'dewin', 'dew',
Expand All @@ -112,27 +112,24 @@ def valid_data(self, data):
return any([data[x] is not None for x in (
'wind_dir', 'wind_ave', 'wind_gust', 'hum_out', 'temp_out',
'temp_in', 'hum_in', 'rel_pressure')])


errors = {
'400': 'bad request',
'401': 'invalid wid or key',
'429': 'too frequent data',
}

def upload_data(self, session, prepared_data={}, live=False):
url = 'http://api.weathercloud.net/v01/set'
try:
rsp = session.get(url, params=prepared_data, timeout=60)
except Exception as ex:
return False, str(ex)
text = rsp.text.strip()
if rsp.text.strip() == '400':
# WeatherCloud server uses 400 to signal bad request
return False, 'bad request: "{:s}"'.format(rsp.text.strip())
if rsp.text.strip() == '401':
# WeatherCloud server uses 401 to signal invalid wid or key
return False, 'invalid wid or key: "{:s}"'.format(rsp.text.strip())
if rsp.text.strip() == '429':
# WeatherCloud server uses 429 to signal too frequent data upload
return False, 'too frequent data: "{:s}"'.format(rsp.text.strip())
if rsp.text.strip() != '200':
return False, 'unknown error: "{:s}"'.format(rsp.text.strip())
if rsp.text.strip() == '200':
return True, 'upload successful: "{:s}"'.format(rsp.text.strip())
if text in self.errors:
return False, '{} ({})'.format(self.errors[text], text)
if text != '200':
return False, 'unknown error ({})'.format(text)
return True, 'OK'


Expand Down

0 comments on commit 1ca6652

Please sign in to comment.