diff --git a/packages/modules/vehicles/polestar/api.py b/packages/modules/vehicles/polestar/api.py index 47d48935c6..464c701e03 100755 --- a/packages/modules/vehicles/polestar/api.py +++ b/packages/modules/vehicles/polestar/api.py @@ -1,5 +1,5 @@ import logging -import requests +from modules.common import req import time from modules.vehicles.polestar.auth import PolestarAuth from modules.common.component_state import CarState @@ -13,13 +13,12 @@ class PolestarApi: def __init__(self, username: str, password: str, vin: str) -> None: self.auth = PolestarAuth(username, password, vin) self.vin = vin - self.client_session = requests.session() + self.client_session = req.get_http_session() def query_params(self, params: dict, url='https://pc-api.polestar.com/eu-north-1/mystar-v2/') -> dict or None: access_token = self.auth.get_auth_token() if access_token is None: - log.error("query_params:not yet authenticated") - return None + raise Exception("query_params error:could not get auth token") headers = { "Content-Type": "application/json", @@ -29,22 +28,19 @@ def query_params(self, params: dict, url='https://pc-api.polestar.com/eu-north-1 log.info("query_params:%s", params['query']) try: result = self.client_session.get(url=url, params=params, headers=headers) - except requests.RequestException as e: - log.error("query_params:http error:%s", e) - return None - - if result.status_code == 401: - self.auth.delete_token() - raise Exception("query_params error: get response %d: unauthorized Exception", result.status_code) - if result.status_code != 200: - raise Exception("query_params error: get response %d: %s", result.status_code, result.text) + except Exception as e: + if result.status_code == 401: + self.auth.delete_token() + if self.auth.access_token is not None: + # if we got an access code but the query failed, VIN could be wrong, so let`s check it + self.check_vin() + raise e result_data = result.json() if result_data.get('errors'): error_message = result_data['errors'][0]['message'] raise Exception("query_params error: %s", error_message) - log.debug(result_data) return result_data def get_battery_data(self) -> dict or None: @@ -57,13 +53,7 @@ def get_battery_data(self) -> dict or None: result = self.query_params(params) - if result is not None and result['data'] is not None and result['data']['getBatteryData'] is not None \ - and result['data']['getBatteryData']['batteryChargeLevelPercentage'] is not None: - return result['data']['getBatteryData'] - elif self.auth.access_token is not None: - # if we got an access code but the query failed, VIN could be wrong, so let`s check it - self.check_vin() - return None + return result['data']['getBatteryData'] def check_vin(self) -> None: # get Vehicle Data @@ -80,6 +70,7 @@ def check_vin(self) -> None: if len(cars) == 0: raise Exception("Es konnten keine Fahrzeuge im Account gefunden werden. Bitte in den Einstellungen " + "prüfen, ob der Besitzer-Account des Polestars eingetragen ist.") + for i in range(0, len(cars)): if cars[i]['vin'] == self.vin: pass