Skip to content

Commit d84ffe4

Browse files
authored
Merge pull request #19 from PimDoos/dev
Add error checking to API wrapper (fix #16)
2 parents bda381b + 6467246 commit d84ffe4

File tree

1 file changed

+56
-52
lines changed

1 file changed

+56
-52
lines changed

custom_components/kia_connect/KiaConnectApi.py

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,60 +26,79 @@ def __init__(
2626
self.user = {}
2727
self.vehicle = {}
2828
self.session = aiohttp.ClientSession()
29+
30+
async def post(self, path, json):
31+
url = self.api_base_uri + path
32+
async with self.session.post(url, json=json) as response:
33+
if(response.status == 200):
34+
try:
35+
api_response = await response.json()
36+
if api_response["isSuccess"]:
37+
return api_response
38+
else:
39+
return None
40+
except:
41+
return None
42+
else:
43+
return None
44+
45+
async def get(self, path):
46+
url = self.api_base_uri + path
47+
async with self.session.get(url) as response:
48+
if(response.status == 200):
49+
try:
50+
api_response = await response.json()
51+
if api_response["isSuccess"]:
52+
return api_response
53+
else:
54+
return None
55+
except:
56+
return None
57+
else:
58+
return None
2959

3060
async def login(self) -> bool:
3161
"""Authenticates the API session using the credentials specified in the constructor"""
3262

33-
payload = {
63+
credentials = {
3464
"username": self.username,
3565
"password": self.password
3666
}
37-
url = self.api_base_uri + API_PATH_USER_LOGIN
38-
async with self.session.post(url, json=payload) as response:
39-
if(response.status == 200):
40-
api_response = await response.json()
41-
if api_response["isSuccess"]:
42-
self.user = await self.get_user()
43-
return True
44-
else:
45-
return False
46-
else:
67+
response = await self.post(API_PATH_USER_LOGIN, credentials)
68+
if response != None:
69+
try:
70+
self.user = await self.get_user()
71+
return True
72+
except:
4773
return False
74+
else:
75+
return False
4876

4977
async def logout(self) -> bool:
5078
"""Deauthenticates the API session"""
51-
url = self.api_base_uri + API_PATH_USER_LOGOUT
52-
async with self.session.get(url) as response:
53-
if(response.status == 200):
54-
api_response = await response.json()
55-
if api_response["isSuccess"]:
56-
return True
57-
else:
58-
return False
59-
else:
60-
return False
79+
response = await self.post(API_PATH_USER_LOGOUT)
80+
return response != None
6181

6282
async def is_logged_in(self) -> bool:
6383
"""Checks whether the session is currently authenticated"""
64-
url = self.api_base_uri + API_PATH_USER_LOGGED_IN
65-
async with self.session.get(url) as response:
66-
if(response.status == 200):
67-
api_response = await response.json()
68-
return api_response["isSuccess"]
69-
else:
70-
return False
84+
response = await self.get(API_PATH_USER_LOGGED_IN)
85+
return response != None
7186

7287
async def get_user(self):
7388
"""Get the currently logged in user"""
74-
url = self.api_base_uri + API_PATH_USER
75-
async with self.session.get(url) as response:
76-
if response.status == 200:
77-
api_response = await response.json()
78-
if api_response["isSuccess"]:
79-
user = api_response["data"]
80-
return user
81-
else:
82-
return None
89+
response: dict = await self.get(API_PATH_USER)
90+
if response != None:
91+
return response.get("data", None)
92+
else:
93+
return None
94+
95+
async def get_vehicle_status(self, vehicle_id: int):
96+
"""Get the vehicle status"""
97+
response: dict = await self.get(API_PATH_VEHICLE_STATUS.format(id=vehicle_id))
98+
if response != None:
99+
return response.get("data", None)
100+
else:
101+
return None
83102

84103
async def get_vehicle_ids(self):
85104
"""Get a list of vehicle IDs associated with the logged in user"""
@@ -107,18 +126,3 @@ async def get_vehicle_info(self, vehicle_id: int):
107126
else:
108127
return None
109128

110-
111-
async def get_vehicle_status(self, vehicle_id: int):
112-
"""Get the vehicle status"""
113-
url = self.api_base_uri + API_PATH_VEHICLE_STATUS.format(id=vehicle_id)
114-
async with self.session.get(url) as response:
115-
if response.status == 200:
116-
api_response = await response.json()
117-
if api_response["isSuccess"]:
118-
119-
vehicle = api_response["data"]
120-
return vehicle
121-
else:
122-
return None
123-
else:
124-
return None

0 commit comments

Comments
 (0)