Skip to content

Commit

Permalink
Add retry logic to internal call (#1707)
Browse files Browse the repository at this point in the history
  • Loading branch information
owahltinez authored Jan 8, 2023
1 parent 3ebcef2 commit 4978b8f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions spotdl/utils/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from json import dumps
from typing import Dict, Optional

import requests
from spotipy import Spotify
from spotipy.cache_handler import CacheFileHandler, MemoryCacheHandler
from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth
Expand Down Expand Up @@ -141,7 +142,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._initialized = True

def _get(self, url, args=None, payload=None, **kwargs):
def _get(self, url, args=None, payload=None, retries=3, **kwargs):
"""
Overrides the get method of the SpotifyClient.
Allows us to cache requests
Expand All @@ -161,7 +162,15 @@ def _get(self, url, args=None, payload=None, **kwargs):
if self.cache.get(cache_key) is not None:
return self.cache[cache_key]

response = self._internal_call("GET", url, payload, kwargs)
# Wrap in a try-except and retry up to `retries` times.
response = None
while response is None:
try:
response = self._internal_call("GET", url, payload, kwargs)
except requests.exceptions.Timeout as exc:
retries -= 1
if retries <= 0:
raise exc

if use_cache and cache_key is not None:
self.cache[cache_key] = response
Expand Down

0 comments on commit 4978b8f

Please sign in to comment.