1010import json
1111
1212from .exceptions import *
13+ from .models import *
14+
15+ API_URL = "https://tahomalink.com/enduser-mobile-web/enduserAPI/" # /doc for API doc
1316
14- API_URL = 'https://tahomalink.com/enduser-mobile-web/enduserAPI/' # /doc for API doc
1517
1618class TahomaClient (object ):
1719 """ Interface class for the Tahoma API """
1820
19- def __init__ (self , username , password ):
21+ def __init__ (self , username , password , api_url = API_URL ):
2022 """
2123 Constructor
2224
@@ -26,53 +28,52 @@ def __init__(self, username, password):
2628
2729 self .username = username
2830 self .password = password
29-
31+ self . api_url = api_url
3032
3133 self ._devices = None
3234
33-
3435 self .__roles = []
3536
3637 async def login (self ):
37-
38- payload = {
39- 'userId' : self .username ,
40- 'userPassword' : self .password
41- }
38+
39+ payload = {"userId" : self .username , "userPassword" : self .password }
4240
4341 async with aiohttp .ClientSession () as session :
44- async with session .post (API_URL + ' login' , data = payload ) as response :
42+ async with session .post (self . api_url + " login" , data = payload ) as response :
4543
4644 result = await response .json ()
4745
4846 # 401
4947 # {'errorCode': 'AUTHENTICATION_ERROR', 'error': 'Bad credentials'}
5048 # {'errorCode': 'AUTHENTICATION_ERROR', 'error': 'Your setup cannot be accessed through this application'}
5149 if response .status == 401 :
52- if result ['errorCode' ] == 'AUTHENTICATION_ERROR' :
50+ if result ["errorCode" ] == "AUTHENTICATION_ERROR" :
51+
52+ if "Too many requests" in result ["error" ]:
53+ print (result ["error" ])
5354
54- if 'Too many requests' in result ['error' ]:
55- print (result ['error' ])
55+ if (
56+ "Your setup cannot be accessed through this application"
57+ in result ["error" ]
58+ ):
59+ print (result ["error" ])
5660
57- if 'Your setup cannot be accessed through this application' in result [' error' ]:
58- print (result [' error' ])
61+ if "Bad credentials" in result [" error" ]:
62+ print (result [" error" ])
5963
60- if 'Bad credentials' in result ['error' ]:
61- print (result ['error' ])
64+ print (result ["error" ])
6265
63- print ( result [ ' error' ])
66+ return False # todo throw error
6467
65- return False # todo throw error
66-
6768 # 401
6869 # {'errorCode': 'AUTHENTICATION_ERROR', 'error': 'Too many requests, try again later : login with xxx@xxx.tld'}
6970 # TODO Add retry logic on too many requests + for debug, log requests + timespans
70-
71+
7172 # 200
7273 # {'success': True, 'roles': [{'name': 'ENDUSER'}]}
7374 if response .status == 200 :
74- if result [' success' ] == True :
75- self .__roles = result [' roles' ]
75+ if result [" success" ] == True :
76+ self .__roles = result [" roles" ]
7677 self .__cookies = response .cookies
7778
7879 return True
@@ -81,24 +82,26 @@ async def login(self):
8182 print (response .status )
8283 print (result )
8384
84- async def get_devices (self , refresh = False ):
85+ async def get_devices (self , refresh = False ) -> List [Device ]:
86+
87+ if self ._devices and refresh == False :
88+ return self ._devices
8589
86- if self . _devices is None or refresh == True :
90+ cookies = self . __cookies
8791
88- cookies = self .__cookies
92+ # TODO add retry logic for unauthorized?
93+ async with aiohttp .ClientSession () as session :
94+ async with session .get (
95+ self .api_url + "setup/devices" , cookies = cookies
96+ ) as response :
8997
90- async with aiohttp .ClientSession () as session :
91- async with session .get (API_URL + 'setup/devices' , cookies = cookies ) as response :
98+ result = await response .json ()
9299
93- result = await response . json ()
100+ # for device in result.items ()
94101
95- if (response .status is 200 ):
96- self ._devices = result
102+ if response .status is 200 :
103+ devices = [Device (** d ) for d in result ]
104+ self ._devices = devices
97105
98- return result
99-
100- # TODO add retry logic for unauthorized?
106+ return devices
101107
102- else :
103- return []
104- # TODO Save cookies
0 commit comments