Skip to content

Commit ea317dc

Browse files
committed
Removes duplicated code + adds support for retry to Alt functions.
1 parent cf2d2f6 commit ea317dc

1 file changed

Lines changed: 70 additions & 20 deletions

File tree

‎SpotifyAPI/Web/SpotifyWebAPI.cs‎

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public void Dispose()
5959
/// Maximum number of tries for one failed request.
6060
/// </summary>
6161
public int RetryTimes { get; set; } = 10;
62+
63+
public IEnumerable<int> RetryErrorCodes { get; private set; } = new int[] { 500, 502, 503 };
6264
#endregion Configuration
6365

6466
#region Search
@@ -714,10 +716,13 @@ public ListResponse<bool> IsFollowing(FollowType followType, List<string> ids)
714716
{
715717
if (!UseAuth)
716718
throw new InvalidOperationException("Auth is required for IsFollowing");
717-
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(_builder.IsFollowing(followType, ids));
718-
return ExtractDataFromAltDownload(res);
719+
720+
var url = _builder.IsFollowing(followType, ids);
721+
return DownloadDataAltAndExtract(url);
719722
}
720723

724+
725+
721726
/// <summary>
722727
/// Check to see if the current user is following one or more artists or other Spotify users asynchronously.
723728
/// </summary>
@@ -729,8 +734,9 @@ public async Task<ListResponse<bool>> IsFollowingAsync(FollowType followType, Li
729734
{
730735
if (!UseAuth)
731736
throw new InvalidOperationException("Auth is required for IsFollowing");
732-
Tuple<ResponseInfo, JToken> res = await DownloadDataAltAsync<JToken>(_builder.IsFollowing(followType, ids));
733-
return ExtractDataFromAltDownload(res);
737+
738+
var url = _builder.IsFollowing(followType, ids);
739+
return await DownloadDataAltAndExtractAsync(url);
734740
}
735741

736742
/// <summary>
@@ -839,8 +845,9 @@ public ListResponse<bool> IsFollowingPlaylist(string ownerId, string playlistId,
839845
{
840846
if (!UseAuth)
841847
throw new InvalidOperationException("Auth is required for IsFollowingPlaylist");
842-
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(_builder.IsFollowingPlaylist(ownerId, playlistId, ids));
843-
return ExtractDataFromAltDownload(res);
848+
849+
var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids);
850+
return DownloadDataAltAndExtract(url);
844851
}
845852

846853
/// <summary>
@@ -855,8 +862,9 @@ public async Task<ListResponse<bool>> IsFollowingPlaylistAsync(string ownerId, s
855862
{
856863
if (!UseAuth)
857864
throw new InvalidOperationException("Auth is required for IsFollowingPlaylist");
858-
Tuple<ResponseInfo, JToken> res = await DownloadDataAltAsync<JToken>(_builder.IsFollowingPlaylist(ownerId, playlistId, ids));
859-
return ExtractDataFromAltDownload(res);
865+
866+
var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids);
867+
return await DownloadDataAltAndExtractAsync(url);
860868
}
861869

862870
/// <summary>
@@ -999,8 +1007,9 @@ public ListResponse<bool> CheckSavedTracks(List<string> ids)
9991007
{
10001008
if (!UseAuth)
10011009
throw new InvalidOperationException("Auth is required for CheckSavedTracks");
1002-
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(_builder.CheckSavedTracks(ids));
1003-
return ExtractDataFromAltDownload(res);
1010+
1011+
var url = _builder.CheckSavedTracks(ids);
1012+
return DownloadDataAltAndExtract(url);
10041013
}
10051014

10061015
/// <summary>
@@ -1013,8 +1022,8 @@ public async Task<ListResponse<bool>> CheckSavedTracksAsync(List<string> ids)
10131022
{
10141023
if (!UseAuth)
10151024
throw new InvalidOperationException("Auth is required for CheckSavedTracks");
1016-
Tuple<ResponseInfo, JToken> res = await DownloadDataAltAsync<JToken>(_builder.CheckSavedTracks(ids));
1017-
return ExtractDataFromAltDownload(res);
1025+
var url = _builder.CheckSavedTracks(ids);
1026+
return await DownloadDataAltAndExtractAsync(url);
10181027
}
10191028

10201029
/// <summary>
@@ -1127,8 +1136,9 @@ public ListResponse<bool> CheckSavedAlbums(List<string> ids)
11271136
{
11281137
if (!UseAuth)
11291138
throw new InvalidOperationException("Auth is required for CheckSavedTracks");
1130-
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(_builder.CheckSavedAlbums(ids));
1131-
return ExtractDataFromAltDownload(res);
1139+
1140+
var url = _builder.CheckSavedAlbums(ids);
1141+
return DownloadDataAltAndExtract(url);
11321142
}
11331143

11341144
/// <summary>
@@ -1141,8 +1151,8 @@ public async Task<ListResponse<bool>> CheckSavedAlbumsAsync(List<string> ids)
11411151
{
11421152
if (!UseAuth)
11431153
throw new InvalidOperationException("Auth is required for CheckSavedAlbumsAsync");
1144-
Tuple<ResponseInfo, JToken> res = await DownloadDataAltAsync<JToken>(_builder.CheckSavedAlbums(ids));
1145-
return ExtractDataFromAltDownload(res);
1154+
var url = _builder.CheckSavedAlbums(ids);
1155+
return await DownloadDataAltAndExtractAsync(url);
11461156
}
11471157

11481158
#endregion Library
@@ -1816,6 +1826,46 @@ public async Task<Paging<T>> GetPreviousPageAsync<T>(Paging<T> paging)
18161826
return await GetPreviousPageAsync<Paging<T>, T>(paging);
18171827
}
18181828

1829+
private ListResponse<bool> DownloadDataAltAndExtract(string url)
1830+
{
1831+
int triesLeft = RetryTimes + 1;
1832+
Error lastError = null;
1833+
1834+
ListResponse<bool> data = null;
1835+
do
1836+
{
1837+
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(url);
1838+
data = ExtractDataFromAltDownload(res);
1839+
1840+
lastError = data.Error;
1841+
1842+
triesLeft -= 1;
1843+
1844+
} while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status));
1845+
1846+
return data;
1847+
}
1848+
1849+
private async Task<ListResponse<bool>> DownloadDataAltAndExtractAsync(string url)
1850+
{
1851+
int triesLeft = RetryTimes + 1;
1852+
Error lastError = null;
1853+
1854+
ListResponse<bool> data = null;
1855+
do
1856+
{
1857+
Tuple<ResponseInfo, JToken> res = await DownloadDataAltAsync<JToken>(url);
1858+
data = ExtractDataFromAltDownload(res);
1859+
1860+
lastError = data.Error;
1861+
1862+
triesLeft -= 1;
1863+
1864+
} while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status));
1865+
1866+
return data;
1867+
}
1868+
18191869
private static ListResponse<bool> ExtractDataFromAltDownload(Tuple<ResponseInfo, JToken> res)
18201870
{
18211871
ListResponse<bool> ret = null;
@@ -1855,7 +1905,7 @@ public T UploadData<T>(string url, string uploadData, string method = "POST") wh
18551905

18561906
triesLeft -= 1;
18571907

1858-
} while (UseAutoRetry && triesLeft > 0 && lastError != null);
1908+
} while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status));
18591909

18601910
return response.Item2;
18611911
}
@@ -1881,7 +1931,7 @@ public async Task<T> UploadDataAsync<T>(string url, string uploadData, string me
18811931

18821932
triesLeft -= 1;
18831933

1884-
} while (UseAutoRetry && triesLeft > 0 && lastError != null);
1934+
} while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status));
18851935

18861936
return response.Item2;
18871937
}
@@ -1901,7 +1951,7 @@ public T DownloadData<T>(string url) where T : BasicModel
19011951

19021952
triesLeft -= 1;
19031953

1904-
} while (UseAutoRetry && triesLeft > 0 && lastError != null);
1954+
} while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status));
19051955

19061956

19071957
return response.Item2;
@@ -1922,7 +1972,7 @@ public async Task<T> DownloadDataAsync<T>(string url) where T : BasicModel
19221972

19231973
triesLeft -= 1;
19241974

1925-
} while (UseAutoRetry && triesLeft > 0 && lastError != null);
1975+
} while (UseAutoRetry && triesLeft > 0 && lastError != null && RetryErrorCodes.Contains(lastError.Status));
19261976

19271977

19281978
return response.Item2;

0 commit comments

Comments
 (0)