diff --git a/SpotifyAPI/Web/SpotifyWebAPI.cs b/SpotifyAPI/Web/SpotifyWebAPI.cs index b785b55d..2a8059c3 100644 --- a/SpotifyAPI/Web/SpotifyWebAPI.cs +++ b/SpotifyAPI/Web/SpotifyWebAPI.cs @@ -59,6 +59,8 @@ namespace SpotifyAPI.Web /// Maximum number of tries for one failed request. /// public int RetryTimes { get; set; } = 10; + + public IEnumerable RetryErrorCodes { get; private set; } = new int[] { 500, 502, 503 }; #endregion Configuration #region Search @@ -714,10 +716,13 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowing"); - Tuple res = DownloadDataAlt(_builder.IsFollowing(followType, ids)); - return ExtractDataFromAltDownload(res); + + var url = _builder.IsFollowing(followType, ids); + return DownloadDataAltAndExtract(url); } + + /// /// Check to see if the current user is following one or more artists or other Spotify users asynchronously. /// @@ -729,8 +734,9 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowing"); - Tuple res = await DownloadDataAltAsync(_builder.IsFollowing(followType, ids)); - return ExtractDataFromAltDownload(res); + + var url = _builder.IsFollowing(followType, ids); + return await DownloadDataAltAndExtractAsync(url); } /// @@ -839,8 +845,9 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowingPlaylist"); - Tuple res = DownloadDataAlt(_builder.IsFollowingPlaylist(ownerId, playlistId, ids)); - return ExtractDataFromAltDownload(res); + + var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids); + return DownloadDataAltAndExtract(url); } /// @@ -855,8 +862,9 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowingPlaylist"); - Tuple res = await DownloadDataAltAsync(_builder.IsFollowingPlaylist(ownerId, playlistId, ids)); - return ExtractDataFromAltDownload(res); + + var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids); + return await DownloadDataAltAndExtractAsync(url); } /// @@ -999,8 +1007,9 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedTracks"); - Tuple res = DownloadDataAlt(_builder.CheckSavedTracks(ids)); - return ExtractDataFromAltDownload(res); + + var url = _builder.CheckSavedTracks(ids); + return DownloadDataAltAndExtract(url); } /// @@ -1013,8 +1022,8 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedTracks"); - Tuple res = await DownloadDataAltAsync(_builder.CheckSavedTracks(ids)); - return ExtractDataFromAltDownload(res); + var url = _builder.CheckSavedTracks(ids); + return await DownloadDataAltAndExtractAsync(url); } /// @@ -1127,8 +1136,9 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedTracks"); - Tuple res = DownloadDataAlt(_builder.CheckSavedAlbums(ids)); - return ExtractDataFromAltDownload(res); + + var url = _builder.CheckSavedAlbums(ids); + return DownloadDataAltAndExtract(url); } /// @@ -1141,8 +1151,8 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedAlbumsAsync"); - Tuple res = await DownloadDataAltAsync(_builder.CheckSavedAlbums(ids)); - return ExtractDataFromAltDownload(res); + var url = _builder.CheckSavedAlbums(ids); + return await DownloadDataAltAndExtractAsync(url); } #endregion Library @@ -1816,6 +1826,46 @@ namespace SpotifyAPI.Web return await GetPreviousPageAsync, T>(paging); } + private ListResponse DownloadDataAltAndExtract(string url) + { + int triesLeft = RetryTimes + 1; + Error lastError = null; + + ListResponse data = null; + do + { + Tuple res = DownloadDataAlt(url); + data = ExtractDataFromAltDownload(res); + + lastError = data.Error; + + triesLeft -= 1; + + } while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status)); + + return data; + } + + private async Task> DownloadDataAltAndExtractAsync(string url) + { + int triesLeft = RetryTimes + 1; + Error lastError = null; + + ListResponse data = null; + do + { + Tuple res = await DownloadDataAltAsync(url); + data = ExtractDataFromAltDownload(res); + + lastError = data.Error; + + triesLeft -= 1; + + } while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status)); + + return data; + } + private static ListResponse ExtractDataFromAltDownload(Tuple res) { ListResponse ret = null; @@ -1855,7 +1905,7 @@ namespace SpotifyAPI.Web triesLeft -= 1; - } while (UseAutoRetry && triesLeft > 0 && lastError != null); + } while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status)); return response.Item2; } @@ -1881,7 +1931,7 @@ namespace SpotifyAPI.Web triesLeft -= 1; - } while (UseAutoRetry && triesLeft > 0 && lastError != null); + } while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status)); return response.Item2; } @@ -1901,7 +1951,7 @@ namespace SpotifyAPI.Web triesLeft -= 1; - } while (UseAutoRetry && triesLeft > 0 && lastError != null); + } while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status)); return response.Item2; @@ -1922,7 +1972,7 @@ namespace SpotifyAPI.Web triesLeft -= 1; - } while (UseAutoRetry && triesLeft > 0 && lastError != null); + } while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status)); return response.Item2;