From 9502b4faceebff4d35ee12587745f498fa33061d Mon Sep 17 00:00:00 2001 From: Mateo Date: Wed, 10 Apr 2024 13:50:31 +0200 Subject: [PATCH] get site without client (#148) * get site without client * remove last / * add timeout * remove sites file on github --- app/site_devices.json | 1 - app/utils/data.py | 6 +++--- app/utils/display.py | 6 +++--- app/utils/sites.py | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) delete mode 100644 app/site_devices.json create mode 100644 app/utils/sites.py diff --git a/app/site_devices.json b/app/site_devices.json deleted file mode 100644 index e8366ed..0000000 --- a/app/site_devices.json +++ /dev/null @@ -1 +0,0 @@ -{"1": "test mateo", "2": "test mateo", "3": "test mateo", "4": "test mateo", "21": "st peray", "22": "st peray", "5": "tour de brison", "6": "tour de brison", "7": "tour de brison", "8": "tour de brison", "9": "st marguerite", "10": "st marguerite", "11": "st marguerite", "12": "st marguerite", "13": "salaunes 1", "14": "salaunes 1", "15": "salaunes 1", "16": "salaunes 1", "17": "salaunes 2", "18": "salaunes 2", "19": "salaunes 2", "20": "salaunes 2", "23": "adf 1010", "24": "adf 1015", "25": "adf 1200", "26": "adf 1220", "27": "adf 1231", "28": "adf 1320", "29": "adf 1340", "30": "adf 1420", "31": "adf 1440", "32": "adf 5445", "33": "adf 5557", "34": "adf 5559", "35": "adf 5882", "36": "adf 5993", "37": "adf 5999", "38": "adf 7447", "39": "adf 7448", "40": "courmettes", "41": "courmettes", "42": "courmettes", "43": "courmettes", "44": "valbonne", "45": "valbonne", "46": "valbonne", "47": "valbonne", "48": "cabanelle", "49": "cabanelle", "50": "cabanelle", "51": "cabanelle", "52": "ferion", "53": "ferion", "54": "ferion", "55": "ferion"} \ No newline at end of file diff --git a/app/utils/data.py b/app/utils/data.py index 60f7521..bc946ea 100644 --- a/app/utils/data.py +++ b/app/utils/data.py @@ -11,7 +11,7 @@ import pandas as pd -from services import call_api +from utils.sites import get_sites def read_stored_DataFrame(data): @@ -147,10 +147,10 @@ def load_site_data_file(api_client, user_credentials, site_devices_file="site_de with site_devices_path.open() as json_file: return json.load(json_file) - client_sites = pd.DataFrame(call_api(api_client.get_sites, user_credentials)()) + client_sites = get_sites(user_credentials) site_devices_dict = {} for _, site in client_sites.iterrows(): - site_ids = api_client.get_site_devices(site["id"]).json() + site_ids = set(api_client.get_site_devices(site["id"]).json()) for site_id in site_ids: site_devices_dict[str(site_id)] = site["name"].replace("_", " ") with site_devices_path.open("w") as fp: diff --git a/app/utils/display.py b/app/utils/display.py index f375a62..7cf7aee 100644 --- a/app/utils/display.py +++ b/app/utils/display.py @@ -6,7 +6,6 @@ from datetime import datetime import dash_leaflet as dl -import pandas as pd import pytz import requests from dash import html @@ -15,7 +14,8 @@ from timezonefinder import TimezoneFinder import config as cfg -from services import api_client, call_api +from services import api_client +from utils.sites import get_sites DEPARTMENTS = requests.get(cfg.GEOJSON_FILE, timeout=10).json() @@ -76,7 +76,7 @@ def build_sites_markers(user_headers, user_credentials): user_token = user_headers["Authorization"].split(" ")[1] api_client.token = user_token - client_sites = pd.DataFrame(call_api(api_client.get_sites, user_credentials)()) + client_sites = get_sites(user_credentials) markers = [] for _, site in client_sites.iterrows(): diff --git a/app/utils/sites.py b/app/utils/sites.py new file mode 100644 index 0000000..7111530 --- /dev/null +++ b/app/utils/sites.py @@ -0,0 +1,38 @@ +# Copyright (C) 2020-2024, Pyronear. + +# This program is licensed under the Apache License 2.0. +# See LICENSE or go to for full license details. + +from typing import Any, Dict, Optional + +import pandas as pd +import requests + +import config as cfg + + +def get_token(api_url: str, login: str, pwd: str) -> str: + response = requests.post(f"{api_url}/login/access-token", data={"username": login, "password": pwd}, timeout=3) + if response.status_code != 200: + raise ValueError(response.json()["detail"]) + return response.json()["access_token"] + + +def api_request(method_type: str, route: str, headers=Dict[str, str], payload: Optional[Dict[str, Any]] = None): + kwargs = {"json": payload} if isinstance(payload, dict) else {} + + response = getattr(requests, method_type)(route, headers=headers, **kwargs) + return response.json() + + +def get_sites(user_credentials): + api_url = cfg.API_URL.rstrip("/") + superuser_login = user_credentials["username"] + superuser_pwd = user_credentials["password"] + + superuser_auth = { + "Authorization": f"Bearer {get_token(api_url, superuser_login, superuser_pwd)}", + "Content-Type": "application/json", + } + api_sites = api_request("get", f"{api_url}/sites/", superuser_auth) + return pd.DataFrame(api_sites)