Spaces:
Running
Running
| import os | |
| import requests | |
| from dotenv import load_dotenv | |
| from typing import Dict, List | |
| load_dotenv() | |
| API_URL = os.getenv('API_URL') | |
| API_KEY = os.getenv('API_KEY') | |
| def _get_with_auth(url: str, **kwargs): | |
| """ | |
| Sends a GET request to the API with the authorization header. | |
| """ | |
| # Merge the headers with the kwargs | |
| headers = kwargs.get('headers', {}) | |
| headers.update({'Authorization': f'{API_KEY}'}) | |
| kwargs['headers'] = headers | |
| return requests.get(url, **kwargs) | |
| # Get reference data | |
| def get_courts() -> List[str]: | |
| """ | |
| Gets the list of courts from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}/references/courts') | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return [] | |
| def get_surfaces() -> List[str]: | |
| """ | |
| Gets the list of surfaces from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}/references/surfaces') | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return [] | |
| def get_series() -> List[str]: | |
| """ | |
| Gets the list of series from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}/references/series') | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return [] | |
| def get_list_tournaments(): | |
| """ | |
| Gets the list of tournaments from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}/tournament/names') | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return [] | |
| def get_matches_in_tournaments(tournament: str, year: int) -> list[dict]: | |
| """ | |
| Gets the list of matches in a tournament from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}/tournament/matches', params={'name': tournament, 'year': year}) | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return [] | |
| def get_list_models(): | |
| """ | |
| Gets the list of available models from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}/list_available_models', params={'aliases': ['prod']}) | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return [] | |
| def get_player(player_id: int): | |
| """ | |
| Gets the player information from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}/player/{player_id}') | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return {} | |
| def get_players(ids: List[int]) -> Dict[int, Dict]: | |
| """ | |
| Gets the list of players from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}/players', params={'ids': ids}) | |
| if response.status_code == 200: | |
| return {player['id']: player for player in response.json().values()} | |
| else: | |
| return {} | |
| def get_prediction(data: Dict): | |
| """ | |
| Gets the prediction of a match between two players from the API. | |
| """ | |
| response = _get_with_auth(f'{API_URL}predict', params=data) | |
| response.raise_for_status() | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| return {} |