Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Commit

Permalink
add auto token refresh on 401
Browse files Browse the repository at this point in the history
  • Loading branch information
drosoCode committed Apr 29, 2022
1 parent a524e3e commit 2d630ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ For the first authentication, in order to obtain an access_token and a refresh_t
This portal will then redirect to a specific url and provide the code and state parameters.

By default in the function :meth:`edf_api.auth.EDFAuth.get_login_url` this url is ``edfetmoiauth:/oauthcallback`` which will raise an error in the javascript console (because the scheme is unknown).
You can then retrieve this error and parse it (there is an example to do so in the cli.py demo file), this method allows to retrieve the data even without a server ready to receive the data from the redirection.
You can then retrieve this error and parse it (there is an example to do so in the cli.py demo file), this is currently the only way to retrieve the result as the redirect url is restricted (any other one will be refused by the server).

.. image:: _static/error.png
:width: 600
Expand Down
17 changes: 13 additions & 4 deletions edf_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ def __init__(self, session: aiohttp.ClientSession, access_token: str, refresh_to
self._refresh_token = refresh_token
self._expiration = expiration

async def _get_access_token(self) -> str:
async def _get_access_token(self, force=False) -> str:
"""
Function to get and access_token and ensure that it is valid
Args:
force: set to true to force the token refresh
Returns:
a valid access_token
"""
if self._expiration-5 < time.time():
if self._expiration-5 < time.time() or force:
auth = EDFAuth(self._session)
self._access_token, self._refresh_token, self._expiration = await auth.get_token(refresh_token=self._refresh_token)
return self._access_token
Expand Down Expand Up @@ -69,7 +72,7 @@ async def get_info(self, insee: str, pdl: str) -> ElecInfo:
return ElecInfo.from_json(req["listeCoupuresInfoReseau"][i], req["listeCrises"][i])
return ElecInfo.no_outage()

async def get_data(self, bp_num: str, pdl: str, start: str, end: str) -> dict:
async def get_data(self, bp_num: str, pdl: str, start: str, end: str, retry: bool = False) -> dict:
"""
Function to get the consumption data
Expand All @@ -78,8 +81,9 @@ async def get_data(self, bp_num: str, pdl: str, start: str, end: str) -> dict:
pdl: the PDL id
start: the start date for the retrieved data in YYYY-MM-DD format
end: the end date for the retrieved data in YYYY-MM-DD format
retry: (internal) used to know if this is the first call to this function or if this is a retry
"""
tok = await self._get_access_token()
tok = await self._get_access_token(retry)
async with async_timeout.timeout(TIMEOUT):
response = await self._session.get(f"https://api-edf.edelia.fr/api/v1/sites/-/load-curve?step=30&begin-date={start}&end-date={end}&withCost=true",
headers={
Expand All @@ -91,4 +95,9 @@ async def get_data(self, bp_num: str, pdl: str, start: str, end: str) -> dict:
},
)
req = await response.json()

if (("errorCode" in req and req["errorCode"] == "401") or ("status" in req and req["status"] == 401)) and not retry:
# try to force the access_token refresh if
return await self.get_data(bp_num, pdl, start, end, True)

return req
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setup(
name='edf_api',
packages=find_packages(include=['edf_api']),
version='0.1.0',
version='0.1.1',
author='drosocode',
license='MIT',
description='API for EDF',
Expand Down

0 comments on commit 2d630ed

Please sign in to comment.