File size: 1,861 Bytes
8ec4d2d 815b0dc 50e328e 8ec4d2d 815b0dc 8ec4d2d 815b0dc 8ec4d2d 815b0dc 8ec4d2d 815b0dc 8ec4d2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import requests
from loguru import logger
from . import MOONLANDING_URL
def get_auth_headers(token: str, prefix: str = "Bearer"):
return {"Authorization": f"{prefix} {token}"}
def http_post(path: str, token: str, payload=None, domain: str = None, params=None) -> requests.Response:
"""HTTP POST request to the AutoNLP API, raises UnreachableAPIError if the API cannot be reached"""
try:
response = requests.post(
url=domain + path, json=payload, headers=get_auth_headers(token=token), allow_redirects=True, params=params
)
except requests.exceptions.ConnectionError:
logger.error("β Failed to reach AutoNLP API, check your internet connection")
response.raise_for_status()
return response
def http_get(path: str, token: str, domain: str = None) -> requests.Response:
"""HTTP POST request to the AutoNLP API, raises UnreachableAPIError if the API cannot be reached"""
try:
response = requests.get(url=domain + path, headers=get_auth_headers(token=token), allow_redirects=True)
except requests.exceptions.ConnectionError:
logger.error("β Failed to reach AutoNLP API, check your internet connection")
response.raise_for_status()
return response
def user_authentication(token):
headers = {}
cookies = {}
if token.startswith("hf_"):
headers["Authorization"] = f"Bearer {token}"
else:
cookies = {"token": token}
try:
response = requests.get(
MOONLANDING_URL + "/api/whoami-v2",
headers=headers,
cookies=cookies,
timeout=3,
)
except (requests.Timeout, ConnectionError) as err:
logger.error(f"Failed to request whoami-v2 - {repr(err)}")
raise Exception("Hugging Face Hub is unreachable, please try again later.")
return response.json()
|