Added Tracks client

This commit is contained in:
Jonas Dellinger 2020-05-07 13:48:31 +02:00
parent cd650a7e6d
commit 2f09ba25ec
15 changed files with 215 additions and 2 deletions

View File

@ -7,7 +7,7 @@
"{",
" public class $1",
" {",
" public ${2:string} ${3:Name} { get; set; }",
" public ${2:string} ${3:Name} { get; private set; }",
"",
" $4",
" }",

View File

@ -1,6 +1,20 @@
using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
public interface ITracksClient
{
Task<TracksResponse> GetSeveral(TracksRequest request);
Task<TrackAudioAnalysis> GetAudioAnalysis(string trackId);
Task<TrackAudioFeatures> GetAudioFeatures(string trackId);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716")]
Task<FullTrack> Get(string trackId);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716")]
Task<FullTrack> Get(string trackId, TrackRequest request);
Task<TracksAudioFeaturesResponse> GetSeveralAudioFeatures(TracksAudioFeaturesRequest request);
}
}

View File

@ -1,9 +1,54 @@
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
using URLs = SpotifyAPI.Web.SpotifyUrls;
namespace SpotifyAPI.Web
{
public class TracksClient : APIClient, ITracksClient
{
public TracksClient(IAPIConnector apiConnector) : base(apiConnector) { }
public Task<FullTrack> Get(string trackId)
{
Ensure.ArgumentNotNullOrEmptyString(trackId, nameof(trackId));
return API.Get<FullTrack>(URLs.Track(trackId));
}
public Task<FullTrack> Get(string trackId, TrackRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(trackId, nameof(trackId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<FullTrack>(URLs.Track(trackId), request.BuildQueryParams());
}
public Task<TrackAudioAnalysis> GetAudioAnalysis(string trackId)
{
Ensure.ArgumentNotNullOrEmptyString(trackId, nameof(trackId));
return API.Get<TrackAudioAnalysis>(URLs.AudioAnalysis(trackId));
}
public Task<TrackAudioFeatures> GetAudioFeatures(string trackId)
{
Ensure.ArgumentNotNullOrEmptyString(trackId, nameof(trackId));
return API.Get<TrackAudioFeatures>(URLs.AudioFeatures(trackId));
}
public Task<TracksResponse> GetSeveral(TracksRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<TracksResponse>(URLs.Tracks(), request.BuildQueryParams());
}
public Task<TracksAudioFeaturesResponse> GetSeveralAudioFeatures(TracksAudioFeaturesRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<TracksAudioFeaturesResponse>(URLs.AudioFeatures(), request.BuildQueryParams());
}
}
}

View File

@ -11,7 +11,7 @@ namespace SpotifyAPI.Web
{
public JObject BuildBodyParams()
{
// Make sure everything is okay before building query params
// Make sure everything is okay before building body params
CustomEnsure();
var bodyProps = GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)

View File

@ -0,0 +1,8 @@
namespace SpotifyAPI.Web
{
public class TrackRequest : RequestParams
{
[QueryParam("market")]
public string Market { get; set; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class TracksAudioFeaturesRequest : RequestParams
{
public TracksAudioFeaturesRequest(IList<string> ids)
{
Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids));
Ids = ids;
}
[QueryParam("ids")]
public IList<string> Ids { get; private set; }
}
}

View File

@ -0,0 +1,20 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class TracksRequest : RequestParams
{
public TracksRequest(IList<string> ids)
{
Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids));
Ids = ids;
}
[QueryParam("ids")]
public IList<string> Ids { get; }
[QueryParam("market")]
public string Market { get; set; }
}
}

View File

@ -0,0 +1,18 @@
namespace SpotifyAPI.Web
{
public class Section
{
public float Start { get; private set; }
public float Duration { get; private set; }
public float Confidence { get; private set; }
public float Loudness { get; private set; }
public float Tempo { get; private set; }
public float TempoConfidence { get; private set; }
public int Key { get; private set; }
public float KeyConfidence { get; private set; }
public int Mode { get; private set; }
public float ModeConfidence { get; private set; }
public int TimeSignature { get; private set; }
public float TimeSignatureConfidence { get; private set; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class Segment
{
public float Start { get; private set; }
public float Duration { get; private set; }
public float Confidence { get; private set; }
public float LoudnessStart { get; private set; }
public float LoudnessMax { get; private set; }
public float LoudnessMaxTime { get; private set; }
public float LoudnessEnd { get; private set; }
public List<float> Pitches { get; private set; }
public List<float> Timbre { get; private set; }
}
}

View File

@ -0,0 +1,9 @@
namespace SpotifyAPI.Web
{
public class TimeInterval
{
public float Start { get; private set; }
public float Duration { get; private set; }
public float Confidence { get; private set; }
}
}

View File

@ -0,0 +1,13 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class TrackAudioAnalysis
{
public List<TimeInterval> Bars { get; private set; }
public List<TimeInterval> Beats { get; private set; }
public List<Section> Sections { get; private set; }
public List<Segment> Segments { get; private set; }
public List<TimeInterval> Tatums { get; private set; }
}
}

View File

@ -0,0 +1,24 @@
namespace SpotifyAPI.Web
{
public class TrackAudioFeatures
{
public float Acousticness { get; private set; }
public string AnalysisUrl { get; private set; }
public float Danceability { get; private set; }
public int DurationMs { get; private set; }
public float Energy { get; private set; }
public string Id { get; private set; }
public float Instrumentalness { get; private set; }
public int Key { get; private set; }
public float Liveness { get; private set; }
public float Loudness { get; private set; }
public int Mode { get; private set; }
public float Speechiness { get; private set; }
public float Tempo { get; private set; }
public int TimeSignature { get; private set; }
public string TrackHref { get; private set; }
public string Type { get; private set; }
public string Uri { get; private set; }
public float Valence { get; private set; }
}
}

View File

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class TracksAudioFeaturesResponse
{
public List<TrackAudioFeatures> AudioFeatures { get; private set; }
}
}

View File

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class TracksResponse
{
public List<FullTrack> Tracks { get; private set; }
}
}

View File

@ -51,6 +51,16 @@ namespace SpotifyAPI.Web
public static Uri PlaylistFollowers(string playlistId) => EUri($"playlists/{playlistId}/followers");
public static Uri Tracks() => EUri($"tracks");
public static Uri Track(string trackId) => EUri($"tracks/{trackId}");
public static Uri AudioAnalysis(string trackId) => EUri($"audio-analysis/{trackId}");
public static Uri AudioFeatures(string trackId) => EUri($"audio-features/{trackId}");
public static Uri AudioFeatures() => EUri($"audio-features");
private static Uri EUri(FormattableString path) => new Uri(path.ToString(_provider), UriKind.Relative);
}
}