Skip to content

Commit

Permalink
get site without client (#148)
Browse files Browse the repository at this point in the history
* get site without client

* remove last /

* add timeout

* remove sites file on github
  • Loading branch information
MateoLostanlen authored Apr 10, 2024
1 parent 5685eb2 commit 9502b4f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
1 change: 0 additions & 1 deletion app/site_devices.json

This file was deleted.

6 changes: 3 additions & 3 deletions app/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import pandas as pd

from services import call_api
from utils.sites import get_sites


def read_stored_DataFrame(data):
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions app/utils/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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():
Expand Down
38 changes: 38 additions & 0 deletions app/utils/sites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (C) 2020-2024, Pyronear.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> 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)

0 comments on commit 9502b4f

Please sign in to comment.