Skip to content

Commit

Permalink
fix: jwt will refresh after 3 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
Lash-L committed Feb 13, 2023
1 parent bffb69f commit a16f442
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/southern_company_api/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import re
import typing
from typing import List
Expand Down Expand Up @@ -42,6 +43,7 @@ def __init__(self, username: str, password: str):
self.username = username
self.password = password
self._jwt: typing.Optional[str] = None
self._jwt_expiry: datetime.datetime = datetime.datetime.now()
self._sc: typing.Optional[str] = None
self._request_token: typing.Optional[str] = None
self._accounts: List[Account] = []
Expand All @@ -60,7 +62,7 @@ async def accounts(self) -> List[Account]:

@property
async def jwt(self) -> str:
if self._jwt is None:
if self._jwt is None or datetime.datetime.now() >= self._jwt_expiry:
self._jwt = await self.get_jwt()
return self._jwt

Expand Down Expand Up @@ -201,6 +203,9 @@ async def get_jwt(self) -> str:

# Returning JWT
self._jwt = token
# We can get the exact expiration date from the jwt token, but I don't think it is needed. It should always be
# greater than 3 hours.
self._jwt_expiry = datetime.datetime.now() + datetime.timedelta(hours=3)
return token

async def get_accounts(self) -> List[Account]:
Expand Down

0 comments on commit a16f442

Please sign in to comment.