import os import requests from applemusicpy import AppleMusic from spotipy import Spotify from .auth0 import Auth0 VENDOR_API_HOST = os.environ.get("VENDOR_API_HOST") VENDOR_DATA_TIMEOUT = os.environ.get("VENDOR_DATA_TIMEOUT") or str( 60 * 60 * 24 ) class Auth: @staticmethod def get_token() -> str: return Auth0.get_token() class AppleMusicVendor(AppleMusic): def __init__(self): self.proxies = None self.token_str = Auth.get_token() self.root = f"https://{VENDOR_API_HOST}/vendor-api/apple/v1/" self.max_retries = 3 self.requests_timeout = None self._session = requests.api # individual calls, slower def _auth_headers(self): headers = super()._auth_headers() # adding common headers here, as there is no other convenient way headers["X-Data-Timeout"] = VENDOR_DATA_TIMEOUT return headers class SpotifyVendor(Spotify): max_get_retries = 1 def __init__(self): """ Creates a Spotify API client. :param auth: An authorization token (optional) :param requests_session: A Requests session object or a truthy value to create one. A falsy value disables sessions. It should generally be a good idea to keep sessions enabled for performance reasons (connection pooling). :param client_credentials_manager: SpotifyClientCredentials object :param proxies: Definition of proxies (optional) :param requests_timeout: Tell Requests to stop waiting for a response after a given number of seconds """ self.prefix = f"https://{VENDOR_API_HOST}/vendor-api/spotify/v1/" self._auth = Auth.get_token() self.client_credentials_manager = None self.proxies = None self.requests_timeout = None self.language = None self._session = requests.api def _auth_headers(self): headers = super()._auth_headers() # adding common headers here, as there is no other convenient way headers["X-Data-Timeout"] = VENDOR_DATA_TIMEOUT return headers def get_spotify_client(): return SpotifyVendor() def get_apple_music_client(): return AppleMusicVendor()