Skip to content

Commit

Permalink
add brands
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaNovg committed Jan 17, 2025
1 parent 07691e3 commit cc28ce5
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 81 deletions.
78 changes: 31 additions & 47 deletions pyfiat/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,25 @@
import base64
import datetime

from dataclasses import dataclass
from requests_auth_aws_sigv4 import AWSSigV4

from .command import Command

AWS_REGION = "eu-west-1"
LOGIN_API_KEY = "3_mOx_J2dRgjXYCdyhchv3b5lhi54eBcdCTX4BI8MORqmZCoQWhA0mV2PTlptLGUQI"
API_KEY = "2wGyL6PHec9o1UeLPYpoYa1SkEWqeBur9bLsi24i"
LOGIN_URL = "https://loginmyuconnect.fiat.com"
TOKEN_URL = "https://authz.sdpr-01.fcagcv.com/v2/cognito/identity/token"
API_URL = "https://channels.sdpr-01.fcagcv.com"
AUTH_API_KEY = "JWRYW7IYhW9v0RqDghQSx4UcRYRILNmc8zAuh5ys"
AUTH_URL = "https://mfa.fcl-01.fcagcv.com"
LOCALE = "de_de"


@dataclass
class Brand:
name: str
AWS_REGION: str = "eu-west-1"
LOGIN_API_KEY = "3_mOx_J2dRgjXYCdyhchv3b5lhi54eBcdCTX4BI8MORqmZCoQWhA0mV2PTlptLGUQI"
API_KEY = "2wGyL6PHec9o1UeLPYpoYa1SkEWqeBur9bLsi24i"
LOGIN_URL = "https://loginmyuconnect.fiat.com"
TOKEN_URL = "https://authz.sdpr-01.fcagcv.com/v2/cognito/identity/token"
API_URL = "https://channels.sdpr-01.fcagcv.com"
AUTH_API_KEY = "JWRYW7IYhW9v0RqDghQSx4UcRYRILNmc8zAuh5ys"
AUTH_URL = "https://mfa.fcl-01.fcagcv.com"
from .brands import Brand


class API:
def __init__(self, email: str, password: str, pin: str, dev_mode: bool = False):
def __init__(self, email: str, password: str, pin: str, brand: Brand, dev_mode: bool = False):
self.email = email
self.password = password
self.pin = pin
self.brand = brand
self.dev_mode = dev_mode

self.uid: str = None
self.aws_auth: AWSSigV4 = None

self.sess = requests.Session()
self.cognito_client = boto3.client(
'cognito-identity', AWS_REGION) if not dev_mode else None
self.cognito_client = None

self.expire_time: datetime.datetime = None

Expand All @@ -58,7 +35,7 @@ def _with_default_params(self, params: dict):
"authMode": "cookie",
"sdkBuild": "12234",
"format": "json",
"APIKey": LOGIN_API_KEY,
"APIKey": self.brand.login_api_key,
}

def _default_aws_headers(self, key: str):
Expand All @@ -74,18 +51,22 @@ def _default_aws_headers(self, key: str):
def login(self):
"""Logs into the Fiat Cloud and caches the auth tokens"""

if self.cognito_client is None:
self.cognito_client = boto3.client(
'cognito-identity', self.brand.region)

r = self.sess.request(
method="GET",
url=LOGIN_URL + "/accounts.webSdkBootstrap",
params={"apiKey": LOGIN_API_KEY}
url=self.brand.login_url + "/accounts.webSdkBootstrap",
params={"apiKey": self.brand.login_api_key}
).json()

if r['statusCode'] != 200:
raise Exception("bootstrap failed")

r = self.sess.request(
method="POST",
url=LOGIN_URL + "/accounts.login",
url=self.brand.login_url + "/accounts.login",
params=self._with_default_params({
"loginID": self.email,
"password": self.password,
Expand All @@ -102,7 +83,7 @@ def login(self):

r = self.sess.request(
method="POST",
url=LOGIN_URL + "/accounts.getJWT",
url=self.brand.login_url + "/accounts.getJWT",
params=self._with_default_params({
"login_token": login_token,
"fields": "profile.firstName,profile.lastName,profile.email,country,locale,data.disclaimerCodeGSDP"
Expand All @@ -114,8 +95,8 @@ def login(self):

r = self.sess.request(
method="POST",
url=TOKEN_URL,
headers=self._default_aws_headers(API_KEY),
url=self.brand.token_url,
headers=self._default_aws_headers(self.brand.api_key),
json={"gigya_token": r['id_token']}
).json()

Expand All @@ -128,7 +109,7 @@ def login(self):

self.aws_auth = AWSSigV4(
'execute-api',
region=AWS_REGION,
region=self.brand.region,
aws_access_key_id=creds['AccessKeyId'],
aws_secret_access_key=creds['SecretKey'],
aws_session_token=creds['SessionToken'],
Expand Down Expand Up @@ -156,9 +137,9 @@ def list_vehicles(self) -> list[dict]:

return self.sess.request(
method="GET",
url=API_URL + f"/v4/accounts/{self.uid}/vehicles",
url=self.brand.api_url + f"/v4/accounts/{self.uid}/vehicles",
headers=self._default_aws_headers(
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 @@ -174,9 +155,9 @@ def get_vehicle(self, vin: str) -> dict:

return self.sess.request(
method="GET",
url=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(
API_KEY) | {"content-type": "application/json"},
self.brand.api_key) | {"content-type": "application/json"},
auth=self.aws_auth,
).json()

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

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

Expand All @@ -209,17 +190,20 @@ def get_vehicle_location(self, vin: str) -> dict:

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

def command(self,
vin: str, cmd: Command):
"""Sends given command to the vehicle with a given VIN"""

if self.dev_mode:
return

data = {
'pin': base64.b64encode(self.pin.encode()).decode(encoding="utf-8"),
}
Expand All @@ -228,9 +212,9 @@ def command(self,

r = self.sess.request(
method="POST",
url=AUTH_URL +
url=self.brand.auth_url +
f"/v1/accounts/{self.uid}/ignite/pin/authenticate",
headers=self._default_aws_headers(AUTH_API_KEY) | {
headers=self._default_aws_headers(self.brand.auth_api_key) | {
"content-type": "application/json"},
auth=self.aws_auth,
json=data,
Expand All @@ -246,10 +230,10 @@ def command(self,

r = self.sess.request(
method="POST",
url=API_URL +
url=self.brand.api_url +
f"/v1/accounts/{self.uid}/vehicles/{vin}/{cmd.url}",
headers=self._default_aws_headers(
API_KEY) | {"content-type": "application/json"},
self.brand.api_key) | {"content-type": "application/json"},
auth=self.aws_auth,
json=data,
).json()
Expand Down
71 changes: 71 additions & 0 deletions pyfiat/brands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from dataclasses import dataclass


@dataclass
class Brand:
name: str
region: str = "eu-west-1"
login_api_key: str = "3_mOx_J2dRgjXYCdyhchv3b5lhi54eBcdCTX4BI8MORqmZCoQWhA0mV2PTlptLGUQI"
api_key: str = "2wGyL6PHec9o1UeLPYpoYa1SkEWqeBur9bLsi24i"
login_url: str = "https://loginmyuconnect.fiat.com"
token_url: str = "https://authz.sdpr-01.fcagcv.com/v2/cognito/identity/token"
api_url: str = "https://channels.sdpr-01.fcagcv.com"
auth_api_key: str = "JWRYW7IYhW9v0RqDghQSx4UcRYRILNmc8zAuh5ys"
auth_url: str = "https://mfa.fcl-01.fcagcv.com"


FIAT_EU = Brand("fiat_eu")

FIAT_US = Brand(
name="fiat_us",
region="us-east-1",
login_api_key="3_etlYkCXNEhz4_KJVYDqnK1CqxQjvJStJMawBohJU2ch3kp30b0QCJtLCzxJ93N-M",
api_key="OgNqp2eAv84oZvMrXPIzP8mR8a6d9bVm1aaH9LqU",
login_url="https://login-us.fiat.com",
token_url="https://authz.sdpr-02.fcagcv.com/v2/cognito/identity/token",
api_url="https://channels.sdpr-02.fcagcv.com",
auth_api_key="JWRYW7IYhW9v0RqDghQSx4UcRYRILNmc8zAuh5ys",
auth_url="https://mfa.fcl-01.fcagcv.com"
)

JEEP_EU = Brand(
name="jeep_eu",
login_api_key="3_ZvJpoiZQ4jT5ACwouBG5D1seGEntHGhlL0JYlZNtj95yERzqpH4fFyIewVMmmK7j",
login_url="https://login.jeep.com"
)

JEEP_US = Brand(
name="jeep_us",
region="us-east-1",
login_api_key="3_5qxvrevRPG7--nEXe6huWdVvF5kV7bmmJcyLdaTJ8A45XUYpaR398QNeHkd7EB1X",
api_key="OgNqp2eAv84oZvMrXPIzP8mR8a6d9bVm1aaH9LqU",
login_url="https://login-us.jeep.com",
token_url="https://authz.sdpr-02.fcagcv.com/v2/cognito/identity/token",
api_url="https://channels.sdpr-02.fcagcv.com",
auth_api_key="fNQO6NjR1N6W0E5A6sTzR3YY4JGbuPv48Nj9aZci",
auth_url="https://mfa.fcl-02.fcagcv.com"
)

DODGE_US = Brand(
name="dodge_us",
region="us-east-1",
login_api_key="3_etlYkCXNEhz4_KJVYDqnK1CqxQjvJStJMawBohJU2ch3kp30b0QCJtLCzxJ93N-M",
api_key="OgNqp2eAv84oZvMrXPIzP8mR8a6d9bVm1aaH9LqU",
login_url="https://login-us.dodge.com",
token_url="https://authz.sdpr-02.fcagcv.com/v2/cognito/identity/token",
api_url="https://channels.sdpr-02.fcagcv.com",
auth_api_key="JWRYW7IYhW9v0RqDghQSx4UcRYRILNmc8zAuh5ys",
auth_url="https://mfa.fcl-01.fcagcv.com"
)

RAM_US = Brand(
name="ram_us",
region="us-east-1",
login_api_key="3_7YjzjoSb7dYtCP5-D6FhPsCciggJFvM14hNPvXN9OsIiV1ujDqa4fNltDJYnHawO",
api_key="OgNqp2eAv84oZvMrXPIzP8mR8a6d9bVm1aaH9LqU",
login_url="https://login-us.ramtrucks.com",
token_url="https://authz.sdpr-02.fcagcv.com/v2/cognito/identity/token",
api_url="https://channels.sdpr-02.fcagcv.com",
auth_api_key="JWRYW7IYhW9v0RqDghQSx4UcRYRILNmc8zAuh5ys",
auth_url="https://mfa.fcl-01.fcagcv.com"
)
Loading

0 comments on commit cc28ce5

Please sign in to comment.