Skip to content

Commit

Permalink
rework API, use backup token URL if the main is down
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaNovg committed Jan 31, 2025
1 parent 52124c0 commit d08d2e9
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 152 deletions.
72 changes: 45 additions & 27 deletions pyfiat/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,22 @@ def login(self):
if r['statusCode'] != 200:
raise Exception(f"unable to obtain JWT: {r}")

r: dict = self.sess.request(
method="POST",
url=self.brand.token_url,
headers=self._default_aws_headers(
self.brand.api_key) | {"content-type": "application/json"},
json={"gigya_token": r['id_token']}
)
exc = None
for url in self.brand.token_url:
try:
r = self.sess.request(
method="POST",
url=url,
headers=self._default_aws_headers(
self.brand.api.key) | {"content-type": "application/json"},
json={"gigya_token": r['id_token']}
)
except Exception as e:
exc = e
else:
break
else:
raise Exception(f"unable to obtain token: {exc}")

r.raise_for_status()
r = r.json()
Expand Down Expand Up @@ -170,9 +179,9 @@ def list_vehicles(self) -> list[dict]:

return self.sess.request(
method="GET",
url=self.brand.api_url + f"/v4/accounts/{self.uid}/vehicles",
url=self.brand.api.url + f"/v4/accounts/{self.uid}/vehicles",
headers=self._default_aws_headers(
self.brand.api_key) | {"content-type": "application/json"},
self.brand.api.key) | {"content-type": "application/json"},
params={"stage": "ALL"},
auth=self.aws_auth,
).json()['vehicles']
Expand All @@ -188,9 +197,9 @@ def get_vehicle(self, vin: str) -> dict:

return self.sess.request(
method="GET",
url=self.brand.api_url + f"/v2/accounts/{self.uid}/vehicles/{vin}/status",
url=self.brand.api.url + f"/v2/accounts/{self.uid}/vehicles/{vin}/status",
headers=self._default_aws_headers(
self.brand.api_key) | {"content-type": "application/json"},
self.brand.api.key) | {"content-type": "application/json"},
auth=self.aws_auth,
).json()

Expand All @@ -205,10 +214,10 @@ def get_vehicle_status(self, vin: str) -> dict:

return self.sess.request(
method="GET",
url=self.brand.api_url +
url=self.brand.api.url +
f"/v1/accounts/{self.uid}/vehicles/{vin}/remote/status",
headers=self._default_aws_headers(
self.brand.api_key) | {"content-type": "application/json"},
self.brand.api.key) | {"content-type": "application/json"},
auth=self.aws_auth,
).json()

Expand All @@ -223,10 +232,10 @@ def get_vehicle_location(self, vin: str) -> dict:

return self.sess.request(
method="GET",
url=self.brand.api_url +
url=self.brand.api.url +
f"/v1/accounts/{self.uid}/vehicles/{vin}/location/lastknown",
headers=self._default_aws_headers(
self.brand.api_key) | {"content-type": "application/json"},
self.brand.api.key) | {"content-type": "application/json"},
auth=self.aws_auth,
).json()

Expand All @@ -243,18 +252,27 @@ def command(self,

self._refresh_token_if_needed()

r = self.sess.request(
method="POST",
url=self.brand.auth_url +
f"/v1/accounts/{self.uid}/ignite/pin/authenticate",
headers=self._default_aws_headers(self.brand.auth_api_key) | {
"content-type": "application/json"},
auth=self.aws_auth,
json=data,
).json()
exc = None
for auth in self.brand.auth:
try:
r = self.sess.request(
method="POST",
url=auth.url +
f"/v1/accounts/{self.uid}/ignite/pin/authenticate",
headers=self._default_aws_headers(auth.token) | {
"content-type": "application/json"},
auth=self.aws_auth,
json=data,
).json()
except Exception as e:
exc = e
else:
break
else:
raise Exception(f"Authentication failed: {exc}")

if not 'token' in r:
raise Exception("authentication failed")
raise Exception("authentication failed: no token found")

data = {
"command": cmd.name,
Expand All @@ -263,10 +281,10 @@ def command(self,

r = self.sess.request(
method="POST",
url=self.brand.api_url +
url=self.brand.api.url +
f"/v1/accounts/{self.uid}/vehicles/{vin}/{cmd.url}",
headers=self._default_aws_headers(
self.brand.api_key) | {"content-type": "application/json"},
self.brand.api.key) | {"content-type": "application/json"},
auth=self.aws_auth,
json=data,
).json()
Expand Down
Loading

0 comments on commit d08d2e9

Please sign in to comment.