Skip to content

Commit

Permalink
Update datetime functions to use timezone awareness (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkb79 authored May 6, 2024
1 parent 75ff807 commit 8371418
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/audible/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import logging
from collections.abc import Callable, Generator, Iterator
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -72,7 +72,9 @@ def refresh_access_token(
resp_dict = resp.json()

expires_in_sec = int(resp_dict["expires_in"])
expires = (datetime.utcnow() + timedelta(seconds=expires_in_sec)).timestamp()
expires = (
datetime.now(timezone.utc) + timedelta(seconds=expires_in_sec)
).timestamp()

return {"access_token": resp_dict["access_token"], "expires": expires}

Expand Down Expand Up @@ -191,7 +193,7 @@ def sign_request(
Returns:
A dict with the signed headers.
"""
date = datetime.utcnow().isoformat("T") + "Z"
date = datetime.now(timezone.utc).isoformat("T") + "Z"
str_body = body.decode("utf-8")

data = f"{method}\n{path}\n{date}\n{str_body}\n{adp_token}"
Expand Down Expand Up @@ -779,10 +781,14 @@ def user_profile(self) -> dict[str, Any]:
def access_token_expires(self) -> timedelta:
if self.expires is None:
raise Exception("No expires timestamp found.")
return datetime.fromtimestamp(self.expires) - datetime.utcnow()
return datetime.fromtimestamp(self.expires, timezone.utc) - datetime.now(
timezone.utc
)

@property
def access_token_expired(self) -> bool:
if self.expires is None:
raise Exception("No expires timestamp found.")
return datetime.fromtimestamp(self.expires) <= datetime.utcnow()
return datetime.fromtimestamp(self.expires, timezone.utc) <= datetime.now(
timezone.utc
)
4 changes: 2 additions & 2 deletions src/audible/register.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Any

import httpx
Expand Down Expand Up @@ -80,7 +80,7 @@ def register(
access_token = tokens["bearer"]["access_token"]
refresh_token = tokens["bearer"]["refresh_token"]
expires_s = int(tokens["bearer"]["expires_in"])
expires = (datetime.utcnow() + timedelta(seconds=expires_s)).timestamp()
expires = (datetime.now(timezone.utc) + timedelta(seconds=expires_s)).timestamp()

extensions = success_response["extensions"]
device_info = extensions["device_info"]
Expand Down

0 comments on commit 8371418

Please sign in to comment.