Skip to content

Commit

Permalink
Track analysis and features + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLarsenNZ committed Nov 5, 2018
1 parent ada0a9c commit de83b3b
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 33 deletions.
59 changes: 59 additions & 0 deletions src/SpotifyApi.NetCore.Tests/TracksApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,64 @@ public async Task GetTracks_TrackIdsMarket_AvailableMarketsIsNull()
// assert
Assert.IsNull(response[0].AvailableMarkets);
}

[TestCategory("Integration")]
//[TestMethod] // not a reliable test - intermittent gateway timeout on Spotify end
public async Task GetTrackAudioAnalysis_TrackId_BarsIsNotNullOrZero()
{
// arrange
const string trackId = "5lA3pwMkBdd24StM90QrNR";

var http = new HttpClient();
http.Timeout = TimeSpan.FromSeconds(30);
var accounts = new AccountsService(http, TestsHelper.GetLocalConfig());

var api = new TracksApi(http, accounts);

// act
var response = await api.GetTrackAudioAnalysis(trackId);

// assert
Assert.IsTrue(response.Bars != null && response.Bars.Length > 0);
}

[TestCategory("Integration")]
[TestMethod]
public async Task GetTrackAudioFeatures_TrackId_TempoNotZero()
{
// arrange
const string trackId = "5lA3pwMkBdd24StM90QrNR";

var http = new HttpClient();
var accounts = new AccountsService(http, TestsHelper.GetLocalConfig());

var api = new TracksApi(http, accounts);

// act
var response = await api.GetTrackAudioFeatures(trackId);

// assert
Assert.IsTrue(response.Tempo != 0);
}

[TestCategory("Integration")]
[TestMethod]
public async Task GetTracksAudioFeatures_TrackIds_ThreeFeatures()
{
// arrange
string[] trackIds = new[] { "5lA3pwMkBdd24StM90QrNR", "20I6sIOMTCkB6w7ryavxtO", "7xGfFoTpQ2E7fRF5lN10tr" };

var http = new HttpClient();
var accounts = new AccountsService(http, TestsHelper.GetLocalConfig());

var api = new TracksApi(http, accounts);

// act
var response = await api.GetTracksAudioFeatures(trackIds);

// assert
Assert.IsTrue(response != null && response.Length == 3 );
}

}
}
37 changes: 35 additions & 2 deletions src/SpotifyApi.NetCore/ITracksApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,58 @@ public interface ITracksApi

#region GetTrackAudioAnalysis

/// <summary>
/// Get a detailed audio analysis for a single track identified by its unique Spotify ID.
/// </summary>
/// <param name="trackId">The Spotify ID for the track.</param>
/// <returns>Task of <see cref="TrackAudioAnalysis" /></returns>
Task<TrackAudioAnalysis> GetTrackAudioAnalysis(string trackId);

/// <summary>
/// Get a detailed audio analysis for a single track identified by its unique Spotify ID.
/// </summary>
/// <param name="trackId">The Spotify ID for the track.</param>
/// <typeparam name="T">Optionally provide your own type to deserialise Spotify's response to.</typeparam>
/// <returns>Task of T. The Spotify response is deserialised as T.</returns>
Task<T> GetTrackAudioAnalysis<T>(string trackId);

#endregion

#region GetTrackAudioFeatures

/// <summary>
/// Get audio feature information for a single track identified by its unique Spotify ID.
/// </summary>
/// <param name="trackId">The Spotify ID for the track.</param>
/// <returns>Task of <see cref="TrackAudioFeatures" /></returns>
Task<TrackAudioFeatures> GetTrackAudioFeatures(string trackId);

/// <summary>
/// Get audio feature information for a single track identified by its unique Spotify ID.
/// </summary>
/// <param name="trackId">The Spotify ID for the track.</param>
/// <typeparam name="T">Optionally provide your own type to deserialise Spotify's response to.</typeparam>
/// <returns>Task of T. The Spotify response is deserialised as T.</returns>
Task<T> GetTrackAudioFeatures<T>(string trackId);

#endregion

#region GetTracksAudioFeatures

Task<TrackAudioFeatures> GetTracksAudioFeatures(string[] trackId);
/// <summary>
/// Get audio features for multiple tracks based on their Spotify IDs.
/// </summary>
/// <param name="trackIds">An array of the Spotify IDs for the tracks. Maximum: 100 IDs.</param>
/// <returns>Task of <see cref="TrackAudioFeatures[]" /></returns>
Task<TrackAudioFeatures[]> GetTracksAudioFeatures(string[] trackIds);

Task<T> GetTracksAudioFeatures<T>(string[] trackId);
/// <summary>
/// Get audio features for multiple tracks based on their Spotify IDs.
/// </summary>
/// <param name="trackIds">An array of the Spotify IDs for the tracks. Maximum: 100 IDs.</param>
/// <typeparam name="T">Optionally provide your own type to deserialise Spotify's response to.</typeparam>
/// <returns>Task of T. The Spotify response is deserialised as T.</returns>
Task<T> GetTracksAudioFeatures<T>(string[] trackIds);

#endregion
}
Expand Down
6 changes: 0 additions & 6 deletions src/SpotifyApi.NetCore/Models/TrackAudioFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ namespace SpotifyApi.NetCore
using Newtonsoft.Json.Converters;

public partial class TrackAudioFeatures
{
[JsonProperty("audio_features")]
public TrackAudioFeature[] AudioFeatures { get; set; }
}

public partial class TrackAudioFeature
{
[JsonProperty("danceability")]
public double Danceability { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/SpotifyApi.NetCore/SpotifyApiErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static async Task<SpotifyApiError> ReadErrorResponse(HttpResponseMessage
if (response.Content == null) return null;

// if not JSON content type
if (response.Content.Headers.ContentType.MediaType != "application/json") return null;
if (response.Content.Headers.ContentType?.MediaType != "application/json") return null;

var content = await response.Content.ReadAsStringAsync();

Expand Down
80 changes: 56 additions & 24 deletions src/SpotifyApi.NetCore/TracksApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,74 @@ public async Task<T> GetTrack<T>(string trackId, string market = null)
/// <returns>Task of T. The Spotify response is deserialised as T.</returns>
public async Task<T> GetTracks<T>(string[] trackIds, string market = null)
{
if (trackIds == null || trackIds.Length ==0) throw new ArgumentNullException("trackIds");
if (trackIds == null || trackIds.Length == 0) throw new ArgumentNullException("trackIds");
string url = $"{BaseUrl}/tracks/?ids={string.Join(",", trackIds)}";
if (market != null) url += $"&market={market}";
return await GetModelFromProperty<T>(url,"tracks");
return await GetModelFromProperty<T>(url, "tracks");
}

#endregion

public Task<TrackAudioAnalysis> GetTrackAudioAnalysis(string trackId)
{
throw new NotImplementedException();
}
#region GetTrackAudioAnalysis

public Task<T> GetTrackAudioAnalysis<T>(string trackId)
{
throw new NotImplementedException();
}
/// <summary>
/// Get a detailed audio analysis for a single track identified by its unique Spotify ID.
/// </summary>
/// <param name="trackId">The Spotify ID for the track.</param>
/// <returns>Task of <see cref="TrackAudioAnalysis" /></returns>
public async Task<TrackAudioAnalysis> GetTrackAudioAnalysis(string trackId) => await GetTrackAudioAnalysis<TrackAudioAnalysis>(trackId);

public Task<TrackAudioFeatures> GetTrackAudioFeatures(string trackId)
{
throw new NotImplementedException();
}
/// <summary>
/// Get a detailed audio analysis for a single track identified by its unique Spotify ID.
/// </summary>
/// <param name="trackId">The Spotify ID for the track.</param>
/// <typeparam name="T">Optionally provide your own type to deserialise Spotify's response to.</typeparam>
/// <returns>Task of T. The Spotify response is deserialised as T.</returns>
public async Task<T> GetTrackAudioAnalysis<T>(string trackId) => await GetModel<T>($"{BaseUrl}/audio-analysis/{trackId}");

public Task<T> GetTrackAudioFeatures<T>(string trackId)
{
throw new NotImplementedException();
}
#endregion

public Task<TrackAudioFeatures> GetTracksAudioFeatures(string[] trackId)
{
throw new NotImplementedException();
}
#region GetTrackAudioFeatures

public Task<T> GetTracksAudioFeatures<T>(string[] trackId)
/// <summary>
/// Get audio feature information for a single track identified by its unique Spotify ID.
/// </summary>
/// <param name="trackId">The Spotify ID for the track.</param>
/// <returns>Task of <see cref="TrackAudioFeatures" /></returns>
public async Task<TrackAudioFeatures> GetTrackAudioFeatures(string trackId) => await GetTrackAudioFeatures<TrackAudioFeatures>(trackId);

/// <summary>
/// Get audio feature information for a single track identified by its unique Spotify ID.
/// </summary>
/// <param name="trackId">The Spotify ID for the track.</param>
/// <typeparam name="T">Optionally provide your own type to deserialise Spotify's response to.</typeparam>
/// <returns>Task of T. The Spotify response is deserialised as T.</returns>
public async Task<T> GetTrackAudioFeatures<T>(string trackId) => await GetModel<T>($"{BaseUrl}/audio-features/{trackId}");

#endregion

#region GetTracksAudioFeatures

/// <summary>
/// Get audio features for multiple tracks based on their Spotify IDs.
/// </summary>
/// <param name="trackIds">An array of the Spotify IDs for the tracks. Maximum: 100 IDs.</param>
/// <returns>Task of <see cref="TrackAudioFeatures[]" /></returns>
public async Task<TrackAudioFeatures[]> GetTracksAudioFeatures(string[] trackIds) => await GetTracksAudioFeatures<TrackAudioFeatures[]>(trackIds);

/// <summary>
/// Get audio features for multiple tracks based on their Spotify IDs.
/// </summary>
/// <param name="trackIds">An array of the Spotify IDs for the tracks. Maximum: 100 IDs.</param>
/// <typeparam name="T">Optionally provide your own type to deserialise Spotify's response to.</typeparam>
/// <returns>Task of T. The Spotify response is deserialised as T.</returns>
public async Task<T> GetTracksAudioFeatures<T>(string[] trackIds)
{
throw new NotImplementedException();
if (trackIds == null || trackIds.Length == 0) throw new ArgumentNullException("trackIds");
string url = $"{BaseUrl}/audio-features/?ids={string.Join(",", trackIds)}";
return await GetModelFromProperty<T>(url, "audio_features");
}

#endregion
}
}

0 comments on commit de83b3b

Please sign in to comment.