Skip to content

Commit

Permalink
feat: made tokens async props
Browse files Browse the repository at this point in the history
  • Loading branch information
Lash-L committed Feb 10, 2023
1 parent 180d7de commit a56487b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
56 changes: 38 additions & 18 deletions src/southern_company_api/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,57 @@ class SouthernCompanyAPI:
def __init__(self, username: str, password: str):
self.username = username
self.password = password
self.jwt: typing.Optional[str] = None
self.sc: typing.Optional[str] = None
self.request_token: typing.Optional[str] = None
self._jwt: typing.Optional[str] = None
self._sc: typing.Optional[str] = None
self._request_token: typing.Optional[str] = None
self._accounts: List[Account] = []

@property
def accounts(self) -> List[Account]:
async def sc(self) -> typing.Optional[str]:
if self._sc is None:
await self._get_sc_web_token()
return self._sc

@property
async def accounts(self) -> List[Account]:
if len(self._accounts) == 0:
await self.get_accounts()
return self._accounts

@property
async def jwt(self) -> typing.Optional[str]:
if self._jwt is None:
self._jwt = await self.get_jwt()
return self._jwt

@property
async def request_token(self) -> typing.Optional[str]:
if self._request_token is None:
self._request_token = await get_request_verification_token()
return self._request_token

async def connect(self) -> None:
"""
Connects to Southern company and gets all accounts
"""
self.request_token = await get_request_verification_token()
self.sc = await self._get_sc_web_token()
self.jwt = await self.get_jwt()
self._request_token = await get_request_verification_token()
self._sc = await self._get_sc_web_token()
self._jwt = await self.get_jwt()
self._accounts = await self.get_accounts()

async def authenticate(self) -> bool:
"""Determines if you can authenticate with Southern Company with given login"""
self.request_token = await get_request_verification_token()
self.sc = await self._get_sc_web_token()
self._request_token = await get_request_verification_token()
self._sc = await self._get_sc_web_token()
return True

async def _get_sc_web_token(self) -> str:
"""Gets a sc_web_token which we get from a successful log in"""
if self.request_token is None:
self.request_token = await get_request_verification_token()
if self._request_token is None:
self._request_token = await get_request_verification_token()
headers = {
"Content-Type": "application/json; charset=utf-8",
"RequestVerificationToken": self.request_token,
"RequestVerificationToken": self._request_token,
}

data = {
Expand Down Expand Up @@ -104,9 +124,9 @@ async def _get_sc_web_token(self) -> str:
raise NoScTokenFound("Login request did not return a sc token")

async def _get_southern_jwt_cookie(self) -> str:
if self.sc is None:
self.sc = await self._get_sc_web_token()
data = {"ScWebToken": self.sc}
if self._sc is None:
self._sc = await self._get_sc_web_token()
data = {"ScWebToken": self._sc}
async with aiohttp.ClientSession() as session:
async with session.post(
"https://customerservice2.southerncompany.com/Account/LoginComplete?"
Expand Down Expand Up @@ -180,13 +200,13 @@ async def get_jwt(self) -> str:
)

# Returning JWT
self.jwt = token
self._jwt = token
return token

async def get_accounts(self) -> List[Account]:
if self.jwt is None:
if self._jwt is None:
await self.get_jwt()
headers = {"Authorization": f"bearer {self.jwt}"}
headers = {"Authorization": f"bearer {self._jwt}"}
async with aiohttp.ClientSession() as session:
async with session.get(
"https://customerservice2api.southerncompany.com/api/account/"
Expand Down
8 changes: 4 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def test_ga_power_get_sc_web_token():
with patch("southern_company_api.parser.aiohttp.ClientSession.post") as mock_post:
mock_post.return_value = MockResponse("", 200, "", ga_power_sample_sc_response)
sca = SouthernCompanyAPI("", "")
sca.request_token = "sample"
sca._request_token = "sample"
response_token = await sca._get_sc_web_token()
assert response_token == "sample_sc_token"

Expand All @@ -93,7 +93,7 @@ async def test_ga_power_get_jwt_cookie():
"", 200, ga_power_southern_jwt_cookie_header, ""
)
sca = SouthernCompanyAPI("", "")
sca.sc = ""
sca._sc = ""
token = await sca._get_southern_jwt_cookie()
assert token == "sample_cookie"

Expand All @@ -120,7 +120,7 @@ async def test_ga_power_get_accounts():
)
mock_get.return_value.__aenter__.return_value.status = 200
sca = SouthernCompanyAPI("", "")
sca.jwt = "sample"
sca._jwt = "sample"
response_token: typing.List[Account] = await sca.get_accounts()
assert response_token[0].name == "Home Energy"
assert sca.accounts == response_token
assert sca._accounts == response_token

0 comments on commit a56487b

Please sign in to comment.