Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions SoundCloud.NET/SoundCloudApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;

namespace SoundCloud.NET
Expand Down Expand Up @@ -95,7 +96,7 @@ internal class SoundCloudApi : SoundCloudClient
{ ApiCommand.Playlist, new Uri("https://api.soundcloud.com/playlists/{0}.json") },

//Resolver
{ ApiCommand.Resolve, new Uri("https://api.soundcloud.com/resolve.json?url={0}") },
{ ApiCommand.Resolve, new Uri("https://api.soundcloud.com/resolve?url={0}") },
};

#endregion Private Properties
Expand All @@ -113,10 +114,7 @@ internal class SoundCloudApi : SoundCloudClient
/// <param name="parameters">Parameters format of an api command uri.</param>
public static T ApiAction<T>(ApiCommand command, params object[] parameters)
{
var uri =
ApiDictionary[command]
.With(parameters);

var uri = ApiDictionary[command].With(parameters);
return ApiAction<T>(uri);
}

Expand All @@ -130,12 +128,8 @@ public static T ApiAction<T>(ApiCommand command, params object[] parameters)
/// <param name="parameters">Parameters format of an api command uri.</param>
public static T ApiAction<T>(ApiCommand command, HttpMethod method, params object[] parameters)
{
var uri =
ApiDictionary[command]
.With(parameters);

var uri = ApiDictionary[command].With(parameters);
bool requireAuthentication = command != ApiCommand.UserCredentialsFlow;

return ApiAction<T>(uri, method, requireAuthentication);
}

Expand All @@ -147,11 +141,7 @@ public static T ApiAction<T>(ApiCommand command, HttpMethod method, params objec
/// <param name="extraParameters">Dictionnary of parameters to be passed in the api action uri.</param>
public static T ApiAction<T>(ApiCommand command, Dictionary<string, object> extraParameters)
{
var uri =
ApiDictionary[command]
.UriAppendingParameters(extraParameters);


var uri = ApiDictionary[command].UriAppendingParameters(extraParameters);
return ApiAction<T>(uri);
}

Expand All @@ -166,11 +156,7 @@ public static T ApiAction<T>(ApiCommand command, Dictionary<string, object> extr
/// <param name="parameters"></param>
public static T ApiAction<T>(ApiCommand command, HttpMethod method, Dictionary<string, object> extraParameters, params object[] parameters)
{
var uri =
ApiDictionary[command]
.UriAppendingParameters(extraParameters)
.With(parameters);

var uri = ApiDictionary[command].UriAppendingParameters(extraParameters).With(parameters);
return ApiAction<T>(uri, method);
}

Expand Down Expand Up @@ -199,12 +185,15 @@ public static T ApiAction<T>(Uri uri, HttpMethod method = HttpMethod.Get, bool r
api = uri.UriWithAuthorizedUri(SoundCloudAccessToken.AccessToken);
}

var request = WebRequest.Create(api);
var request = (HttpWebRequest)WebRequest.Create(api);

request.AllowAutoRedirect = false;

request.Method = method.ToString().ToUpperInvariant();

// Force returned type to JSON
request.ContentType = "application/json";
request.ContentLength = 0;

//add gzip enabled header
if (EnableGZip) request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
Expand All @@ -216,9 +205,25 @@ public static T ApiAction<T>(Uri uri, HttpMethod method = HttpMethod.Get, bool r
{
//OnApiActionExecuting Event
OnApiActionExecuting(EventArgs.Empty);

response = (HttpWebResponse)request.GetResponse();

//header check for redirects
if (response.Headers != null && response.Headers.AllKeys.Contains("Location"))
{
//got a location url 302
var loc = response.Headers["Location"];
if (!string.IsNullOrEmpty(loc))
{
if (!string.IsNullOrEmpty(loc.Trim()))
{
uri = new Uri(loc);
return ApiAction<T>(uri, method, requireAuthentication);
}
}
}



if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
{
var stream = response.GetResponseStream();
Expand Down Expand Up @@ -269,4 +274,4 @@ public static T ApiAction<T>(Uri uri, HttpMethod method = HttpMethod.Get, bool r

#endregion Shared Methods
}
}
}
1 change: 1 addition & 0 deletions SoundCloud.NET/SoundCloudException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace SoundCloud.NET
/// <summary>
/// Handles SoundCloud api exceptions.
/// </summary>
[Serializable]
public class SoundCloudException : Exception
{
/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions SoundCloud.NET/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ public static Track GetTrack(int id)
return SoundCloudApi.ApiAction<Track>(ApiCommand.Track, id);
}

/// <summary>
/// Returns a track by track permanent url.
/// </summary>
/// <param name="id"> Track id. </param>
public static Track GetTrack(string url)
{
return SoundCloudApi.ApiAction<Track>(ApiCommand.Resolve, url);
}


/// <summary>
/// Returns a collection of tracks after filtering.
/// </summary>
Expand Down
37 changes: 19 additions & 18 deletions SoundCloud.NET/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/*
SoundCloud.NET Library For Sound Cloud Api Management.
Copyright (C) 2011 Haythem Tlili

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
SoundCloud.NET Library For Sound Cloud Api Management.
Copyright (C) 2011 Haythem Tlili
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SoundCloud.NET
Expand Down Expand Up @@ -98,7 +99,7 @@ public static Uri UriAppendingQueryString(this Uri uri, string name, string valu
return
new UriBuilder(uri)
{
Query = (uri.Query + "&" + name + "=" + value).TrimStart('&')
Query = (uri.Query + "&" + name + "=" + value).TrimStart('&').TrimStart('?')
}
.Uri;
}
Expand All @@ -107,7 +108,7 @@ public static Uri UriAppendingQueryString(this Uri uri, string querystring)
return
new UriBuilder(uri)
{
Query = (uri.Query + "&" + querystring).TrimStart('&')
Query = (uri.Query + "&" + querystring).TrimStart('&').TrimStart('?')
}
.Uri;
}
Expand Down