Skip to content

Commit edcb2ff

Browse files
authored
Merge pull request #106 from lostfields/chore/avoid-date-to-string
chore/Avoid date to string
2 parents 28640fd + 4d4a43f commit edcb2ff

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

pcomfortcloud/authentication.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _check_token_is_valid(self):
7272
(now_unix > self._token["unix_timestamp_token_received"] + self._token["expires_in_sec"]):
7373

7474
if self._raw:
75-
print("--- Token is invalid")
75+
print("--- Token is expired")
7676
return False
7777

7878
if self._raw:
@@ -83,11 +83,15 @@ def _check_token_is_valid(self):
8383
print("--- Token is invalid")
8484
return False
8585

86-
def _get_api_key(self, timestamp, token):
86+
def _get_api_key(self, timestamp: datetime.datetime, token: string):
8787
try:
88-
date = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
89-
timestamp_ms = str(int(date.replace(tzinfo=datetime.timezone.utc).timestamp() * 1000))
90-
88+
date = datetime.datetime(
89+
timestamp.year, timestamp.month, timestamp.day,
90+
timestamp.hour, timestamp.minute, timestamp.second,
91+
0, datetime.timezone.utc
92+
)
93+
timestamp_ms = str(int(date.timestamp() * 1000))
94+
9195
components = [
9296
'Comfort Cloud'.encode('utf-8'),
9397
'521325fb2dd486bf4831b47644317fca'.encode('utf-8'),
@@ -259,17 +263,16 @@ def _get_new_token(self):
259263
# RETRIEVE ACC_CLIENT_ID
260264
# ------------------------------------------------------------------
261265
now = datetime.datetime.now()
262-
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
263266
response = requests.post(
264267
f'{constants.BASE_PATH_ACC}/auth/v2/login',
265268
headers={
266269
"Content-Type": "application/json;charset=utf-8",
267270
"User-Agent": "G-RAC",
268271
"x-app-name": "Comfort Cloud",
269-
"x-app-timestamp": timestamp,
272+
"x-app-timestamp": now.strftime("%Y-%m-%d %H:%M:%S"),
270273
"x-app-type": "1",
271274
"x-app-version": self._app_version,
272-
"x-cfc-api-key": self._get_api_key(timestamp, token_response["access_token"]),
275+
"x-cfc-api-key": self._get_api_key(now, token_response["access_token"]),
273276
"x-user-authorization-v2": "Bearer " + token_response["access_token"]
274277
},
275278
json={
@@ -357,15 +360,14 @@ def _refresh_token(self):
357360

358361
def _get_header_for_api_calls(self, no_client_id=False):
359362
now = datetime.datetime.now()
360-
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
361363
return {
362364
"Content-Type": "application/json;charset=utf-8",
363365
"x-app-name": "Comfort Cloud",
364366
"user-agent": "G-RAC",
365-
"x-app-timestamp": timestamp,
367+
"x-app-timestamp": now.strftime("%Y-%m-%d %H:%M:%S"),
366368
"x-app-type": "1",
367369
"x-app-version": self._app_version,
368-
"x-cfc-api-key": self._get_api_key(timestamp, self._token["access_token"]),
370+
"x-cfc-api-key": self._get_api_key(now, self._token["access_token"]),
369371
"x-client-id": self._token["acc_client_id"],
370372
"x-user-authorization-v2": "Bearer " + self._token["access_token"]
371373
}

0 commit comments

Comments
 (0)