|
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | 4 | import urllib.parse |
| 5 | +from json import JSONDecodeError |
5 | 6 | from types import TracebackType |
6 | 7 | from typing import Any, Dict, List, Optional, Type, Union |
7 | 8 |
|
8 | 9 | import humps |
9 | 10 | from aiohttp import ClientResponse, ClientSession |
10 | 11 |
|
11 | | -from pyhoma.exceptions import BadCredentialsException, TooManyRequestsException |
| 12 | +from pyhoma.exceptions import ( |
| 13 | + BadCredentialsException, |
| 14 | + NotAuthenticatedException, |
| 15 | + TooManyRequestsException, |
| 16 | +) |
12 | 17 | from pyhoma.models import Command, Device, Event, Execution, Scenario, State |
13 | 18 |
|
14 | 19 | JSON = Union[Dict[str, Any], List[Dict[str, Any]]] |
@@ -217,28 +222,29 @@ async def check_response(response: ClientResponse) -> None: |
217 | 222 | """ Check the response returned by the TaHoma API""" |
218 | 223 | if response.status == 200: |
219 | 224 | return |
220 | | - # 401 |
221 | | - # {'errorCode': 'AUTHENTICATION_ERROR', |
222 | | - # 'error': 'Too many requests, try again later : login with xxx@xxx.tld'} |
223 | | - # 'error': 'Bad credentials'} |
224 | | - # 'error': 'Your setup cannot be accessed through this application'} |
225 | | - result = await response.json() |
226 | | - if response.status == 401: |
227 | | - if result.get("errorCode") == "AUTHENTICATION_ERROR": |
228 | | - message = result.get("error") |
229 | 225 |
|
230 | | - if "Too many requests" in message: |
231 | | - raise TooManyRequestsException(message) |
| 226 | + try: |
| 227 | + result = await response.json(content_type=None) |
| 228 | + except JSONDecodeError: |
| 229 | + result = await response.text() |
| 230 | + raise Exception( |
| 231 | + f"Unknown error while requesting {response.url}. {response.status} - {result}" |
| 232 | + ) |
232 | 233 |
|
233 | | - if "Your setup cannot be accessed through this application" in message: |
234 | | - raise Exception(message) |
| 234 | + if result.get("errorCode"): |
| 235 | + message = result.get("error") |
235 | 236 |
|
236 | | - if "Bad credentials" in message: |
237 | | - raise BadCredentialsException(message) |
| 237 | + # {"errorCode": "AUTHENTICATION_ERROR", |
| 238 | + # "error": "Too many requests, try again later : login with xxx@xxx.tld"} |
| 239 | + if "Too many requests" in message: |
| 240 | + raise TooManyRequestsException(message) |
238 | 241 |
|
239 | | - raise Exception(message) |
| 242 | + # {"errorCode": "AUTHENTICATION_ERROR", "error": "Bad credentials"} |
| 243 | + if message == "Bad credentials": |
| 244 | + raise BadCredentialsException(message) |
240 | 245 |
|
241 | | - if 400 < response.status < 500: |
242 | | - message = result.get("error") |
| 246 | + # {"errorCode": "RESOURCE_ACCESS_DENIED", "error": "Not authenticated"} |
| 247 | + if message == "Not authenticated": |
| 248 | + raise NotAuthenticatedException(message) |
243 | 249 |
|
244 | | - raise Exception(message) |
| 250 | + raise Exception(message if message else result) |
0 commit comments