Fix misc bugs related to multithreading

This commit is contained in:
nathom 2021-04-12 10:44:55 -07:00
parent 8066db1862
commit 89ddd0d3a6
2 changed files with 22 additions and 2 deletions

View file

@ -450,6 +450,11 @@ class TidalClient(ClientInterface):
self.refresh_token = None
self.expiry = None
self.session = requests.Session()
# for multithreading
adapter = requests.adapters.HTTPAdapter(pool_connections=200, pool_maxsize=200)
self.session.mount('https://', adapter)
def login(
self,
user_id=None,
@ -617,6 +622,7 @@ class TidalClient(ClientInterface):
self.country_code = resp["user"]["countryCode"]
self.access_token = resp["access_token"]
self.token_expiry = resp["expires_in"] + time.time()
self._update_authorization()
def _login_by_access_token(self, token, user_id=None):
headers = {"authorization": f"Bearer {token}"}
@ -630,6 +636,7 @@ class TidalClient(ClientInterface):
self.user_id = resp["userId"]
self.country_code = resp["countryCode"]
self.access_token = token
self._update_authorization()
def _api_get(self, item_id: str, media_type: str) -> dict:
url = f"{media_type}s/{item_id}"
@ -667,6 +674,9 @@ class TidalClient(ClientInterface):
r = requests.post(url, data=data, auth=auth, verify=False).json()
return r
def _update_authorization(self):
self.session.headers.update({'authorization': f"Bearer {self.access_token}"})
class SoundCloudClient(ClientInterface):
source = "soundcloud"