From a67305cb1434c295874ced82cba29c0d69444f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Hou=C5=A1ka?= Date: Thu, 8 Sep 2016 08:30:33 +0200 Subject: [PATCH 1/3] Reworked async/await in WebAPI --- SpotifyAPI/Web/SpotifyWebAPI.cs | 235 ++++++++++++++++---------------- 1 file changed, 117 insertions(+), 118 deletions(-) diff --git a/SpotifyAPI/Web/SpotifyWebAPI.cs b/SpotifyAPI/Web/SpotifyWebAPI.cs index 5892641a..16f617d1 100644 --- a/SpotifyAPI/Web/SpotifyWebAPI.cs +++ b/SpotifyAPI/Web/SpotifyWebAPI.cs @@ -110,9 +110,9 @@ namespace SpotifyAPI.Web /// The index of the first result to return. Default: 0 /// An ISO 3166-1 alpha-2 country code or the string from_token. /// - public async Task SearchItemsAsync(string q, SearchType type, int limit = 20, int offset = 0, string market = "") + public Task SearchItemsAsync(string q, SearchType type, int limit = 20, int offset = 0, string market = "") { - return await DownloadDataAsync(_builder.SearchItems(q, type, limit, offset, market)); + return DownloadDataAsync(_builder.SearchItems(q, type, limit, offset, market)); } #endregion Search @@ -142,9 +142,9 @@ namespace SpotifyAPI.Web /// The index of the first track to return. Default: 0 (the first object). /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// - public async Task> GetAlbumTracksAsync(string id, int limit = 20, int offset = 0, string market = "") + public Task> GetAlbumTracksAsync(string id, int limit = 20, int offset = 0, string market = "") { - return await DownloadDataAsync>(_builder.GetAlbumTracks(id, limit, offset, market)); + return DownloadDataAsync>(_builder.GetAlbumTracks(id, limit, offset, market)); } /// @@ -164,9 +164,9 @@ namespace SpotifyAPI.Web /// The Spotify ID for the album. /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// - public async Task GetAlbumAsync(string id, string market = "") + public Task GetAlbumAsync(string id, string market = "") { - return await DownloadDataAsync(_builder.GetAlbum(id, market)); + return DownloadDataAsync(_builder.GetAlbum(id, market)); } /// @@ -186,9 +186,9 @@ namespace SpotifyAPI.Web /// A list of the Spotify IDs for the albums. Maximum: 20 IDs. /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// - public async Task GetSeveralAlbumsAsync(List ids, string market = "") + public Task GetSeveralAlbumsAsync(List ids, string market = "") { - return await DownloadDataAsync(_builder.GetSeveralAlbums(ids, market)); + return DownloadDataAsync(_builder.GetSeveralAlbums(ids, market)); } #endregion Albums @@ -210,9 +210,9 @@ namespace SpotifyAPI.Web /// /// The Spotify ID for the artist. /// - public async Task GetArtistAsync(string id) + public Task GetArtistAsync(string id) { - return await DownloadDataAsync(_builder.GetArtist(id)); + return DownloadDataAsync(_builder.GetArtist(id)); } /// @@ -232,9 +232,9 @@ namespace SpotifyAPI.Web /// /// The Spotify ID for the artist. /// - public async Task GetRelatedArtistsAsync(string id) + public Task GetRelatedArtistsAsync(string id) { - return await DownloadDataAsync(_builder.GetRelatedArtists(id)); + return DownloadDataAsync(_builder.GetRelatedArtists(id)); } /// @@ -254,9 +254,9 @@ namespace SpotifyAPI.Web /// The Spotify ID for the artist. /// The country: an ISO 3166-1 alpha-2 country code. /// - public async Task GetArtistsTopTracksAsync(string id, string country) + public Task GetArtistsTopTracksAsync(string id, string country) { - return await DownloadDataAsync(_builder.GetArtistsTopTracks(id, country)); + return DownloadDataAsync(_builder.GetArtistsTopTracks(id, country)); } /// @@ -296,9 +296,9 @@ namespace SpotifyAPI.Web /// geographical market /// /// - public async Task> GetArtistsAlbumsAsync(string id, AlbumType type = AlbumType.All, int limit = 20, int offset = 0, string market = "") + public Task> GetArtistsAlbumsAsync(string id, AlbumType type = AlbumType.All, int limit = 20, int offset = 0, string market = "") { - return await DownloadDataAsync>(_builder.GetArtistsAlbums(id, type, limit, offset, market)); + return DownloadDataAsync>(_builder.GetArtistsAlbums(id, type, limit, offset, market)); } /// @@ -316,9 +316,9 @@ namespace SpotifyAPI.Web /// /// A list of the Spotify IDs for the artists. Maximum: 50 IDs. /// - public async Task GetSeveralArtistsAsync(List ids) + public Task GetSeveralArtistsAsync(List ids) { - return await DownloadDataAsync(_builder.GetSeveralArtists(ids)); + return DownloadDataAsync(_builder.GetSeveralArtists(ids)); } #endregion Artists @@ -356,11 +356,11 @@ namespace SpotifyAPI.Web /// The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. /// The index of the first item to return. Default: 0 /// AUTH NEEDED - public async Task GetFeaturedPlaylistsAsync(string locale = "", string country = "", DateTime timestamp = default(DateTime), int limit = 20, int offset = 0) + public Task GetFeaturedPlaylistsAsync(string locale = "", string country = "", DateTime timestamp = default(DateTime), int limit = 20, int offset = 0) { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetFeaturedPlaylists"); - return await DownloadDataAsync(_builder.GetFeaturedPlaylists(locale, country, timestamp, limit, offset)); + return DownloadDataAsync(_builder.GetFeaturedPlaylists(locale, country, timestamp, limit, offset)); } /// @@ -386,11 +386,11 @@ namespace SpotifyAPI.Web /// The index of the first item to return. Default: 0 /// /// AUTH NEEDED - public async Task GetNewAlbumReleasesAsync(string country = "", int limit = 20, int offset = 0) + public Task GetNewAlbumReleasesAsync(string country = "", int limit = 20, int offset = 0) { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetNewAlbumReleases"); - return await DownloadDataAsync(_builder.GetNewAlbumReleases(country, limit, offset)); + return DownloadDataAsync(_builder.GetNewAlbumReleases(country, limit, offset)); } /// @@ -430,11 +430,11 @@ namespace SpotifyAPI.Web /// The index of the first item to return. Default: 0 (the first object). /// /// AUTH NEEDED - public async Task GetCategoriesAsync(string country = "", string locale = "", int limit = 20, int offset = 0) + public Task GetCategoriesAsync(string country = "", string locale = "", int limit = 20, int offset = 0) { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetCategories"); - return await DownloadDataAsync(_builder.GetCategories(country, locale, limit, offset)); + return DownloadDataAsync(_builder.GetCategories(country, locale, limit, offset)); } /// @@ -470,9 +470,9 @@ namespace SpotifyAPI.Web /// /// /// AUTH NEEDED - public async Task GetCategoryAsync(string categoryId, string country = "", string locale = "") + public Task GetCategoryAsync(string categoryId, string country = "", string locale = "") { - return await DownloadDataAsync(_builder.GetCategory(categoryId, country, locale)); + return DownloadDataAsync(_builder.GetCategory(categoryId, country, locale)); } /// @@ -498,9 +498,9 @@ namespace SpotifyAPI.Web /// The index of the first item to return. Default: 0 /// /// AUTH NEEDED - public async Task GetCategoryPlaylistsAsync(string categoryId, string country = "", int limit = 20, int offset = 0) + public Task GetCategoryPlaylistsAsync(string categoryId, string country = "", int limit = 20, int offset = 0) { - return await DownloadDataAsync(_builder.GetCategoryPlaylists(categoryId, country, limit, offset)); + return DownloadDataAsync(_builder.GetCategoryPlaylists(categoryId, country, limit, offset)); } /// @@ -553,10 +553,10 @@ namespace SpotifyAPI.Web /// Because min_*, max_* and target_* are applied to pools before relinking, the generated results may not precisely match the filters applied. /// /// AUTH NEEDED - public async Task GetRecommendationsAsync(List artistSeed = null, List genreSeed = null, List trackSeed = null, + public Task GetRecommendationsAsync(List artistSeed = null, List genreSeed = null, List trackSeed = null, TuneableTrack target = null, TuneableTrack min = null, TuneableTrack max = null, int limit = 20, string market = "") { - return await DownloadDataAsync(_builder.GetRecommendations(artistSeed, genreSeed, trackSeed, target, min, max, limit, market)); + return DownloadDataAsync(_builder.GetRecommendations(artistSeed, genreSeed, trackSeed, target, min, max, limit, market)); } /// @@ -574,9 +574,9 @@ namespace SpotifyAPI.Web /// /// /// AUTH NEEDED - public async Task GetRecommendationSeedsGenresAsync() + public Task GetRecommendationSeedsGenresAsync() { - return await DownloadDataAsync(_builder.GetRecommendationSeedsGenres()); + return DownloadDataAsync(_builder.GetRecommendationSeedsGenres()); } #endregion Browse @@ -606,11 +606,11 @@ namespace SpotifyAPI.Web /// The last artist ID retrieved from the previous request. /// /// AUTH NEEDED - public async Task GetFollowedArtistsAsync(FollowType followType, int limit = 20, string after = "") + public Task GetFollowedArtistsAsync(FollowType followType, int limit = 20, string after = "") { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetFollowedArtists"); - return await DownloadDataAsync(_builder.GetFollowedArtists(limit, after)); + return DownloadDataAsync(_builder.GetFollowedArtists(limit, after)); } /// @@ -642,9 +642,8 @@ namespace SpotifyAPI.Web { {"ids", new JArray(ids)} }; - return await - UploadDataAsync(_builder.Follow(followType), - ob.ToString(Formatting.None), "PUT") ?? new ErrorResponse(); + return (await UploadDataAsync(_builder.Follow(followType), + ob.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -666,9 +665,9 @@ namespace SpotifyAPI.Web /// Artists or the Users Spotify ID /// /// AUTH NEEDED - public async Task FollowAsync(FollowType followType, string id) + public Task FollowAsync(FollowType followType, string id) { - return await FollowAsync(followType, new List { id }); + return FollowAsync(followType, new List { id }); } /// @@ -700,7 +699,7 @@ namespace SpotifyAPI.Web { {"ids", new JArray(ids)} }; - return await UploadDataAsync(_builder.Unfollow(followType), ob.ToString(Formatting.None), "DELETE") ?? new ErrorResponse(); + return (await UploadDataAsync(_builder.Unfollow(followType), ob.ToString(Formatting.None), "DELETE").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -722,9 +721,9 @@ namespace SpotifyAPI.Web /// Artists or the Users Spotify ID /// /// AUTH NEEDED - public async Task UnfollowAsync(FollowType followType, string id) + public Task UnfollowAsync(FollowType followType, string id) { - return await UnfollowAsync(followType, new List { id }); + return UnfollowAsync(followType, new List { id }); } /// @@ -752,13 +751,13 @@ namespace SpotifyAPI.Web /// A list of the artist or the user Spotify IDs to check /// /// AUTH NEEDED - public async Task> IsFollowingAsync(FollowType followType, List ids) + public Task> IsFollowingAsync(FollowType followType, List ids) { if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowing"); var url = _builder.IsFollowing(followType, ids); - return await DownloadListAsync(url); + return DownloadListAsync(url); } /// @@ -780,9 +779,9 @@ namespace SpotifyAPI.Web /// Artists or the Users Spotify ID /// /// AUTH NEEDED - public async Task> IsFollowingAsync(FollowType followType, string id) + public Task> IsFollowingAsync(FollowType followType, string id) { - return await IsFollowingAsync(followType, new List { id }); + return IsFollowingAsync(followType, new List { id }); } /// @@ -822,13 +821,13 @@ namespace SpotifyAPI.Web /// /// /// AUTH NEEDED - public async Task FollowPlaylistAsync(string ownerId, string playlistId, bool showPublic = true) + public Task FollowPlaylistAsync(string ownerId, string playlistId, bool showPublic = true) { JObject body = new JObject { {"public", showPublic} }; - return await UploadDataAsync(_builder.FollowPlaylist(ownerId, playlistId, showPublic), body.ToString(Formatting.None), "PUT"); + return UploadDataAsync(_builder.FollowPlaylist(ownerId, playlistId, showPublic), body.ToString(Formatting.None), "PUT"); } /// @@ -850,9 +849,9 @@ namespace SpotifyAPI.Web /// The Spotify ID of the playlist that is to be no longer followed. /// /// AUTH NEEDED - public async Task UnfollowPlaylistAsync(string ownerId, string playlistId) + public Task UnfollowPlaylistAsync(string ownerId, string playlistId) { - return await UploadDataAsync(_builder.UnfollowPlaylist(ownerId, playlistId), "", "DELETE"); + return UploadDataAsync(_builder.UnfollowPlaylist(ownerId, playlistId), "", "DELETE"); } /// @@ -880,13 +879,13 @@ namespace SpotifyAPI.Web /// A list of Spotify User IDs /// /// AUTH NEEDED - public async Task> IsFollowingPlaylistAsync(string ownerId, string playlistId, List ids) + public Task> IsFollowingPlaylistAsync(string ownerId, string playlistId, List ids) { if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowingPlaylist"); var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids); - return await DownloadListAsync(url); + return DownloadListAsync(url); } /// @@ -910,9 +909,9 @@ namespace SpotifyAPI.Web /// A Spotify User ID /// /// AUTH NEEDED - public async Task> IsFollowingPlaylistAsync(string ownerId, string playlistId, string id) + public Task> IsFollowingPlaylistAsync(string ownerId, string playlistId, string id) { - return await IsFollowingPlaylistAsync(ownerId, playlistId, new List { id }); + return IsFollowingPlaylistAsync(ownerId, playlistId, new List { id }); } #endregion Follow @@ -940,7 +939,7 @@ namespace SpotifyAPI.Web public async Task SaveTracksAsync(List ids) { JArray array = new JArray(ids); - return await UploadDataAsync(_builder.SaveTracks(), array.ToString(Formatting.None), "PUT") ?? new ErrorResponse(); + return (await UploadDataAsync(_builder.SaveTracks(), array.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -960,9 +959,9 @@ namespace SpotifyAPI.Web /// A Spotify ID /// /// AUTH NEEDED - public async Task SaveTrackAsync(string id) + public Task SaveTrackAsync(string id) { - return await SaveTracksAsync(new List { id }); + return SaveTracksAsync(new List { id }); } /// @@ -988,11 +987,11 @@ namespace SpotifyAPI.Web /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// /// AUTH NEEDED - public async Task> GetSavedTracksAsync(int limit = 20, int offset = 0, string market = "") + public Task> GetSavedTracksAsync(int limit = 20, int offset = 0, string market = "") { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetSavedTracks"); - return await DownloadDataAsync>(_builder.GetSavedTracks(limit, offset, market)); + return DownloadDataAsync>(_builder.GetSavedTracks(limit, offset, market)); } /// @@ -1016,7 +1015,7 @@ namespace SpotifyAPI.Web public async Task RemoveSavedTracksAsync(List ids) { JArray array = new JArray(ids); - return await UploadDataAsync(_builder.RemoveSavedTracks(), array.ToString(Formatting.None), "DELETE") ?? new ErrorResponse(); + return (await UploadDataAsync(_builder.RemoveSavedTracks(), array.ToString(Formatting.None), "DELETE").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -1040,12 +1039,12 @@ namespace SpotifyAPI.Web /// A list of the Spotify IDs. /// /// AUTH NEEDED - public async Task> CheckSavedTracksAsync(List ids) + public Task> CheckSavedTracksAsync(List ids) { if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedTracks"); var url = _builder.CheckSavedTracks(ids); - return await DownloadListAsync(url); + return DownloadListAsync(url); } /// @@ -1069,7 +1068,7 @@ namespace SpotifyAPI.Web public async Task SaveAlbumsAsync(List ids) { JArray array = new JArray(ids); - return await UploadDataAsync(_builder.SaveAlbums(), array.ToString(Formatting.None), "PUT") ?? new ErrorResponse(); + return (await UploadDataAsync(_builder.SaveAlbums(), array.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -1089,9 +1088,9 @@ namespace SpotifyAPI.Web /// A Spotify ID /// /// AUTH NEEDED - public async Task SaveAlbumAsync(string id) + public Task SaveAlbumAsync(string id) { - return await SaveAlbumsAsync(new List { id }); + return SaveAlbumsAsync(new List { id }); } /// @@ -1117,11 +1116,11 @@ namespace SpotifyAPI.Web /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// /// AUTH NEEDED - public async Task> GetSavedAlbumsAsync(int limit = 20, int offset = 0, string market = "") + public Task> GetSavedAlbumsAsync(int limit = 20, int offset = 0, string market = "") { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetSavedAlbumsAsync"); - return await DownloadDataAsync>(_builder.GetSavedAlbums(limit, offset, market)); + return DownloadDataAsync>(_builder.GetSavedAlbums(limit, offset, market)); } /// @@ -1145,7 +1144,7 @@ namespace SpotifyAPI.Web public async Task RemoveSavedAlbumsAsync(List ids) { JArray array = new JArray(ids); - return await UploadDataAsync(_builder.RemoveSavedAlbums(), array.ToString(Formatting.None), "DELETE") ?? new ErrorResponse(); + return (await UploadDataAsync(_builder.RemoveSavedAlbums(), array.ToString(Formatting.None), "DELETE").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -1169,12 +1168,12 @@ namespace SpotifyAPI.Web /// A list of the Spotify IDs. /// /// AUTH NEEDED - public async Task> CheckSavedAlbumsAsync(List ids) + public Task> CheckSavedAlbumsAsync(List ids) { if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedAlbumsAsync"); var url = _builder.CheckSavedAlbums(ids); - return await DownloadListAsync(url); + return DownloadListAsync(url); } #endregion Library @@ -1206,9 +1205,9 @@ namespace SpotifyAPI.Web /// The index of the first entity to return. Default: 0 (i.e., the first track). Use with limit to get the next set of entities. /// /// AUTH NEEDED - public async Task> GetUsersTopTracksAsync(TimeRangeType timeRange = TimeRangeType.MediumTerm, int limit = 20, int offest = 0) + public Task> GetUsersTopTracksAsync(TimeRangeType timeRange = TimeRangeType.MediumTerm, int limit = 20, int offest = 0) { - return await DownloadDataAsync>(_builder.GetUsersTopTracks(timeRange, limit, offest)); + return DownloadDataAsync>(_builder.GetUsersTopTracks(timeRange, limit, offest)); } /// @@ -1236,9 +1235,9 @@ namespace SpotifyAPI.Web /// The index of the first entity to return. Default: 0 (i.e., the first track). Use with limit to get the next set of entities. /// /// AUTH NEEDED - public async Task> GetUsersTopArtistsAsync(TimeRangeType timeRange = TimeRangeType.MediumTerm, int limit = 20, int offest = 0) + public Task> GetUsersTopArtistsAsync(TimeRangeType timeRange = TimeRangeType.MediumTerm, int limit = 20, int offest = 0) { - return await DownloadDataAsync>(_builder.GetUsersTopArtists(timeRange, limit, offest)); + return DownloadDataAsync>(_builder.GetUsersTopArtists(timeRange, limit, offest)); } #endregion @@ -1268,11 +1267,11 @@ namespace SpotifyAPI.Web /// The index of the first playlist to return. Default: 0 (the first object) /// /// AUTH NEEDED - public async Task> GetUserPlaylistsAsync(string userId, int limit = 20, int offset = 0) + public Task> GetUserPlaylistsAsync(string userId, int limit = 20, int offset = 0) { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetUserPlaylists"); - return await DownloadDataAsync>(_builder.GetUserPlaylists(userId, limit, offset)); + return DownloadDataAsync>(_builder.GetUserPlaylists(userId, limit, offset)); } /// @@ -1306,11 +1305,11 @@ namespace SpotifyAPI.Web /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// /// AUTH NEEDED - public async Task GetPlaylistAsync(string userId, string playlistId, string fields = "", string market = "") + public Task GetPlaylistAsync(string userId, string playlistId, string fields = "", string market = "") { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetPlaylist"); - return await DownloadDataAsync(_builder.GetPlaylist(userId, playlistId, fields, market)); + return DownloadDataAsync(_builder.GetPlaylist(userId, playlistId, fields, market)); } /// @@ -1348,11 +1347,11 @@ namespace SpotifyAPI.Web /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// /// AUTH NEEDED - public async Task> GetPlaylistTracksAsync(string userId, string playlistId, string fields = "", int limit = 100, int offset = 0, string market = "") + public Task> GetPlaylistTracksAsync(string userId, string playlistId, string fields = "", int limit = 100, int offset = 0, string market = "") { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetPlaylistTracks"); - return await DownloadDataAsync>(_builder.GetPlaylistTracks(userId, playlistId, fields, limit, offset, market)); + return DownloadDataAsync>(_builder.GetPlaylistTracks(userId, playlistId, fields, limit, offset, market)); } /// @@ -1393,14 +1392,14 @@ namespace SpotifyAPI.Web /// /// /// AUTH NEEDED - public async Task CreatePlaylistAsync(string userId, string playlistName, bool isPublic = true) + public Task CreatePlaylistAsync(string userId, string playlistName, bool isPublic = true) { JObject body = new JObject { {"name", playlistName}, {"public", isPublic} }; - return await UploadDataAsync(_builder.CreatePlaylist(userId, playlistName, isPublic), body.ToString(Formatting.None)); + return UploadDataAsync(_builder.CreatePlaylist(userId, playlistName, isPublic), body.ToString(Formatting.None)); } /// @@ -1438,7 +1437,7 @@ namespace SpotifyAPI.Web body.Add("name", newName); if (newPublic != null) body.Add("public", newPublic); - return await UploadDataAsync(_builder.UpdatePlaylist(userId, playlistId), body.ToString(Formatting.None), "PUT") ?? new ErrorResponse(); + return (await UploadDataAsync(_builder.UpdatePlaylist(userId, playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -1474,7 +1473,7 @@ namespace SpotifyAPI.Web { {"uris", new JArray(uris.Take(100))} }; - return await UploadDataAsync(_builder.ReplacePlaylistTracks(userId, playlistId), body.ToString(Formatting.None), "PUT") ?? new ErrorResponse(); + return await (UploadDataAsync(_builder.ReplacePlaylistTracks(userId, playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -1514,7 +1513,7 @@ namespace SpotifyAPI.Web { {"tracks", JArray.FromObject(uris.Take(100))} }; - return await UploadDataAsync(_builder.RemovePlaylistTracks(userId, playlistId, uris), body.ToString(Formatting.None), "DELETE") ?? new ErrorResponse(); + return await (UploadDataAsync(_builder.RemovePlaylistTracks(userId, playlistId, uris), body.ToString(Formatting.None), "DELETE").ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -1538,9 +1537,9 @@ namespace SpotifyAPI.Web /// Spotify URI /// /// AUTH NEEDED - public async Task RemovePlaylistTrackAsync(string userId, string playlistId, DeleteTrackUri uri) + public Task RemovePlaylistTrackAsync(string userId, string playlistId, DeleteTrackUri uri) { - return await RemovePlaylistTracksAsync(userId, playlistId, new List { uri }); + return RemovePlaylistTracksAsync(userId, playlistId, new List { uri }); } /// @@ -1576,7 +1575,7 @@ namespace SpotifyAPI.Web { {"uris", JArray.FromObject(uris.Take(100))} }; - return await UploadDataAsync(_builder.AddPlaylistTracks(userId, playlistId, uris, position), body.ToString(Formatting.None)) ?? new ErrorResponse(); + return await (UploadDataAsync(_builder.AddPlaylistTracks(userId, playlistId, uris, position), body.ToString(Formatting.None)).ConfigureAwait(false)) ?? new ErrorResponse(); } /// @@ -1602,9 +1601,9 @@ namespace SpotifyAPI.Web /// The position to insert the tracks, a zero-based index /// /// AUTH NEEDED - public async Task AddPlaylistTrackAsync(string userId, string playlistId, string uri, int? position = null) + public Task AddPlaylistTrackAsync(string userId, string playlistId, string uri, int? position = null) { - return await AddPlaylistTracksAsync(userId, playlistId, new List { uri }, position); + return AddPlaylistTracksAsync(userId, playlistId, new List { uri }, position); } /// @@ -1642,7 +1641,7 @@ namespace SpotifyAPI.Web /// The playlist's snapshot ID against which you want to make the changes. /// /// AUTH NEEDED - public async Task ReorderPlaylistAsync(string userId, string playlistId, int rangeStart, int insertBefore, int rangeLength = 1, string snapshotId = "") + public Task ReorderPlaylistAsync(string userId, string playlistId, int rangeStart, int insertBefore, int rangeLength = 1, string snapshotId = "") { JObject body = new JObject { @@ -1653,7 +1652,7 @@ namespace SpotifyAPI.Web }; if (!string.IsNullOrEmpty(snapshotId)) body.Add("snapshot_id", snapshotId); - return await UploadDataAsync(_builder.ReorderPlaylist(userId, playlistId), body.ToString(Formatting.None), "PUT"); + return UploadDataAsync(_builder.ReorderPlaylist(userId, playlistId), body.ToString(Formatting.None), "PUT"); } #endregion Playlists @@ -1677,11 +1676,11 @@ namespace SpotifyAPI.Web /// /// /// AUTH NEEDED - public async Task GetPrivateProfileAsync() + public Task GetPrivateProfileAsync() { if (!UseAuth) throw new InvalidOperationException("Auth is required for GetPrivateProfile"); - return await DownloadDataAsync(_builder.GetPrivateProfile()); + return DownloadDataAsync(_builder.GetPrivateProfile()); } /// @@ -1699,9 +1698,9 @@ namespace SpotifyAPI.Web /// /// The user's Spotify user ID. /// - public async Task GetPublicProfileAsync(string userId) + public Task GetPublicProfileAsync(string userId) { - return await DownloadDataAsync(_builder.GetPublicProfile(userId)); + return DownloadDataAsync(_builder.GetPublicProfile(userId)); } #endregion Profiles @@ -1725,9 +1724,9 @@ namespace SpotifyAPI.Web /// A list of the Spotify IDs for the tracks. Maximum: 50 IDs. /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// - public async Task GetSeveralTracksAsync(List ids, string market = "") + public Task GetSeveralTracksAsync(List ids, string market = "") { - return await DownloadDataAsync(_builder.GetSeveralTracks(ids, market)); + return DownloadDataAsync(_builder.GetSeveralTracks(ids, market)); } /// @@ -1747,9 +1746,9 @@ namespace SpotifyAPI.Web /// The Spotify ID for the track. /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. /// - public async Task GetTrackAsync(string id, string market = "") + public Task GetTrackAsync(string id, string market = "") { - return await DownloadDataAsync(_builder.GetTrack(id, market)); + return DownloadDataAsync(_builder.GetTrack(id, market)); } /// @@ -1769,9 +1768,9 @@ namespace SpotifyAPI.Web /// The Spotify ID for the track. /// /// AUTH NEEDED - public async Task GetAudioFeaturesAsync(string id) + public Task GetAudioFeaturesAsync(string id) { - return await DownloadDataAsync(_builder.GetAudioFeatures(id)); + return DownloadDataAsync(_builder.GetAudioFeatures(id)); } /// @@ -1791,9 +1790,9 @@ namespace SpotifyAPI.Web /// A list of Spotify Track-IDs. Maximum: 100 IDs. /// /// AUTH NEEDED - public async Task GetSeveralAudioFeaturesAsync(List ids) + public Task GetSeveralAudioFeaturesAsync(List ids) { - return await DownloadDataAsync(_builder.GetSeveralAudioFeatures(ids)); + return DownloadDataAsync(_builder.GetSeveralAudioFeatures(ids)); } #endregion Tracks @@ -1812,16 +1811,16 @@ namespace SpotifyAPI.Web return GetNextPage, T>(paging); } - public async Task GetNextPageAsync(Paging paging) where TOut : BasicModel + public Task GetNextPageAsync(Paging paging) where TOut : BasicModel { if (!paging.HasNextPage()) throw new InvalidOperationException("This Paging-Object has no Next-Page"); - return await DownloadDataAsync(paging.Next); + return DownloadDataAsync(paging.Next); } - public async Task> GetNextPageAsync(Paging paging) + public Task> GetNextPageAsync(Paging paging) { - return await GetNextPageAsync, T>(paging); + return GetNextPageAsync, T>(paging); } public TOut GetPreviousPage(Paging paging) where TOut : BasicModel @@ -1836,16 +1835,16 @@ namespace SpotifyAPI.Web return GetPreviousPage, T>(paging); } - public async Task GetPreviousPageAsync(Paging paging) where TOut : BasicModel + public Task GetPreviousPageAsync(Paging paging) where TOut : BasicModel { if (!paging.HasPreviousPage()) throw new InvalidOperationException("This Paging-Object has no Previous-Page"); - return await DownloadDataAsync(paging.Previous); + return DownloadDataAsync(paging.Previous); } - public async Task> GetPreviousPageAsync(Paging paging) + public Task> GetPreviousPageAsync(Paging paging) { - return await GetPreviousPageAsync, T>(paging); + return GetPreviousPageAsync, T>(paging); } private ListResponse DownloadList(string url) @@ -1877,8 +1876,8 @@ namespace SpotifyAPI.Web ListResponse data = null; do { - if (data != null) { await Task.Delay(RetryAfter); } - Tuple res = await DownloadDataAltAsync(url); + if (data != null) { await Task.Delay(RetryAfter).ConfigureAwait(false); } + Tuple res = await DownloadDataAltAsync(url).ConfigureAwait(false); data = ExtractDataToListResponse(res); lastError = data.Error; @@ -1953,8 +1952,8 @@ namespace SpotifyAPI.Web WebClient.SetHeader("Authorization", TokenType + " " + AccessToken); WebClient.SetHeader("Content-Type", "application/json"); - if (response != null) { await Task.Delay(RetryAfter); } - response = await WebClient.UploadJsonAsync(url, uploadData, method); + if (response != null) { await Task.Delay(RetryAfter).ConfigureAwait(false); } + response = await WebClient.UploadJsonAsync(url, uploadData, method).ConfigureAwait(false); response.Item2.AddResponseInfo(response.Item1); lastError = response.Item2.Error; @@ -1996,8 +1995,8 @@ namespace SpotifyAPI.Web Tuple response = null; do { - if (response != null) { await Task.Delay(RetryAfter); } - response = await DownloadDataAltAsync(url); + if (response != null) { await Task.Delay(RetryAfter).ConfigureAwait(false); } + response = await DownloadDataAltAsync(url).ConfigureAwait(false); response.Item2.AddResponseInfo(response.Item1); lastError = response.Item2.Error; From 80f58df92934c178984c7fef9d879b24851c3453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Hou=C5=A1ka?= Date: Thu, 8 Sep 2016 12:06:24 +0200 Subject: [PATCH 2/3] Fixed rest of awaits. --- SpotifyAPI/Local/Models/Track.cs | 8 +++++--- SpotifyAPI/Local/RemoteHandler.cs | 10 +++++----- SpotifyAPI/Web/SpotifyWebClient.cs | 12 ++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/SpotifyAPI/Local/Models/Track.cs b/SpotifyAPI/Local/Models/Track.cs index 10b1f3bf..c439614a 100755 --- a/SpotifyAPI/Local/Models/Track.cs +++ b/SpotifyAPI/Local/Models/Track.cs @@ -103,7 +103,9 @@ namespace SpotifyAPI.Local.Models string url = GetAlbumArtUrl(size); if (url == "") return null; - var data = await wc.DownloadDataTaskAsync(url); + var data = + + wc.DownloadDataTaskAsync(url); using (MemoryStream ms = new MemoryStream(data)) { return (Bitmap)Image.FromStream(ms); @@ -116,7 +118,7 @@ namespace SpotifyAPI.Local.Models /// /// AlbumArtSize (160,320,640) /// A byte[], which is the albumart in binary data - public async Task GetAlbumArtAsByteArrayAsync(AlbumArtSize size) + public Task GetAlbumArtAsByteArrayAsync(AlbumArtSize size) { using (WebClient wc = new WebClient()) { @@ -124,7 +126,7 @@ namespace SpotifyAPI.Local.Models string url = GetAlbumArtUrl(size); if (url == "") return null; - return await wc.DownloadDataTaskAsync(url); + return wc.DownloadDataTaskAsync(url); } } diff --git a/SpotifyAPI/Local/RemoteHandler.cs b/SpotifyAPI/Local/RemoteHandler.cs index 6e627d7d..41e66192 100644 --- a/SpotifyAPI/Local/RemoteHandler.cs +++ b/SpotifyAPI/Local/RemoteHandler.cs @@ -24,23 +24,23 @@ namespace SpotifyAPI.Local internal async void SendPauseRequest() { - await QueryAsync("remote/pause.json?pause=true", true, true, -1); + await QueryAsync("remote/pause.json?pause=true", true, true, -1).ConfigureAwait(false); } internal async void SendPlayRequest() { - await QueryAsync("remote/pause.json?pause=false", true, true, -1); + await QueryAsync("remote/pause.json?pause=false", true, true, -1).ConfigureAwait(false); } internal async void SendPlayRequest(string url, string context = "") { // TODO: instead of having an empty context, one way to fix the bug with the playback time beyond the length of a song would be to provide a 1-song context, and it would be fixed. - await QueryAsync($"remote/play.json?uri={url}&context={context}", true, true, -1); + await QueryAsync($"remote/play.json?uri={url}&context={context}", true, true, -1).ConfigureAwait(false); } internal async void SendQueueRequest(string url) { - await QueryAsync("remote/play.json?uri=" + url + "?action=queue", true, true, -1); + await QueryAsync("remote/play.json?uri=" + url + "?action=queue", true, true, -1).ConfigureAwait(false); } internal StatusResponse GetNewStatus() @@ -151,7 +151,7 @@ namespace SpotifyAPI.Local using (var wc = new ExtendedWebClient()) { if (SpotifyLocalAPI.IsSpotifyRunning()) - response = "[ " + await wc.DownloadStringTaskAsync(new Uri(address)) + " ]"; + response = "[ " + await wc.DownloadStringTaskAsync(new Uri(address)).ConfigureAwait(false) + " ]"; } } catch diff --git a/SpotifyAPI/Web/SpotifyWebClient.cs b/SpotifyAPI/Web/SpotifyWebClient.cs index 2375f05a..327f6039 100644 --- a/SpotifyAPI/Web/SpotifyWebClient.cs +++ b/SpotifyAPI/Web/SpotifyWebClient.cs @@ -57,7 +57,7 @@ namespace SpotifyAPI.Web Tuple response; try { - Tuple raw = await DownloadRawAsync(url); + Tuple raw = await DownloadRawAsync(url).ConfigureAwait(false); response = new Tuple(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}"); } catch (WebException e) @@ -91,7 +91,7 @@ namespace SpotifyAPI.Web webClient.Encoding = _encoding; webClient.Headers = _webClient.Headers; - byte[] data = await _webClient.DownloadDataTaskAsync(url); + byte[] data = await _webClient.DownloadDataTaskAsync(url).ConfigureAwait(false); ResponseInfo info = new ResponseInfo() { Headers = webClient.ResponseHeaders @@ -108,7 +108,7 @@ namespace SpotifyAPI.Web public async Task> DownloadJsonAsync(string url) { - Tuple response = await DownloadAsync(url); + Tuple response = await DownloadAsync(url).ConfigureAwait(false); return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); } @@ -138,7 +138,7 @@ namespace SpotifyAPI.Web Tuple response; try { - Tuple data = await UploadRawAsync(url, body, method); + Tuple data = await UploadRawAsync(url, body, method).ConfigureAwait(false); response = new Tuple(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}"); } catch (WebException e) @@ -171,7 +171,7 @@ namespace SpotifyAPI.Web webClient.Proxy = null; webClient.Encoding = _encoding; webClient.Headers = _webClient.Headers; - byte[] data = await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body)); + byte[] data = await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body)).ConfigureAwait(false); ResponseInfo info = new ResponseInfo { Headers = _webClient.ResponseHeaders @@ -188,7 +188,7 @@ namespace SpotifyAPI.Web public async Task> UploadJsonAsync(string url, string body, string method) { - Tuple response = await UploadAsync(url, body, method); + Tuple response = await UploadAsync(url, body, method).ConfigureAwait(false); return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); } From 1ae1f2380a77b75e35dbd226f4b09f917d1119d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Hou=C5=A1ka?= Date: Thu, 8 Sep 2016 12:24:16 +0200 Subject: [PATCH 3/3] Fixed missed await. --- SpotifyAPI/Local/Models/Track.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SpotifyAPI/Local/Models/Track.cs b/SpotifyAPI/Local/Models/Track.cs index c439614a..36f45f84 100755 --- a/SpotifyAPI/Local/Models/Track.cs +++ b/SpotifyAPI/Local/Models/Track.cs @@ -103,9 +103,7 @@ namespace SpotifyAPI.Local.Models string url = GetAlbumArtUrl(size); if (url == "") return null; - var data = - - wc.DownloadDataTaskAsync(url); + var data = await wc.DownloadDataTaskAsync(url).ConfigureAwait(false); using (MemoryStream ms = new MemoryStream(data)) { return (Bitmap)Image.FromStream(ms);