From 10c8944e05ab23fcd740da4de3539c46c2c26cd2 Mon Sep 17 00:00:00 2001 From: mrnikbobjeff Date: Sat, 17 Oct 2015 00:31:01 +0200 Subject: [PATCH] Replaced all occurances of checking whether a string is empty via str == "" with String.IsNullOrEmpty(str) sealed StringAttribute because it is only contains text as attribute sealed SpotifyWebAPI class as it implements IDisposable Changed Exception catching where the Exception is not used to ignore the Exception object Removed Console.WriteLine("Exception: " + e.Message) int HttpProcessor.Process as it is a library function which might not be used in a console application --- SpotifyAPI/Local/Models/Track.cs | 4 +-- SpotifyAPI/Local/RemoteHandler.cs | 14 +++++----- SpotifyAPI/Local/SpotifyLocalAPI.cs | 2 +- SpotifyAPI/SpotifyAPI.csproj | 1 + SpotifyAPI/Web/SimpleHttpServer.cs | 21 ++++++-------- SpotifyAPI/Web/SpotifyWebAPI.cs | 43 +++++++++++++++-------------- SpotifyAPI/Web/Util.cs | 2 +- 7 files changed, 43 insertions(+), 44 deletions(-) diff --git a/SpotifyAPI/Local/Models/Track.cs b/SpotifyAPI/Local/Models/Track.cs index de2c4ecb..6b68cd1e 100644 --- a/SpotifyAPI/Local/Models/Track.cs +++ b/SpotifyAPI/Local/Models/Track.cs @@ -125,7 +125,7 @@ namespace SpotifyAPI.Local.Models { wc.Proxy = null; String url = GetAlbumArtUrl(size); - if (url == "") + if (String.IsNullOrEmpty(url)) return null; var data = wc.DownloadData(url); using (MemoryStream ms = new MemoryStream(data)) @@ -146,7 +146,7 @@ namespace SpotifyAPI.Local.Models { wc.Proxy = null; String url = GetAlbumArtUrl(size); - if (url == "") + if (String.IsNullOrEmpty(url)) return null; return wc.DownloadData(url); } diff --git a/SpotifyAPI/Local/RemoteHandler.cs b/SpotifyAPI/Local/RemoteHandler.cs index b01cbd4a..42a8645c 100644 --- a/SpotifyAPI/Local/RemoteHandler.cs +++ b/SpotifyAPI/Local/RemoteHandler.cs @@ -19,7 +19,7 @@ namespace SpotifyAPI.Local { OauthKey = GetOAuthKey(); CfidKey = GetCfid(); - return CfidKey != ""; + return !String.IsNullOrEmpty(CfidKey); } internal async void SendPauseRequest() @@ -46,7 +46,7 @@ namespace SpotifyAPI.Local internal StatusResponse GetNewStatus() { String response = Query("remote/status.json", true, true, -1); - if (response == "") + if (String.IsNullOrEmpty(response)) { return null; } @@ -77,7 +77,7 @@ namespace SpotifyAPI.Local if (cfidList == null) return ""; if (cfidList.Count != 1) - throw new Exception("CFID couldn't be loaded"); + throw new Exception("CFID could not be loaded"); return cfidList[0].Error == null ? cfidList[0].Token : ""; } @@ -114,9 +114,9 @@ namespace SpotifyAPI.Local response = "[ " + wc.DownloadString(address) + " ]"; } } - catch (Exception) + catch { - return ""; + return String.Empty; } return response; @@ -155,9 +155,9 @@ namespace SpotifyAPI.Local response = "[ " + await wc.DownloadStringTaskAsync(new Uri(address)) + " ]"; } } - catch (Exception) + catch { - return ""; + return String.Empty; } return response; diff --git a/SpotifyAPI/Local/SpotifyLocalAPI.cs b/SpotifyAPI/Local/SpotifyLocalAPI.cs index 40c8bffa..f2e5557f 100644 --- a/SpotifyAPI/Local/SpotifyLocalAPI.cs +++ b/SpotifyAPI/Local/SpotifyLocalAPI.cs @@ -12,7 +12,7 @@ namespace SpotifyAPI.Local { [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); - [DllImport("nircmd.dll")] + [DllImport("nircmd.dll", CharSet = CharSet.Auto)] private static extern bool DoNirCmd(String nirCmdStr); private bool _listenForEvents; diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj index 76cd3221..4ef06276 100644 --- a/SpotifyAPI/SpotifyAPI.csproj +++ b/SpotifyAPI/SpotifyAPI.csproj @@ -24,6 +24,7 @@ 4 bin\Debug\SpotifyAPI.XML 1591 + ..\..\..\My Data\Dokumente\Visual Studio 2015\Projects\BackupAudioRecorder\AudioRecordTest\MAXRULES.ruleset pdbonly diff --git a/SpotifyAPI/Web/SimpleHttpServer.cs b/SpotifyAPI/Web/SimpleHttpServer.cs index eac076c3..b67981c4 100644 --- a/SpotifyAPI/Web/SimpleHttpServer.cs +++ b/SpotifyAPI/Web/SimpleHttpServer.cs @@ -79,15 +79,13 @@ namespace SpotifyAPI.Web HandlePostRequest(); } } - catch (Exception e) + catch { - Console.WriteLine("Exception: " + e); WriteFailure(); } OutputStream.Flush(); - // bs.Flush(); // flush any remaining output _inputStream = null; - OutputStream = null; // bs = null; + OutputStream = null; _socket.Close(); } @@ -97,7 +95,7 @@ namespace SpotifyAPI.Web string[] tokens = request.Split(' '); if (tokens.Length < 2) { - throw new Exception("invalid http request line"); + throw new Exception("Invalid HTTP request line"); } HttpMethod = tokens[0].ToUpper(); HttpUrl = tokens[1]; @@ -108,7 +106,7 @@ namespace SpotifyAPI.Web String line; while ((line = StreamReadLine(_inputStream)) != null) { - if (line.Equals("")) + if (String.IsNullOrEmpty(line)) { return; } @@ -116,7 +114,7 @@ namespace SpotifyAPI.Web int separator = line.IndexOf(':'); if (separator == -1) { - throw new Exception("invalid http header line: " + line); + throw new Exception("Invalid HTTP header line: " + line); } String name = line.Substring(0, separator); int pos = separator + 1; @@ -149,9 +147,7 @@ namespace SpotifyAPI.Web var contentLen = Convert.ToInt32(HttpHeaders["Content-Length"]); if (contentLen > MaxPostSize) { - throw new Exception( - String.Format("POST Content-Length({0}) too big for this simple server", - contentLen)); + throw new Exception(String.Format("POST Content-Length({0}) too big for this simple server", contentLen)); } byte[] buf = new byte[BufSize]; int toRead = contentLen; @@ -164,7 +160,7 @@ namespace SpotifyAPI.Web { break; } - throw new Exception("client disconnected during post"); + throw new Exception("Client disconnected during post"); } toRead -= numread; ms.Write(buf, 0, numread); @@ -207,6 +203,8 @@ namespace SpotifyAPI.Web { IsActive = false; _listener.Stop(); + Dispose(); + GC.SuppressFinalize(this); } public void Listen() @@ -276,7 +274,6 @@ namespace SpotifyAPI.Web p.OutputStream.WriteLine("

Spotify Auth canceled!

"); t = new Thread(o => { - //_funcOne(null, col.Get(1), col.Get(0)); if(OnAuth != null) OnAuth(new AuthEventArgs() { diff --git a/SpotifyAPI/Web/SpotifyWebAPI.cs b/SpotifyAPI/Web/SpotifyWebAPI.cs index 462503b5..2db4f654 100644 --- a/SpotifyAPI/Web/SpotifyWebAPI.cs +++ b/SpotifyAPI/Web/SpotifyWebAPI.cs @@ -9,7 +9,7 @@ using SpotifyAPI.Web.Models; namespace SpotifyAPI.Web { - public class SpotifyWebAPI : IDisposable + public sealed class SpotifyWebAPI : IDisposable { public const String APIBase = "https://api.spotify.com/v1"; @@ -35,6 +35,7 @@ namespace SpotifyAPI.Web public void Dispose() { WebClient.Dispose(); + GC.SuppressFinalize(this); } #region Search @@ -56,7 +57,7 @@ namespace SpotifyAPI.Web builder.Append("&type=" + type.GetStringAttribute(",")); builder.Append("&limit=" + limit); builder.Append("&offset=" + offset); - if (market != "") + if (!String.IsNullOrEmpty(market)) builder.Append("&market=" + market); return DownloadData(builder.ToString()); } @@ -80,7 +81,7 @@ namespace SpotifyAPI.Web StringBuilder builder = new StringBuilder(APIBase + "/albums/" + id + "/tracks"); builder.Append("?limit=" + limit); builder.Append("&offset=" + offset); - if (market != "") + if (!String.IsNullOrEmpty(market)) builder.Append("&market=" + market); return DownloadData>(builder.ToString()); } @@ -93,7 +94,7 @@ namespace SpotifyAPI.Web /// public FullAlbum GetAlbum(String id, String market = "") { - if (market == "") + if (String.IsNullOrEmpty(market)) return DownloadData(APIBase + "/albums/" + id); return DownloadData(APIBase + "/albums/" + id + "?market=" + market); } @@ -106,7 +107,7 @@ namespace SpotifyAPI.Web /// public SeveralAlbums GetSeveralAlbums(List ids, String market = "") { - if (market == "") + if (String.IsNullOrEmpty(market)) return DownloadData(APIBase + "/albums?ids=" + string.Join(",", ids.Take(20))); return DownloadData(APIBase + "/albums?market=" + market + "&ids=" + string.Join(",", ids.Take(20))); } @@ -170,7 +171,7 @@ namespace SpotifyAPI.Web builder.Append("?type=" + type.GetStringAttribute(",")); builder.Append("&limit=" + limit); builder.Append("&offset=" + offset); - if (market != "") + if (!String.IsNullOrEmpty(market)) builder.Append("&market=" + market); return DownloadData>(builder.ToString()); } @@ -209,9 +210,9 @@ namespace SpotifyAPI.Web StringBuilder builder = new StringBuilder(APIBase + "/browse/featured-playlists"); builder.Append("?limit=" + limit); builder.Append("&offset=" + offset); - if (locale != "") + if (!String.IsNullOrEmpty(locale)) builder.Append("&locale=" + locale); - if (country != "") + if (!String.IsNullOrEmpty(country)) builder.Append("&country=" + country); if (timestamp != default(DateTime)) builder.Append("×tamp=" + timestamp.ToString("yyyy-MM-ddTHH:mm:ss")); @@ -234,7 +235,7 @@ namespace SpotifyAPI.Web StringBuilder builder = new StringBuilder(APIBase + "/browse/new-releases"); builder.Append("?limit=" + limit); builder.Append("&offset=" + offset); - if (country != "") + if (!String.IsNullOrEmpty(country)) builder.Append("&country=" + country); return DownloadData(builder.ToString()); } @@ -262,9 +263,9 @@ namespace SpotifyAPI.Web StringBuilder builder = new StringBuilder(APIBase + "/browse/categories"); builder.Append("?limit=" + limit); builder.Append("&offset=" + offset); - if (country != "") + if (!String.IsNullOrEmpty(country)) builder.Append("&country=" + country); - if (locale != "") + if (!String.IsNullOrEmpty(locale)) builder.Append("&locale=" + locale); return DownloadData(builder.ToString()); } @@ -286,9 +287,9 @@ namespace SpotifyAPI.Web public Category GetCategory(String categoryId, String country = "", String locale = "") { StringBuilder builder = new StringBuilder(APIBase + "/browse/categories/" + categoryId); - if (country != "") + if (!String.IsNullOrEmpty(country)) builder.Append("?country=" + country); - if (locale != "") + if (!String.IsNullOrEmpty(locale)) builder.Append((country == "" ? "?locale=" : "&locale=") + locale); return DownloadData(builder.ToString()); } @@ -308,7 +309,7 @@ namespace SpotifyAPI.Web StringBuilder builder = new StringBuilder(APIBase + "/browse/categories/" + categoryId + "/playlists"); builder.Append("?limit=" + limit); builder.Append("&offset=" + offset); - if (country != "") + if (!String.IsNullOrEmpty(country)) builder.Append("&country=" + country); return DownloadData(builder.ToString()); } @@ -332,7 +333,7 @@ namespace SpotifyAPI.Web const FollowType followType = FollowType.Artist; //currently only artist is supported. StringBuilder builder = new StringBuilder(APIBase + "/me/following?type=" + followType.GetStringAttribute("")); builder.Append("&limit=" + limit); - if (after != "") + if (!String.IsNullOrEmpty(after)) builder.Append("&after=" + after); return DownloadData(builder.ToString()); } @@ -531,7 +532,7 @@ namespace SpotifyAPI.Web StringBuilder builder = new StringBuilder(APIBase + "/me/tracks"); builder.Append("?limit=" + limit); builder.Append("&offset=" + offset); - if (market != "") + if (!String.IsNullOrEmpty(market)) builder.Append("&market=" + market); return DownloadData>(builder.ToString()); } @@ -605,7 +606,7 @@ namespace SpotifyAPI.Web throw new InvalidOperationException("Auth is required for GetPlaylist"); StringBuilder builder = new StringBuilder(APIBase + "/users/" + userId + "/playlists/" + playlistId); builder.Append("?fields=" + fields); - if (market != "") + if (!String.IsNullOrEmpty(market)) builder.Append("&market=" + market); return DownloadData(builder.ToString()); } @@ -633,7 +634,7 @@ namespace SpotifyAPI.Web builder.Append("?fields=" + fields); builder.Append("&limit=" + limit); builder.Append("&offset=" + offset); - if (market != "") + if (!String.IsNullOrEmpty(market)) builder.Append("&market=" + market); return DownloadData>(builder.ToString()); } @@ -785,7 +786,7 @@ namespace SpotifyAPI.Web {"range_length", rangeLength}, {"insert_before", insertBefore} }; - if (snapshotId != "") + if (!String.IsNullOrEmpty(snapshotId)) body.Add("snapshot_id", snapshotId); return UploadData(APIBase + "/users/" + userId + "/playlists/" + playlistId + "/tracks", body.ToString(Formatting.None), "PUT"); } @@ -828,7 +829,7 @@ namespace SpotifyAPI.Web /// public SeveralTracks GetSeveralTracks(List ids, String market = "") { - if (market == "") + if (String.IsNullOrEmpty(market)) return DownloadData(APIBase + "/tracks?ids=" + string.Join(",", ids.Take(50))); return DownloadData(APIBase + "/tracks?market=" + market + "&ids=" + string.Join(",", ids.Take(50))); } @@ -841,7 +842,7 @@ namespace SpotifyAPI.Web /// public FullTrack GetTrack(String id, String market = "") { - if (market == "") + if (String.IsNullOrEmpty(market)) return DownloadData(APIBase + "/tracks/" + id); return DownloadData(APIBase + "/tracks/" + id + "?market=" + market); } diff --git a/SpotifyAPI/Web/Util.cs b/SpotifyAPI/Web/Util.cs index bcd0d54f..7c3f4548 100644 --- a/SpotifyAPI/Web/Util.cs +++ b/SpotifyAPI/Web/Util.cs @@ -24,7 +24,7 @@ namespace SpotifyAPI.Web } } - public class StringAttribute : Attribute + public sealed class StringAttribute : Attribute { public String Text { get; set; } public StringAttribute(String text)