-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* get site without client * remove last / * add timeout * remove sites file on github
- Loading branch information
1 parent
5685eb2
commit 9502b4f
Showing
4 changed files
with
44 additions
and
7 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |