Skip to content

Commit

Permalink
Merge pull request #106 from lostfields/chore/avoid-date-to-string
Browse files Browse the repository at this point in the history
chore/Avoid date to string
  • Loading branch information
lostfields authored Nov 9, 2024
2 parents 28640fd + 4d4a43f commit edcb2ff
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pcomfortcloud/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _check_token_is_valid(self):
(now_unix > self._token["unix_timestamp_token_received"] + self._token["expires_in_sec"]):

if self._raw:
print("--- Token is invalid")
print("--- Token is expired")
return False

if self._raw:
Expand All @@ -83,11 +83,15 @@ def _check_token_is_valid(self):
print("--- Token is invalid")
return False

def _get_api_key(self, timestamp, token):
def _get_api_key(self, timestamp: datetime.datetime, token: string):
try:
date = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
timestamp_ms = str(int(date.replace(tzinfo=datetime.timezone.utc).timestamp() * 1000))

date = datetime.datetime(
timestamp.year, timestamp.month, timestamp.day,
timestamp.hour, timestamp.minute, timestamp.second,
0, datetime.timezone.utc
)
timestamp_ms = str(int(date.timestamp() * 1000))

components = [
'Comfort Cloud'.encode('utf-8'),
'521325fb2dd486bf4831b47644317fca'.encode('utf-8'),
Expand Down Expand Up @@ -259,17 +263,16 @@ def _get_new_token(self):
# RETRIEVE ACC_CLIENT_ID
# ------------------------------------------------------------------
now = datetime.datetime.now()
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
response = requests.post(
f'{constants.BASE_PATH_ACC}/auth/v2/login',
headers={
"Content-Type": "application/json;charset=utf-8",
"User-Agent": "G-RAC",
"x-app-name": "Comfort Cloud",
"x-app-timestamp": timestamp,
"x-app-timestamp": now.strftime("%Y-%m-%d %H:%M:%S"),
"x-app-type": "1",
"x-app-version": self._app_version,
"x-cfc-api-key": self._get_api_key(timestamp, token_response["access_token"]),
"x-cfc-api-key": self._get_api_key(now, token_response["access_token"]),
"x-user-authorization-v2": "Bearer " + token_response["access_token"]
},
json={
Expand Down Expand Up @@ -357,15 +360,14 @@ def _refresh_token(self):

def _get_header_for_api_calls(self, no_client_id=False):
now = datetime.datetime.now()
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
return {
"Content-Type": "application/json;charset=utf-8",
"x-app-name": "Comfort Cloud",
"user-agent": "G-RAC",
"x-app-timestamp": timestamp,
"x-app-timestamp": now.strftime("%Y-%m-%d %H:%M:%S"),
"x-app-type": "1",
"x-app-version": self._app_version,
"x-cfc-api-key": self._get_api_key(timestamp, self._token["access_token"]),
"x-cfc-api-key": self._get_api_key(now, self._token["access_token"]),
"x-client-id": self._token["acc_client_id"],
"x-user-authorization-v2": "Bearer " + self._token["access_token"]
}
Expand Down

0 comments on commit edcb2ff

Please sign in to comment.