Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added token refresh feature #62

Merged
merged 4 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def retrieve_site_from_device_id(device_id, site_devices_data):
# We import the cameras's positions from the API that locates the cameras
# Fetching the response in a variable
response = api_client.get_sites()
# Check token expiration
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = api_client.get_sites()

# Getting the json data out of the response
camera_positions = response.json()
Expand Down
57 changes: 47 additions & 10 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
app.config.suppress_callback_exceptions = True
server = app.server # Gunicorn will be looking for the server attribute of this module

response = requests.get('https://api.pyronear.org/devices/', headers=api_client.headers)
# Check token expiration
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = requests.get('https://api.pyronear.org/devices/', headers=api_client.headers)

# We create a rough layout, filled with the content of the homepage/alert page
app.layout = html.Div(
[
Expand All @@ -111,7 +117,7 @@
dcc.Store(
id="devices_data_storage",
storage_type="session",
data=requests.get('https://api.pyronear.org/devices/', headers=api_client.headers).json()
data=response.json()
),

# Main interval that fetches API alerts data
Expand Down Expand Up @@ -370,7 +376,12 @@ def update_live_alerts_data(
"""

# Fetching live alerts where is_acknowledged is False
response = api_client.get_ongoing_alerts().json()
response = api_client.get_ongoing_alerts()
# Check token expiration
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = api_client.get_ongoing_alerts()
response = response.json()

# If there is no alert, we prevent the callback from updating anything
if len(response) == 0:
Expand All @@ -392,7 +403,11 @@ def update_live_alerts_data(
# We now want to build the boolean indexing mask that indicates whether or not the event is unacknowledged
# We start by making an API call to fetch all events
url = cfg.API_URL + '/events/'
all_events = requests.get(url, headers=api_client.headers).json()
response = requests.get(url, headers=api_client.headers)
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = requests.get(url, headers=api_client.headers)
all_events = response.json()

# Then, we construct a dictionary whose keys are the event IDs (as integers) and values are the corresponding
# "is_acknowledged" field in the events table (boolean)
Expand Down Expand Up @@ -438,7 +453,11 @@ def update_live_alerts_data(
for _, row in live_alerts.iterrows():
try:
# For each live alert, we fetch the URL of the associated frame
img_url = api_client.get_media_url(row["media_id"]).json()["url"]
response = api_client.get_media_url(row["media_id"])
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = api_client.get_media_url(row["media_id"])
img_url = response.json()['url']

except Exception:
# This is just a security in case we cannot retrieve the URL of the detection frame
Expand Down Expand Up @@ -523,7 +542,11 @@ def update_live_alerts_data(
for _, row in new_alerts.iterrows():
try:
# For each new live alert, we fetch the URL of the associated frame
img_url = api_client.get_media_url(row["media_id"]).json()["url"]
response = api_client.get_media_url(row["media_id"])
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = api_client.get_media_url(row["media_id"])
img_url = response.json()['url']

except Exception:
# This is just a security in case we cannot retrieve the URL of the detection frame
Expand Down Expand Up @@ -833,8 +856,11 @@ def zoom_on_alert(n_clicks, live_alerts, frame_urls):
# We make an API call to check whether the event has already been acknowledged or not
# Depending on the response, an acknowledgement button will be displayed or not in the alert overview
url = cfg.API_URL + f"/events/{event_id}/"
response = requests.get(url, headers=api_client.headers).json()
acknowledged = response['is_acknowledged']
response = requests.get(url, headers=api_client.headers)
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = requests.get(url, headers=api_client.headers)
acknowledged = response.json()['is_acknowledged']

# We fetch the latitude and longitude of the device that has raised the alert and we build the alert overview
lat, lon, div = build_alert_overview(
Expand Down Expand Up @@ -994,7 +1020,10 @@ def acknowledge_alert(n_clicks):
event_id = event_id.strip('"')

# The event is actually acknowledged thanks to the acknowledge_event of the API client
api_client.acknowledge_event(event_id=int(event_id))
response = api_client.acknowledge_event(event_id=int(event_id))
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
api_client.acknowledge_event(event_id=int(event_id))

return [html.P('Alerte acquittée.')]

Expand Down Expand Up @@ -1321,7 +1350,11 @@ def update_alert_screen(n_intervals, blocked_event_ids, devices_data, site_devic
# We now want to build the boolean indexing mask that indicates whether or not the event is unacknowledged
# We start by making an API call to fetch all events
url = cfg.API_URL + '/events/'
all_events = requests.get(url, headers=api_client.headers).json()
response = requests.get(url, headers=api_client.headers)
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = requests.get(url, headers=api_client.headers)
all_events = response.json()

# Then, we construct a dictionary whose keys are the event IDs (as integers) and values are the corresponding
# "is_acknowledged" field in the events table (boolean)
Expand Down Expand Up @@ -1388,7 +1421,11 @@ def update_alert_screen(n_intervals, blocked_event_ids, devices_data, site_devic
img_url = ""

try:
img_url = api_client.get_media_url(row["media_id"]).json()["url"]
response = api_client.get_media_url(row["media_id"])
if response.status_code == 401:
api_client.refresh_token(cfg.API_LOGIN, cfg.API_PWD)
response = api_client.get_media_url(row["media_id"])
img_url = response.json()['url']

except Exception:
pass
Expand Down