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
This commit is contained in:
mrnikbobjeff 2015-10-17 00:31:01 +02:00
parent 861f4e5969
commit 10c8944e05
7 changed files with 43 additions and 44 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -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;

View File

@ -24,6 +24,7 @@
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\SpotifyAPI.XML</DocumentationFile>
<NoWarn>1591</NoWarn>
<CodeAnalysisRuleSet>..\..\..\My Data\Dokumente\Visual Studio 2015\Projects\BackupAudioRecorder\AudioRecordTest\MAXRULES.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>

View File

@ -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("<html><body><h1>Spotify Auth canceled!</h1></body></html>");
t = new Thread(o =>
{
//_funcOne(null, col.Get(1), col.Get(0));
if(OnAuth != null)
OnAuth(new AuthEventArgs()
{

View File

@ -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<SearchItem>(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<Paging<SimpleTrack>>(builder.ToString());
}
@ -93,7 +94,7 @@ namespace SpotifyAPI.Web
/// <returns></returns>
public FullAlbum GetAlbum(String id, String market = "")
{
if (market == "")
if (String.IsNullOrEmpty(market))
return DownloadData<FullAlbum>(APIBase + "/albums/" + id);
return DownloadData<FullAlbum>(APIBase + "/albums/" + id + "?market=" + market);
}
@ -106,7 +107,7 @@ namespace SpotifyAPI.Web
/// <returns></returns>
public SeveralAlbums GetSeveralAlbums(List<String> ids, String market = "")
{
if (market == "")
if (String.IsNullOrEmpty(market))
return DownloadData<SeveralAlbums>(APIBase + "/albums?ids=" + string.Join(",", ids.Take(20)));
return DownloadData<SeveralAlbums>(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<Paging<SimpleAlbum>>(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("&timestamp=" + 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<NewAlbumReleases>(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<CategoryList>(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<Category>(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<CategoryPlaylist>(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<FollowedArtists>(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<Paging<SavedTrack>>(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<FullPlaylist>(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<Paging<PlaylistTrack>>(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<Snapshot>(APIBase + "/users/" + userId + "/playlists/" + playlistId + "/tracks", body.ToString(Formatting.None), "PUT");
}
@ -828,7 +829,7 @@ namespace SpotifyAPI.Web
/// <returns></returns>
public SeveralTracks GetSeveralTracks(List<String> ids, String market = "")
{
if (market == "")
if (String.IsNullOrEmpty(market))
return DownloadData<SeveralTracks>(APIBase + "/tracks?ids=" + string.Join(",", ids.Take(50)));
return DownloadData<SeveralTracks>(APIBase + "/tracks?market=" + market + "&ids=" + string.Join(",", ids.Take(50)));
}
@ -841,7 +842,7 @@ namespace SpotifyAPI.Web
/// <returns></returns>
public FullTrack GetTrack(String id, String market = "")
{
if (market == "")
if (String.IsNullOrEmpty(market))
return DownloadData<FullTrack>(APIBase + "/tracks/" + id);
return DownloadData<FullTrack>(APIBase + "/tracks/" + id + "?market=" + market);
}

View File

@ -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)