Skip to content

Commit 9952f3e

Browse files
committed
Generalized Download list functions.
1 parent 0fb4a5b commit 9952f3e

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

‎SpotifyAPI/Web/SpotifyWebAPI.cs‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ public ListResponse<bool> IsFollowing(FollowType followType, List<string> ids)
718718
throw new InvalidOperationException("Auth is required for IsFollowing");
719719

720720
var url = _builder.IsFollowing(followType, ids);
721-
return DownloadDataAltAndExtract(url);
721+
return DownloadList<bool>(url);
722722
}
723723

724724

@@ -736,7 +736,7 @@ public async Task<ListResponse<bool>> IsFollowingAsync(FollowType followType, Li
736736
throw new InvalidOperationException("Auth is required for IsFollowing");
737737

738738
var url = _builder.IsFollowing(followType, ids);
739-
return await DownloadDataAltAndExtractAsync(url);
739+
return await DownloadListAsync<bool>(url);
740740
}
741741

742742
/// <summary>
@@ -847,7 +847,7 @@ public ListResponse<bool> IsFollowingPlaylist(string ownerId, string playlistId,
847847
throw new InvalidOperationException("Auth is required for IsFollowingPlaylist");
848848

849849
var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids);
850-
return DownloadDataAltAndExtract(url);
850+
return DownloadList<bool>(url);
851851
}
852852

853853
/// <summary>
@@ -864,7 +864,7 @@ public async Task<ListResponse<bool>> IsFollowingPlaylistAsync(string ownerId, s
864864
throw new InvalidOperationException("Auth is required for IsFollowingPlaylist");
865865

866866
var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids);
867-
return await DownloadDataAltAndExtractAsync(url);
867+
return await DownloadListAsync<bool>(url);
868868
}
869869

870870
/// <summary>
@@ -1009,7 +1009,7 @@ public ListResponse<bool> CheckSavedTracks(List<string> ids)
10091009
throw new InvalidOperationException("Auth is required for CheckSavedTracks");
10101010

10111011
var url = _builder.CheckSavedTracks(ids);
1012-
return DownloadDataAltAndExtract(url);
1012+
return DownloadList<bool>(url);
10131013
}
10141014

10151015
/// <summary>
@@ -1023,7 +1023,7 @@ public async Task<ListResponse<bool>> CheckSavedTracksAsync(List<string> ids)
10231023
if (!UseAuth)
10241024
throw new InvalidOperationException("Auth is required for CheckSavedTracks");
10251025
var url = _builder.CheckSavedTracks(ids);
1026-
return await DownloadDataAltAndExtractAsync(url);
1026+
return await DownloadListAsync<bool>(url);
10271027
}
10281028

10291029
/// <summary>
@@ -1138,7 +1138,7 @@ public ListResponse<bool> CheckSavedAlbums(List<string> ids)
11381138
throw new InvalidOperationException("Auth is required for CheckSavedTracks");
11391139

11401140
var url = _builder.CheckSavedAlbums(ids);
1141-
return DownloadDataAltAndExtract(url);
1141+
return DownloadList<bool>(url);
11421142
}
11431143

11441144
/// <summary>
@@ -1152,7 +1152,7 @@ public async Task<ListResponse<bool>> CheckSavedAlbumsAsync(List<string> ids)
11521152
if (!UseAuth)
11531153
throw new InvalidOperationException("Auth is required for CheckSavedAlbumsAsync");
11541154
var url = _builder.CheckSavedAlbums(ids);
1155-
return await DownloadDataAltAndExtractAsync(url);
1155+
return await DownloadListAsync<bool>(url);
11561156
}
11571157

11581158
#endregion Library
@@ -1826,17 +1826,17 @@ public async Task<Paging<T>> GetPreviousPageAsync<T>(Paging<T> paging)
18261826
return await GetPreviousPageAsync<Paging<T>, T>(paging);
18271827
}
18281828

1829-
private ListResponse<bool> DownloadDataAltAndExtract(string url)
1829+
private ListResponse<T> DownloadList<T>(string url)
18301830
{
18311831
int triesLeft = RetryTimes + 1;
18321832
Error lastError = null;
18331833

1834-
ListResponse<bool> data = null;
1834+
ListResponse<T> data = null;
18351835
do
18361836
{
18371837
if (data != null) { System.Threading.Thread.Sleep(RetryAfter); }
18381838
Tuple<ResponseInfo, JToken> res = DownloadDataAlt<JToken>(url);
1839-
data = ExtractDataFromAltDownload(res);
1839+
data = ExtractDataToListResponse<T>(res);
18401840

18411841
lastError = data.Error;
18421842

@@ -1847,17 +1847,17 @@ private ListResponse<bool> DownloadDataAltAndExtract(string url)
18471847
return data;
18481848
}
18491849

1850-
private async Task<ListResponse<bool>> DownloadDataAltAndExtractAsync(string url)
1850+
private async Task<ListResponse<T>> DownloadListAsync<T>(string url)
18511851
{
18521852
int triesLeft = RetryTimes + 1;
18531853
Error lastError = null;
18541854

1855-
ListResponse<bool> data = null;
1855+
ListResponse<T> data = null;
18561856
do
18571857
{
18581858
if (data != null) { await Task.Delay(RetryAfter); }
18591859
Tuple<ResponseInfo, JToken> res = await DownloadDataAltAsync<JToken>(url);
1860-
data = ExtractDataFromAltDownload(res);
1860+
data = ExtractDataToListResponse<T>(res);
18611861

18621862
lastError = data.Error;
18631863

@@ -1868,21 +1868,25 @@ private async Task<ListResponse<bool>> DownloadDataAltAndExtractAsync(string url
18681868
return data;
18691869
}
18701870

1871-
private static ListResponse<bool> ExtractDataFromAltDownload(Tuple<ResponseInfo, JToken> res)
1871+
private static ListResponse<T> ExtractDataToListResponse<T>(Tuple<ResponseInfo, JToken> res)
18721872
{
1873-
ListResponse<bool> ret = null;
1873+
ListResponse<T> ret = null;
18741874
if (res.Item2 is JArray)
1875-
ret = new ListResponse<bool>
1875+
{
1876+
ret = new ListResponse<T>
18761877
{
1877-
List = res.Item2.ToObject<List<bool>>(),
1878+
List = res.Item2.ToObject<List<T>>(),
18781879
Error = null
18791880
};
1881+
}
18801882
else
1881-
ret = new ListResponse<bool>
1883+
{
1884+
ret = new ListResponse<T>
18821885
{
18831886
List = null,
18841887
Error = res.Item2["error"].ToObject<Error>()
18851888
};
1889+
}
18861890
ret.AddResponseInfo(res.Item1);
18871891
return ret;
18881892
}

0 commit comments

Comments
 (0)