From 3917fbefdc10d6c6f95a79279a7631f289d5b985 Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Mon, 18 Mar 2019 21:24:09 +0100 Subject: [PATCH] Code Cleanup and ImplictGrantAuth name fix, fixes #320 --- SpotifyAPI.Web.Auth/AuthUtil.cs | 4 - SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs | 5 +- SpotifyAPI.Web.Auth/CredentialsAuth.cs | 3 +- ...plictGrantAuth.cs => ImplicitGrantAuth.cs} | 23 ++--- SpotifyAPI.Web.Auth/SpotifyAuthServer.cs | 1 - SpotifyAPI.Web.Example/Program.cs | 1 + SpotifyAPI.Web.Tests/SpotifyWebAPITest.cs | 2 +- SpotifyAPI.Web/Models/BasicModel.cs | 2 - SpotifyAPI.Web/Models/Category.cs | 1 - SpotifyAPI.Web/Models/CursorPaging.cs | 1 - SpotifyAPI.Web/Models/FeaturedPlaylists.cs | 1 - SpotifyAPI.Web/Models/FullAlbum.cs | 1 - SpotifyAPI.Web/Models/FullArtist.cs | 1 - SpotifyAPI.Web/Models/FullPlaylist.cs | 5 +- SpotifyAPI.Web/Models/FullTrack.cs | 5 +- SpotifyAPI.Web/Models/GeneralModels.cs | 4 +- SpotifyAPI.Web/Models/Paging.cs | 1 - SpotifyAPI.Web/Models/PlaybackContext.cs | 3 +- SpotifyAPI.Web/Models/PrivateProfile.cs | 1 - SpotifyAPI.Web/Models/PublicProfile.cs | 1 - SpotifyAPI.Web/Models/Recommendations.cs | 3 +- SpotifyAPI.Web/Models/ResponseInfo.cs | 3 +- SpotifyAPI.Web/Models/SimpleAlbum.cs | 1 - SpotifyAPI.Web/Models/SimpleArtist.cs | 1 - SpotifyAPI.Web/Models/SimplePlaylist.cs | 5 +- SpotifyAPI.Web/Models/SimpleTrack.cs | 3 +- SpotifyAPI.Web/Models/Snapshot.cs | 1 - SpotifyAPI.Web/Models/TuneableTrack.cs | 13 +-- SpotifyAPI.Web/ProxyConfig.cs | 12 +-- SpotifyAPI.Web/SpotifyWebAPI.cs | 70 +++++++------- SpotifyAPI.Web/SpotifyWebBuilder.cs | 92 +++++++++---------- SpotifyAPI.Web/SpotifyWebClient.cs | 15 ++- 32 files changed, 118 insertions(+), 167 deletions(-) rename SpotifyAPI.Web.Auth/{ImplictGrantAuth.cs => ImplicitGrantAuth.cs} (72%) diff --git a/SpotifyAPI.Web.Auth/AuthUtil.cs b/SpotifyAPI.Web.Auth/AuthUtil.cs index d6e40e45..2e6025a2 100644 --- a/SpotifyAPI.Web.Auth/AuthUtil.cs +++ b/SpotifyAPI.Web.Auth/AuthUtil.cs @@ -21,10 +21,6 @@ namespace SpotifyAPI.Web.Auth { Process.Start("open", url); } - else - { - // throw - } #else url = url.Replace("&", "^&"); Process.Start(new ProcessStartInfo("cmd", $"/c start {url}")); diff --git a/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs b/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs index 157b8108..b8687c30 100644 --- a/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs +++ b/SpotifyAPI.Web.Auth/AuthorizationCodeAuth.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -48,7 +47,7 @@ namespace SpotifyAPI.Web.Auth public async Task RefreshToken(string refreshToken) { - List> args = new List>() + List> args = new List> { new KeyValuePair("grant_type", "refresh_token"), new KeyValuePair("refresh_token", refreshToken) @@ -65,7 +64,7 @@ namespace SpotifyAPI.Web.Auth } public async Task ExchangeCode(string code) { - List> args = new List>() + List> args = new List> { new KeyValuePair("grant_type", "authorization_code"), new KeyValuePair("code", code), diff --git a/SpotifyAPI.Web.Auth/CredentialsAuth.cs b/SpotifyAPI.Web.Auth/CredentialsAuth.cs index 45b3d44b..1da062bc 100644 --- a/SpotifyAPI.Web.Auth/CredentialsAuth.cs +++ b/SpotifyAPI.Web.Auth/CredentialsAuth.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -25,7 +24,7 @@ namespace SpotifyAPI.Web.Auth { string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret)); - List> args = new List>() + List> args = new List> { new KeyValuePair("grant_type", "client_credentials") }; diff --git a/SpotifyAPI.Web.Auth/ImplictGrantAuth.cs b/SpotifyAPI.Web.Auth/ImplicitGrantAuth.cs similarity index 72% rename from SpotifyAPI.Web.Auth/ImplictGrantAuth.cs rename to SpotifyAPI.Web.Auth/ImplicitGrantAuth.cs index f5bba3c8..c99809f8 100644 --- a/SpotifyAPI.Web.Auth/ImplictGrantAuth.cs +++ b/SpotifyAPI.Web.Auth/ImplicitGrantAuth.cs @@ -1,24 +1,15 @@ -using System; -using System.Net; using System.Threading.Tasks; using SpotifyAPI.Web.Enums; using SpotifyAPI.Web.Models; using Unosquare.Labs.EmbedIO; using Unosquare.Labs.EmbedIO.Constants; using Unosquare.Labs.EmbedIO.Modules; -#if NETSTANDARD2_0 -using System.Net.Http; -#endif -#if NET46 -using System.Net.Http; -using HttpListenerContext = Unosquare.Net.HttpListenerContext; -#endif namespace SpotifyAPI.Web.Auth { - public class ImplictGrantAuth : SpotifyAuthServer + public class ImplicitGrantAuth : SpotifyAuthServer { - public ImplictGrantAuth(string clientId, string redirectUri, string serverUri, Scope scope = Scope.None, string state = "") : + public ImplicitGrantAuth(string clientId, string redirectUri, string serverUri, Scope scope = Scope.None, string state = "") : base("token", "ImplicitGrantAuth", redirectUri, serverUri, scope, state) { ClientId = clientId; @@ -26,17 +17,17 @@ namespace SpotifyAPI.Web.Auth protected override void AdaptWebServer(WebServer webServer) { - webServer.Module().RegisterController(); + webServer.Module().RegisterController(); } } - public class ImplictGrantAuthController : WebApiController + public class ImplicitGrantAuthController : WebApiController { [WebApiHandler(HttpVerbs.Get, "/auth")] public Task GetAuth() { string state = Request.QueryString["state"]; - SpotifyAuthServer auth = ImplictGrantAuth.GetByState(state); + SpotifyAuthServer auth = ImplicitGrantAuth.GetByState(state); if (auth == null) return this.StringResponseAsync( $"Failed - Unable to find auth request with state \"{state}\" - Please retry"); @@ -57,7 +48,7 @@ namespace SpotifyAPI.Web.Auth } else { - token = new Token() + token = new Token { Error = error }; @@ -67,7 +58,7 @@ namespace SpotifyAPI.Web.Auth return this.StringResponseAsync("OK - This window can be closed now"); } - public ImplictGrantAuthController(IHttpContext context) : base(context) + public ImplicitGrantAuthController(IHttpContext context) : base(context) { } } diff --git a/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs b/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs index 74359eff..cc1d827a 100644 --- a/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs +++ b/SpotifyAPI.Web.Auth/SpotifyAuthServer.cs @@ -6,7 +6,6 @@ using System.Text; using System.Threading; using SpotifyAPI.Web.Enums; using Unosquare.Labs.EmbedIO; -using Unosquare.Labs.EmbedIO.Constants; using Unosquare.Labs.EmbedIO.Modules; namespace SpotifyAPI.Web.Auth diff --git a/SpotifyAPI.Web.Example/Program.cs b/SpotifyAPI.Web.Example/Program.cs index cefa2a80..455e9ffe 100644 --- a/SpotifyAPI.Web.Example/Program.cs +++ b/SpotifyAPI.Web.Example/Program.cs @@ -10,6 +10,7 @@ namespace SpotifyAPI.Web.Example private static string _clientId = ""; //""; private static string _secretId = ""; //""; + // ReSharper disable once UnusedParameter.Local static void Main(string[] args) { _clientId = string.IsNullOrEmpty(_clientId) diff --git a/SpotifyAPI.Web.Tests/SpotifyWebAPITest.cs b/SpotifyAPI.Web.Tests/SpotifyWebAPITest.cs index 8d4181af..d00e5e3a 100644 --- a/SpotifyAPI.Web.Tests/SpotifyWebAPITest.cs +++ b/SpotifyAPI.Web.Tests/SpotifyWebAPITest.cs @@ -21,7 +21,7 @@ namespace SpotifyAPI.Web.Tests public void SetUp() { _mock = new Mock(); - _spotify = new SpotifyWebAPI() + _spotify = new SpotifyWebAPI { WebClient = _mock.Object, UseAuth = false diff --git a/SpotifyAPI.Web/Models/BasicModel.cs b/SpotifyAPI.Web/Models/BasicModel.cs index 04727628..d888dc21 100644 --- a/SpotifyAPI.Web/Models/BasicModel.cs +++ b/SpotifyAPI.Web/Models/BasicModel.cs @@ -1,6 +1,4 @@ using Newtonsoft.Json; -using SpotifyAPI.Web.Enums; -using System; using System.Net; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/Category.cs b/SpotifyAPI.Web/Models/Category.cs index f4e280a2..03be7a0b 100644 --- a/SpotifyAPI.Web/Models/Category.cs +++ b/SpotifyAPI.Web/Models/Category.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/CursorPaging.cs b/SpotifyAPI.Web/Models/CursorPaging.cs index cfb8be42..3496212c 100644 --- a/SpotifyAPI.Web/Models/CursorPaging.cs +++ b/SpotifyAPI.Web/Models/CursorPaging.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/FeaturedPlaylists.cs b/SpotifyAPI.Web/Models/FeaturedPlaylists.cs index 9fa45203..ab1dc09d 100644 --- a/SpotifyAPI.Web/Models/FeaturedPlaylists.cs +++ b/SpotifyAPI.Web/Models/FeaturedPlaylists.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; namespace SpotifyAPI.Web.Models { diff --git a/SpotifyAPI.Web/Models/FullAlbum.cs b/SpotifyAPI.Web/Models/FullAlbum.cs index 47e0458c..b22e4f2d 100644 --- a/SpotifyAPI.Web/Models/FullAlbum.cs +++ b/SpotifyAPI.Web/Models/FullAlbum.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/FullArtist.cs b/SpotifyAPI.Web/Models/FullArtist.cs index bf43263a..1b38a747 100644 --- a/SpotifyAPI.Web/Models/FullArtist.cs +++ b/SpotifyAPI.Web/Models/FullArtist.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/FullPlaylist.cs b/SpotifyAPI.Web/Models/FullPlaylist.cs index 098664ff..aec32d0b 100644 --- a/SpotifyAPI.Web/Models/FullPlaylist.cs +++ b/SpotifyAPI.Web/Models/FullPlaylist.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models @@ -7,7 +6,7 @@ namespace SpotifyAPI.Web.Models public class FullPlaylist : BasicModel { [JsonProperty("collaborative")] - public Boolean Collaborative { get; set; } + public bool Collaborative { get; set; } [JsonProperty("description")] public string Description { get; set; } @@ -34,7 +33,7 @@ namespace SpotifyAPI.Web.Models public PublicProfile Owner { get; set; } [JsonProperty("public")] - public Boolean Public { get; set; } + public bool Public { get; set; } [JsonProperty("snapshot_id")] public string SnapshotId { get; set; } diff --git a/SpotifyAPI.Web/Models/FullTrack.cs b/SpotifyAPI.Web/Models/FullTrack.cs index a08a002e..bab52feb 100644 --- a/SpotifyAPI.Web/Models/FullTrack.cs +++ b/SpotifyAPI.Web/Models/FullTrack.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models @@ -22,7 +21,7 @@ namespace SpotifyAPI.Web.Models public int DurationMs { get; set; } [JsonProperty("explicit")] - public Boolean Explicit { get; set; } + public bool Explicit { get; set; } [JsonProperty("external_ids")] public Dictionary ExternalIds { get; set; } @@ -61,7 +60,7 @@ namespace SpotifyAPI.Web.Models /// Only filled when the "market"-parameter was supplied! /// [JsonProperty("is_playable")] - public Boolean? IsPlayable { get; set; } + public bool? IsPlayable { get; set; } /// /// Only filled when the "market"-parameter was supplied! diff --git a/SpotifyAPI.Web/Models/GeneralModels.cs b/SpotifyAPI.Web/Models/GeneralModels.cs index 993323a3..8dffd92b 100644 --- a/SpotifyAPI.Web/Models/GeneralModels.cs +++ b/SpotifyAPI.Web/Models/GeneralModels.cs @@ -60,7 +60,7 @@ namespace SpotifyAPI.Web.Models public FullTrack Track { get; set; } [JsonProperty("is_local")] - public Boolean IsLocal { get; set; } + public bool IsLocal { get; set; } } public class DeleteTrackUri @@ -84,7 +84,7 @@ namespace SpotifyAPI.Web.Models public bool ShouldSerializePositions() { - return (Positions.Count > 0); + return Positions.Count > 0; } } diff --git a/SpotifyAPI.Web/Models/Paging.cs b/SpotifyAPI.Web/Models/Paging.cs index 9bb13b37..03e5305e 100644 --- a/SpotifyAPI.Web/Models/Paging.cs +++ b/SpotifyAPI.Web/Models/Paging.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/PlaybackContext.cs b/SpotifyAPI.Web/Models/PlaybackContext.cs index dabec300..37d6d4a7 100644 --- a/SpotifyAPI.Web/Models/PlaybackContext.cs +++ b/SpotifyAPI.Web/Models/PlaybackContext.cs @@ -1,5 +1,4 @@ -using System; -using Newtonsoft.Json; +using Newtonsoft.Json; using Newtonsoft.Json.Converters; using SpotifyAPI.Web.Enums; diff --git a/SpotifyAPI.Web/Models/PrivateProfile.cs b/SpotifyAPI.Web/Models/PrivateProfile.cs index a41ecc24..243d6b94 100644 --- a/SpotifyAPI.Web/Models/PrivateProfile.cs +++ b/SpotifyAPI.Web/Models/PrivateProfile.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/PublicProfile.cs b/SpotifyAPI.Web/Models/PublicProfile.cs index 1e71d675..a28b8ea4 100644 --- a/SpotifyAPI.Web/Models/PublicProfile.cs +++ b/SpotifyAPI.Web/Models/PublicProfile.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/Recommendations.cs b/SpotifyAPI.Web/Models/Recommendations.cs index 229be912..921522ec 100644 --- a/SpotifyAPI.Web/Models/Recommendations.cs +++ b/SpotifyAPI.Web/Models/Recommendations.cs @@ -1,5 +1,4 @@ -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using Newtonsoft.Json; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/ResponseInfo.cs b/SpotifyAPI.Web/Models/ResponseInfo.cs index 7a794387..1a8a12c2 100644 --- a/SpotifyAPI.Web/Models/ResponseInfo.cs +++ b/SpotifyAPI.Web/Models/ResponseInfo.cs @@ -1,5 +1,4 @@ -using SpotifyAPI.Web.Enums; -using System.Net; +using System.Net; namespace SpotifyAPI.Web.Models { diff --git a/SpotifyAPI.Web/Models/SimpleAlbum.cs b/SpotifyAPI.Web/Models/SimpleAlbum.cs index 6636a80d..2e2aa0fe 100644 --- a/SpotifyAPI.Web/Models/SimpleAlbum.cs +++ b/SpotifyAPI.Web/Models/SimpleAlbum.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/SimpleArtist.cs b/SpotifyAPI.Web/Models/SimpleArtist.cs index 0604c751..c1294eea 100644 --- a/SpotifyAPI.Web/Models/SimpleArtist.cs +++ b/SpotifyAPI.Web/Models/SimpleArtist.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models diff --git a/SpotifyAPI.Web/Models/SimplePlaylist.cs b/SpotifyAPI.Web/Models/SimplePlaylist.cs index 20952da5..37b37608 100644 --- a/SpotifyAPI.Web/Models/SimplePlaylist.cs +++ b/SpotifyAPI.Web/Models/SimplePlaylist.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models @@ -7,7 +6,7 @@ namespace SpotifyAPI.Web.Models public class SimplePlaylist : BasicModel { [JsonProperty("collaborative")] - public Boolean Collaborative { get; set; } + public bool Collaborative { get; set; } [JsonProperty("external_urls")] public Dictionary ExternalUrls { get; set; } @@ -28,7 +27,7 @@ namespace SpotifyAPI.Web.Models public PublicProfile Owner { get; set; } [JsonProperty("public")] - public Boolean Public { get; set; } + public bool Public { get; set; } [JsonProperty("snapshot_id")] public string SnapshotId { get; set; } diff --git a/SpotifyAPI.Web/Models/SimpleTrack.cs b/SpotifyAPI.Web/Models/SimpleTrack.cs index 59b92103..85702a3e 100644 --- a/SpotifyAPI.Web/Models/SimpleTrack.cs +++ b/SpotifyAPI.Web/Models/SimpleTrack.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.Collections.Generic; namespace SpotifyAPI.Web.Models @@ -19,7 +18,7 @@ namespace SpotifyAPI.Web.Models public int DurationMs { get; set; } [JsonProperty("explicit")] - public Boolean Explicit { get; set; } + public bool Explicit { get; set; } [JsonProperty("external_urls")] public Dictionary ExternUrls { get; set; } diff --git a/SpotifyAPI.Web/Models/Snapshot.cs b/SpotifyAPI.Web/Models/Snapshot.cs index 8e2f0db3..4a7e3e7c 100644 --- a/SpotifyAPI.Web/Models/Snapshot.cs +++ b/SpotifyAPI.Web/Models/Snapshot.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; namespace SpotifyAPI.Web.Models { diff --git a/SpotifyAPI.Web/Models/TuneableTrack.cs b/SpotifyAPI.Web/Models/TuneableTrack.cs index 9cdbe180..9f3943b3 100644 --- a/SpotifyAPI.Web/Models/TuneableTrack.cs +++ b/SpotifyAPI.Web/Models/TuneableTrack.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.Collections.Generic; using System.Globalization; -using System.Linq; using System.Reflection; -using Newtonsoft.Json; namespace SpotifyAPI.Web.Models { @@ -61,10 +57,9 @@ namespace SpotifyAPI.Web.Models string name = info.GetCustomAttribute()?.Text; if(name == null || value == null) continue; - if (value is float) - urlParams.Add($"{prefix}_{name}={((float)value).ToString(CultureInfo.InvariantCulture)}"); - else - urlParams.Add($"{prefix}_{name}={value}"); + urlParams.Add(value is float valueAsFloat + ? $"{prefix}_{name}={valueAsFloat.ToString(CultureInfo.InvariantCulture)}" + : $"{prefix}_{name}={value}"); } if (urlParams.Count > 0) return "&" + string.Join("&", urlParams); diff --git a/SpotifyAPI.Web/ProxyConfig.cs b/SpotifyAPI.Web/ProxyConfig.cs index 779666b2..309f577c 100644 --- a/SpotifyAPI.Web/ProxyConfig.cs +++ b/SpotifyAPI.Web/ProxyConfig.cs @@ -1,7 +1,7 @@ using System; using System.Net; -namespace SpotifyAPI +namespace SpotifyAPI.Web { public class ProxyConfig { @@ -65,11 +65,11 @@ namespace SpotifyAPI BypassProxyOnLocal = BypassProxyOnLocal }; - if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password)) - { - proxy.UseDefaultCredentials = false; - proxy.Credentials = new NetworkCredential(Username, Password); - } + if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) + return proxy; + + proxy.UseDefaultCredentials = false; + proxy.Credentials = new NetworkCredential(Username, Password); return proxy; } diff --git a/SpotifyAPI.Web/SpotifyWebAPI.cs b/SpotifyAPI.Web/SpotifyWebAPI.cs index 7eba5cfe..9dd2cff3 100644 --- a/SpotifyAPI.Web/SpotifyWebAPI.cs +++ b/SpotifyAPI.Web/SpotifyWebAPI.cs @@ -647,8 +647,8 @@ namespace SpotifyAPI.Web { {"ids", new JArray(ids)} }; - return (await UploadDataAsync(_builder.Follow(followType), - ob.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.Follow(followType), + ob.ToString(Formatting.None), "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -704,7 +704,7 @@ namespace SpotifyAPI.Web { {"ids", new JArray(ids)} }; - return (await UploadDataAsync(_builder.Unfollow(followType), ob.ToString(Formatting.None), "DELETE").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.Unfollow(followType), ob.ToString(Formatting.None), "DELETE").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -743,7 +743,7 @@ namespace SpotifyAPI.Web if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowing"); - var url = _builder.IsFollowing(followType, ids); + string url = _builder.IsFollowing(followType, ids); return DownloadList(url); } @@ -761,7 +761,7 @@ namespace SpotifyAPI.Web if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowing"); - var url = _builder.IsFollowing(followType, ids); + string url = _builder.IsFollowing(followType, ids); return DownloadListAsync(url); } @@ -872,7 +872,7 @@ namespace SpotifyAPI.Web if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowingPlaylist"); - var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids); + string url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids); return DownloadList(url); } @@ -889,7 +889,7 @@ namespace SpotifyAPI.Web if (!UseAuth) throw new InvalidOperationException("Auth is required for IsFollowingPlaylist"); - var url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids); + string url = _builder.IsFollowingPlaylist(ownerId, playlistId, ids); return DownloadListAsync(url); } @@ -944,7 +944,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").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.SaveTracks(), array.ToString(Formatting.None), "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1020,7 +1020,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").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.RemoveSavedTracks(), array.ToString(Formatting.None), "DELETE").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1034,7 +1034,7 @@ namespace SpotifyAPI.Web if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedTracks"); - var url = _builder.CheckSavedTracks(ids); + string url = _builder.CheckSavedTracks(ids); return DownloadList(url); } @@ -1048,7 +1048,7 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedTracks"); - var url = _builder.CheckSavedTracks(ids); + string url = _builder.CheckSavedTracks(ids); return DownloadListAsync(url); } @@ -1073,7 +1073,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").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.SaveAlbums(), array.ToString(Formatting.None), "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1149,7 +1149,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").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.RemoveSavedAlbums(), array.ToString(Formatting.None), "DELETE").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1163,7 +1163,7 @@ namespace SpotifyAPI.Web if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedTracks"); - var url = _builder.CheckSavedAlbums(ids); + string url = _builder.CheckSavedAlbums(ids); return DownloadList(url); } @@ -1177,7 +1177,7 @@ namespace SpotifyAPI.Web { if (!UseAuth) throw new InvalidOperationException("Auth is required for CheckSavedAlbumsAsync"); - var url = _builder.CheckSavedAlbums(ids); + string url = _builder.CheckSavedAlbums(ids); return DownloadListAsync(url); } @@ -1601,7 +1601,7 @@ namespace SpotifyAPI.Web body.Add("collaborative", newCollaborative); if (newDescription != null) body.Add("description", newDescription); - return (await UploadDataAsync(_builder.UpdatePlaylist(userId, playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.UpdatePlaylist(userId, playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1625,7 +1625,7 @@ namespace SpotifyAPI.Web body.Add("collaborative", newCollaborative); if (newDescription != null) body.Add("description", newDescription); - return (await UploadDataAsync(_builder.UpdatePlaylist(playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.UpdatePlaylist(playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1651,7 +1651,7 @@ namespace SpotifyAPI.Web /// AUTH NEEDED public async Task UploadPlaylistImageAsync(string userId, string playlistId, string base64EncodedJpgImage) { - return (await UploadDataAsync(_builder.UploadPlaylistImage(userId, playlistId), base64EncodedJpgImage, "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.UploadPlaylistImage(userId, playlistId), base64EncodedJpgImage, "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1675,8 +1675,8 @@ namespace SpotifyAPI.Web /// AUTH NEEDED public async Task UploadPlaylistImageAsync(string playlistId, string base64EncodedJpgImage) { - return (await UploadDataAsync(_builder.UploadPlaylistImage(playlistId), - base64EncodedJpgImage, "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.UploadPlaylistImage(playlistId), + base64EncodedJpgImage, "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1731,7 +1731,7 @@ namespace SpotifyAPI.Web { {"uris", new JArray(uris.Take(100))} }; - return await (UploadDataAsync(_builder.ReplacePlaylistTracks(userId, playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.ReplacePlaylistTracks(userId, playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1748,7 +1748,7 @@ namespace SpotifyAPI.Web { {"uris", new JArray(uris.Take(100))} }; - return await (UploadDataAsync(_builder.ReplacePlaylistTracks(playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.ReplacePlaylistTracks(playlistId), body.ToString(Formatting.None), "PUT").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1809,7 +1809,7 @@ namespace SpotifyAPI.Web { {"tracks", JArray.FromObject(uris.Take(100))} }; - return await (UploadDataAsync(_builder.RemovePlaylistTracks(userId, playlistId, uris), body.ToString(Formatting.None), "DELETE").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.RemovePlaylistTracks(userId, playlistId, uris), body.ToString(Formatting.None), "DELETE").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1828,7 +1828,7 @@ namespace SpotifyAPI.Web { {"tracks", JArray.FromObject(uris.Take(100))} }; - return await (UploadDataAsync(_builder.RemovePlaylistTracks(playlistId, uris), body.ToString(Formatting.None), "DELETE").ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.RemovePlaylistTracks(playlistId, uris), body.ToString(Formatting.None), "DELETE").ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1935,7 +1935,7 @@ namespace SpotifyAPI.Web { {"uris", JArray.FromObject(uris.Take(100))} }; - return await (UploadDataAsync(_builder.AddPlaylistTracks(userId, playlistId, uris, position), body.ToString(Formatting.None)).ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.AddPlaylistTracks(userId, playlistId, uris, position), body.ToString(Formatting.None)).ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -1952,7 +1952,7 @@ namespace SpotifyAPI.Web { {"uris", JArray.FromObject(uris.Take(100))} }; - return await (UploadDataAsync(_builder.AddPlaylistTracks(playlistId, uris, position), body.ToString(Formatting.None)).ConfigureAwait(false)) ?? new ErrorResponse(); + return await UploadDataAsync(_builder.AddPlaylistTracks(playlistId, uris, position), body.ToString(Formatting.None)).ConfigureAwait(false) ?? new ErrorResponse(); } /// @@ -2370,7 +2370,7 @@ namespace SpotifyAPI.Web /// public ErrorResponse TransferPlayback(List deviceIds, bool play = false) { - JObject ob = new JObject() + JObject ob = new JObject { { "play", play }, { "device_ids", new JArray(deviceIds) } @@ -2390,7 +2390,7 @@ namespace SpotifyAPI.Web /// public Task TransferPlaybackAsync(List deviceIds, bool play = false) { - JObject ob = new JObject() + JObject ob = new JObject { { "play", play }, { "device_ids", new JArray(deviceIds) } @@ -2405,8 +2405,8 @@ namespace SpotifyAPI.Web /// Spotify URI of the context to play. /// A JSON array of the Spotify track URIs to play. /// Indicates from where in the context playback should start. - /// The starting time to seek the track to /// Only available when context_uri corresponds to an album or playlist object, or when the uris parameter is used. + /// The starting time to seek the track to /// public ErrorResponse ResumePlayback(string deviceId = "", string contextUri = "", List uris = null, int? offset = null, int positionMs = 0) @@ -2430,8 +2430,8 @@ namespace SpotifyAPI.Web /// Spotify URI of the context to play. /// A JSON array of the Spotify track URIs to play. /// Indicates from where in the context playback should start. - /// The starting time to seek the track to /// Only available when context_uri corresponds to an album or playlist object, or when the uris parameter is used. + /// The starting time to seek the track to /// public Task ResumePlaybackAsync(string deviceId = "", string contextUri = "", List uris = null, int? offset = null, int positionMs = 0) @@ -2455,8 +2455,8 @@ namespace SpotifyAPI.Web /// Spotify URI of the context to play. /// A JSON array of the Spotify track URIs to play. /// Indicates from where in the context playback should start. - /// The starting time to seek the track to /// Only available when context_uri corresponds to an album or playlist object, or when the uris parameter is used. + /// The starting time to seek the track to /// public ErrorResponse ResumePlayback(string deviceId = "", string contextUri = "", List uris = null, string offset = "", int positionMs = 0) @@ -2480,8 +2480,8 @@ namespace SpotifyAPI.Web /// Spotify URI of the context to play. /// A JSON array of the Spotify track URIs to play. /// Indicates from where in the context playback should start. - /// The starting time to seek the track to /// Only available when context_uri corresponds to an album or playlist object, or when the uris parameter is used. + /// The starting time to seek the track to /// public Task ResumePlaybackAsync(string deviceId = "", string contextUri = "", List uris = null, string offset = "", int positionMs = 0) @@ -2862,7 +2862,7 @@ namespace SpotifyAPI.Web { return -1; } - if (!int.TryParse(info.Headers.Get("Retry-After"), out var secondsToWait)) + if (!int.TryParse(info.Headers.Get("Retry-After"), out int secondsToWait)) { return -1; } @@ -2880,7 +2880,7 @@ namespace SpotifyAPI.Web if (response != null) { int msToWait = RetryAfter; - var secondsToWait = GetTooManyRequests(response.Item1); + int secondsToWait = GetTooManyRequests(response.Item1); if (secondsToWait > 0) { msToWait = secondsToWait * 1000; @@ -2900,7 +2900,7 @@ namespace SpotifyAPI.Web } while (UseAutoRetry && triesLeft > 0 && (GetTooManyRequests(response.Item1) != -1 - || (lastError != null && RetryErrorCodes.Contains(lastError.Status)))); + || lastError != null && RetryErrorCodes.Contains(lastError.Status))); return response.Item2; diff --git a/SpotifyAPI.Web/SpotifyWebBuilder.cs b/SpotifyAPI.Web/SpotifyWebBuilder.cs index 92877243..47e913a5 100644 --- a/SpotifyAPI.Web/SpotifyWebBuilder.cs +++ b/SpotifyAPI.Web/SpotifyWebBuilder.cs @@ -70,9 +70,7 @@ namespace SpotifyAPI.Web /// public string GetAlbum(string id, string market = "") { - if (string.IsNullOrEmpty(market)) - return $"{APIBase}/albums/{id}"; - return $"{APIBase}/albums/{id}?market={market}"; + return string.IsNullOrEmpty(market) ? $"{APIBase}/albums/{id}" : $"{APIBase}/albums/{id}?market={market}"; } /// @@ -83,9 +81,9 @@ namespace SpotifyAPI.Web /// public string GetSeveralAlbums(List ids, string market = "") { - if (string.IsNullOrEmpty(market)) - return $"{APIBase}/albums?ids={string.Join(",", ids.Take(20))}"; - return $"{APIBase}/albums?market={market}&ids={string.Join(",", ids.Take(20))}"; + return string.IsNullOrEmpty(market) + ? $"{APIBase}/albums?ids={string.Join(",", ids.Take(20))}" + : $"{APIBase}/albums?market={market}&ids={string.Join(",", ids.Take(20))}"; } #endregion Albums @@ -734,7 +732,7 @@ namespace SpotifyAPI.Web /// /// /// AUTH NEEDED - public string CreatePlaylist(string userId, string playlistName, Boolean isPublic = true) + public string CreatePlaylist(string userId, string playlistName, bool isPublic = true) { return $"{APIBase}/users/{userId}/playlists"; } @@ -829,9 +827,9 @@ namespace SpotifyAPI.Web /// AUTH NEEDED public string AddPlaylistTracks(string userId, string playlistId, List uris, int? position = null) { - if (position == null) - return $"{APIBase}/users/{userId}/playlists/{playlistId}/tracks"; - return $"{APIBase}/users/{userId}/playlists/{playlistId}/tracks?position={position}"; + return position == null + ? $"{APIBase}/users/{userId}/playlists/{playlistId}/tracks" + : $"{APIBase}/users/{userId}/playlists/{playlistId}/tracks?position={position}"; } /// @@ -844,9 +842,9 @@ namespace SpotifyAPI.Web /// AUTH NEEDED public string AddPlaylistTracks(string playlistId, List uris, int? position = null) { - if (position == null) - return $"{APIBase}/playlists/{playlistId}/tracks"; - return $"{APIBase}/playlists/{playlistId}/tracks?position={position}"; + return position == null + ? $"{APIBase}/playlists/{playlistId}/tracks" + : $"{APIBase}/playlists/{playlistId}/tracks?position={position}"; } /// @@ -931,9 +929,9 @@ namespace SpotifyAPI.Web /// public string GetSeveralTracks(List ids, string market = "") { - if (string.IsNullOrEmpty(market)) - return $"{APIBase}/tracks?ids={string.Join(",", ids.Take(50))}"; - return $"{APIBase}/tracks?market={market}&ids={string.Join(",", ids.Take(50))}"; + return string.IsNullOrEmpty(market) + ? $"{APIBase}/tracks?ids={string.Join(",", ids.Take(50))}" + : $"{APIBase}/tracks?market={market}&ids={string.Join(",", ids.Take(50))}"; } /// @@ -944,9 +942,7 @@ namespace SpotifyAPI.Web /// public string GetTrack(string id, string market = "") { - if (string.IsNullOrEmpty(market)) - return $"{APIBase}/tracks/{id}"; - return $"{APIBase}/tracks/{id}?market={market}"; + return string.IsNullOrEmpty(market) ? $"{APIBase}/tracks/{id}" : $"{APIBase}/tracks/{id}?market={market}"; } /// @@ -1002,9 +998,7 @@ namespace SpotifyAPI.Web /// public string GetPlayback(string market = "") { - if (string.IsNullOrEmpty(market)) - return $"{APIBase}/me/player"; - return $"{APIBase}/me/player?market={market}"; + return string.IsNullOrEmpty(market) ? $"{APIBase}/me/player" : $"{APIBase}/me/player?market={market}"; } /// @@ -1014,9 +1008,9 @@ namespace SpotifyAPI.Web /// public string GetPlayingTrack(string market = "") { - if (string.IsNullOrEmpty(market)) - return $"{APIBase}/me/player/currently-playing"; - return $"{APIBase}/me/player/currently-playing?market={market}"; + return string.IsNullOrEmpty(market) + ? $"{APIBase}/me/player/currently-playing" + : $"{APIBase}/me/player/currently-playing?market={market}"; } /// @@ -1035,9 +1029,9 @@ namespace SpotifyAPI.Web /// public string ResumePlayback(string deviceId = "") { - if(string.IsNullOrEmpty(deviceId)) - return $"{APIBase}/me/player/play"; - return $"{APIBase}/me/player/play?device_id={deviceId}"; + return string.IsNullOrEmpty(deviceId) + ? $"{APIBase}/me/player/play" + : $"{APIBase}/me/player/play?device_id={deviceId}"; } /// @@ -1047,9 +1041,9 @@ namespace SpotifyAPI.Web /// public string PausePlayback(string deviceId = "") { - if (string.IsNullOrEmpty(deviceId)) - return $"{APIBase}/me/player/pause"; - return $"{APIBase}/me/player/pause?device_id={deviceId}"; + return string.IsNullOrEmpty(deviceId) + ? $"{APIBase}/me/player/pause" + : $"{APIBase}/me/player/pause?device_id={deviceId}"; } /// @@ -1059,9 +1053,9 @@ namespace SpotifyAPI.Web /// public string SkipPlaybackToNext(string deviceId = "") { - if (string.IsNullOrEmpty(deviceId)) - return $"{APIBase}/me/player/next"; - return $"{APIBase}/me/player/next?device_id={deviceId}"; + return string.IsNullOrEmpty(deviceId) + ? $"{APIBase}/me/player/next" + : $"{APIBase}/me/player/next?device_id={deviceId}"; } /// @@ -1073,9 +1067,9 @@ namespace SpotifyAPI.Web /// public string SkipPlaybackToPrevious(string deviceId = "") { - if (string.IsNullOrEmpty(deviceId)) - return $"{APIBase}/me/player/previous"; - return $"{APIBase}/me/player/previous?device_id={deviceId}"; + return string.IsNullOrEmpty(deviceId) + ? $"{APIBase}/me/player/previous" + : $"{APIBase}/me/player/previous?device_id={deviceId}"; } /// @@ -1087,9 +1081,9 @@ namespace SpotifyAPI.Web /// public string SeekPlayback(int positionMs, string deviceId = "") { - if (string.IsNullOrEmpty(deviceId)) - return $"{APIBase}/me/player/seek?position_ms={positionMs}"; - return $"{APIBase}/me/player/seek?position_ms={positionMs}&device_id={deviceId}"; + return string.IsNullOrEmpty(deviceId) + ? $"{APIBase}/me/player/seek?position_ms={positionMs}" + : $"{APIBase}/me/player/seek?position_ms={positionMs}&device_id={deviceId}"; } /// @@ -1100,9 +1094,9 @@ namespace SpotifyAPI.Web /// public string SetRepeatMode(RepeatState repeatState, string deviceId = "") { - if (string.IsNullOrEmpty(deviceId)) - return $"{APIBase}/me/player/repeat?state={repeatState.GetStringAttribute()}"; - return $"{APIBase}/me/player/repeat?state={repeatState.GetStringAttribute()}&device_id={deviceId}"; + return string.IsNullOrEmpty(deviceId) + ? $"{APIBase}/me/player/repeat?state={repeatState.GetStringAttribute()}" + : $"{APIBase}/me/player/repeat?state={repeatState.GetStringAttribute()}&device_id={deviceId}"; } /// @@ -1113,9 +1107,9 @@ namespace SpotifyAPI.Web /// public string SetVolume(int volumePercent, string deviceId = "") { - if (string.IsNullOrEmpty(deviceId)) - return $"{APIBase}/me/player/volume?volume_percent={volumePercent}"; - return $"{APIBase}/me/player/volume?volume_percent={volumePercent}&device_id={deviceId}"; + return string.IsNullOrEmpty(deviceId) + ? $"{APIBase}/me/player/volume?volume_percent={volumePercent}" + : $"{APIBase}/me/player/volume?volume_percent={volumePercent}&device_id={deviceId}"; } /// @@ -1126,9 +1120,9 @@ namespace SpotifyAPI.Web /// public string SetShuffle(bool shuffle, string deviceId = "") { - if (string.IsNullOrEmpty(deviceId)) - return $"{APIBase}/me/player/shuffle?state={shuffle}"; - return $"{APIBase}/me/player/shuffle?state={shuffle}&device_id={deviceId}"; + return string.IsNullOrEmpty(deviceId) + ? $"{APIBase}/me/player/shuffle?state={shuffle}" + : $"{APIBase}/me/player/shuffle?state={shuffle}&device_id={deviceId}"; } #endregion } diff --git a/SpotifyAPI.Web/SpotifyWebClient.cs b/SpotifyAPI.Web/SpotifyWebClient.cs index 65e829ea..54b2ff4d 100644 --- a/SpotifyAPI.Web/SpotifyWebClient.cs +++ b/SpotifyAPI.Web/SpotifyWebClient.cs @@ -7,7 +7,6 @@ using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using SpotifyAPI.Web.Models; -using SpotifyAPI.Web.Enums; namespace SpotifyAPI.Web { @@ -211,14 +210,12 @@ namespace SpotifyAPI.Web UseProxy = false }; - if (!string.IsNullOrWhiteSpace(proxyConfig?.Host)) - { - WebProxy proxy = proxyConfig.CreateWebProxy(); - clientHandler.UseProxy = true; - clientHandler.Proxy = proxy; - clientHandler.UseDefaultCredentials = proxy.UseDefaultCredentials; - clientHandler.PreAuthenticate = proxy.UseDefaultCredentials; - } + if (string.IsNullOrWhiteSpace(proxyConfig?.Host)) return clientHandler; + WebProxy proxy = proxyConfig.CreateWebProxy(); + clientHandler.UseProxy = true; + clientHandler.Proxy = proxy; + clientHandler.UseDefaultCredentials = proxy.UseDefaultCredentials; + clientHandler.PreAuthenticate = proxy.UseDefaultCredentials; return clientHandler; }