Skip to content
Snippets Groups Projects
Commit 0a190650 authored by jkerdreu's avatar jkerdreu
Browse files

Fix try/except for request

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/trunk@2031 b32b6428-25c9-4566-ad07-03861ab6144f
parent 28f78426
No related branches found
No related tags found
No related merge requests found
......@@ -72,10 +72,9 @@ class GW:
m.update_sensors(attr)
def readweatherstation(self):
try:
netatmo_data = netatmo.API(self.cfg).get_data()
except:
logger.warn("Unable to connect to NA server")
netatmo_data = netatmo.API(self.cfg).get_data()
if netatmo_data == None:
logger.warn("Unable to read weatherstation")
return
main_indoor = netatmo_data["devices"][0] # dic
......
......@@ -9,42 +9,47 @@ logger = logging.getLogger(__name__)
class API:
""" NetAtmo API """
def __init__(self,configfile):
self.configfile = configfile
self.configfile = configfile
def get_token(self):
""" connect to netatmo cloud, return access token """
##read config
cfg = self.configfile['config']
payload = {
'grant_type' : cfg['grant_type'],
'client_id' : cfg['client_id'],
'client_secret' : cfg['client_secret'],
'password' : cfg['password'],
'username' : cfg['username'],
'scope' : cfg['scope']
}
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
response.raise_for_status()
access_token=response.json()["access_token"]
return access_token
except requests.exceptions.HTTPError as error:
logger.warn(error.response.status_code, error.response.text)
""" connect to netatmo cloud, return access token """
##read config
cfg = self.configfile['config']
payload = {
'grant_type' : cfg['grant_type'],
'client_id' : cfg['client_id'],
'client_secret' : cfg['client_secret'],
'password' : cfg['password'],
'username' : cfg['username'],
'scope' : cfg['scope']
}
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
response.raise_for_status()
access_token=response.json()["access_token"]
return access_token
except requests.exceptions.RequestException as error:
logger.warn(error.response.status_code, error.response.text)
return None
def get_data(self): #Fonction which get the information from netatmo API by using a token
token=self.get_token()
if token == None:
return None
cfg = self.configfile['config']
params = {
'access_token': token ,
'device_id': cfg['device_id']
}
'access_token': token ,
'device_id': cfg['device_id']
}
try:
response = requests.post("https://api.netatmo.com/api/getstationsdata", params=params)
response.raise_for_status()
data = response.json()["body"]
return data
except requests.exceptions.HTTPError as error:
except requests.exceptions.RequestException as error:
logger.error(error.response.status_code, error.response.text)
return None
class NAModule(object): # NWS: netatmo weather station
......@@ -154,10 +159,9 @@ class ConfigParser: # netatmo weather station config
self.cfg['devices'].inline_comments[k]='This device is removed !!!'
def get_modules(self):
try:
data = data=API(self.cfg).get_data()
except:
logger.warn("Unable to connect to NA server")
data = data=API(self.cfg).get_data()
if data == None:
logger.warning('Unable to fetch module list')
return None
# NAMain is the first devices
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment