private void UpdateClientAuthorization(SpotifyClient client)
 {
     if (Authorization != null)
     {
         client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Authorization.AccessToken);
     }
 }
        internal SpotifySearchResponse(JObject data, SpotifyClient client) : base(client)
        {
            var artists = data["artists"];

            if (artists != null)
            {
                Artists = new SpotifyPagingResponse <SpotifyArtist>((JObject)artists, (o, c) => new SpotifyArtist(c, o), j => (JObject)j["artists"], client);
            }
            else
            {
                Artists = new SpotifyPagingResponse <SpotifyArtist>();
            }

            var albums = data["albums"];

            if (albums != null)
            {
                Albums = new SpotifyPagingResponse <SpotifyAlbumReference>((JObject)albums, (o, c) => new SpotifyAlbumReference(o, c), j => (JObject)j["albums"], client);
            }
            else
            {
                Albums = new SpotifyPagingResponse <SpotifyAlbumReference>();
            }

            var tracks = data["tracks"];

            if (tracks != null)
            {
                Tracks = new SpotifyPagingResponse <SpotifyTrack>((JObject)tracks, (o, c) => new SpotifyTrack(o, c), j => (JObject)j["tracks"], client);
            }
            else
            {
                Tracks = new SpotifyPagingResponse <SpotifyTrack>();
            }
        }
Ejemplo n.º 3
0
        internal SpotifyAlbumReference(JObject data, SpotifyClient client) : base(client)
        {
            Type             = data["album_type"].ToObject <AlbumType>();
            Artists          = data["artists"].ToObject <IEnumerable <JObject> >().Select(a => new SpotifyArtistReference(client, a)).ToImmutableArray();
            AvailableMarkets = data["available_markets"].ToObject <ImmutableArray <string> >();
            ExternalUrls     = new SpotifyExternalUrlsCollection(data["external_urls"].ToObject <IDictionary <string, string> >());
            Id     = new SpotifyUri(data["uri"].ToObject <string>());
            Images = data["images"].ToObject <ImmutableArray <SpotifyImage> >();
            Name   = data["name"].ToObject <string>();
            var rdp = data["release_date_precision"].ToObject <string>();
            var rd  = data["release_date"].ToObject <string>().Split('-');

            switch (rdp)
            {
            case "year":
                ReleaseDate = new DateTimeOffset(int.Parse(rd[0]), 1, 1, 0, 0, 0, TimeSpan.Zero);
                break;

            case "month":
                ReleaseDate = new DateTimeOffset(int.Parse(rd[0]), int.Parse(rd[1]), 1, 0, 0, 0, TimeSpan.Zero);
                break;

            case "day":
                ReleaseDate = new DateTimeOffset(int.Parse(rd[0]), int.Parse(rd[1]), int.Parse(rd[2]), 0, 0, 0, TimeSpan.Zero);
                break;

            default:
                throw new InvalidOperationException("The specified release date precision was not year, month, or day.");
            }
        }
        internal SpotifyPagingResponse(JObject baseData, Func <JObject, SpotifyClient, T> objectBuilder, Func <JObject, JObject> dataAccessor, SpotifyClient client)
        {
            _objectBuilder = objectBuilder;
            _dataAccessor  = dataAccessor;
            _client        = client;
            _isEmpty       = false;

            UpdateData(baseData);
        }
Ejemplo n.º 5
0
 internal SpotifyArtist(SpotifyClient client, JObject data) : base(client)
 {
     ExternalUrls     = new SpotifyExternalUrlsCollection(data["external_urls"].ToObject <IDictionary <string, string> >());
     FollowerCount    = data["followers"]["total"].ToObject <int>();
     AssociatedGenres = data["genres"].ToObject <ImmutableArray <string> >();
     Images           = data["images"].ToObject <ImmutableArray <SpotifyImage> >();
     Name             = data["name"].ToObject <string>();
     Popularity       = data["popularity"].ToObject <int>();
     Id = new SpotifyUri(data["uri"].ToObject <string>());
 }
        public async Task <bool> EnsureAuthorizedAsync(SpotifyClient client)
        {
            if (Authorization == null || string.IsNullOrEmpty(Authorization.AccessToken) ||
                Authorization.ExpirationTime.ToUnixTimeSeconds() < DateTimeOffset.Now.ToUnixTimeSeconds())
            {
                var http = client.HttpClient;

                var request = new HttpRequestMessage
                {
                    RequestUri = ClientCredentialsAuthorizationEndpoint,
                    Content    = new FormUrlEncodedContent(new Dictionary <string, string>
                    {
                        { "grant_type", "client_credentials" }
                    }),
                    Method = HttpMethod.Post
                };

                request.Headers.Authorization = new AuthenticationHeaderValue(
                    "Basic", EncodeBase64(CombinedClientCredentials));

                var response = await http.SendAsync(request, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);

                var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var jsonData = JObject.Parse(responseString);

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    await HandleAuthenticationErrorAsync(new AuthorizationError((int)response.StatusCode, jsonData)).ConfigureAwait(false);

                    throw new InvalidOperationException("Cannot continue with request after authorization error.");
                }

                response.EnsureSuccessStatusCode();

                Authorization = new AuthorizationSet
                {
                    AccessToken    = jsonData["access_token"].ToObject <string>(),
                    ExpirationTime = DateTimeOffset.Now.AddSeconds(jsonData["expires_in"].ToObject <int>())
                };

                UpdateClientAuthorization(client);

                request.Dispose();
                response.Dispose();

                return(true);
            }

            return(false);
        }
Ejemplo n.º 7
0
 internal SpotifyTrackReference(SpotifyClient client, JObject data) : base(client)
 {
     Artists           = data["artists"].ToObject <IEnumerable <JObject> >().Select(a => new SpotifyArtistReference(client, a)).ToImmutableArray();
     AvailableMarkets  = data["available_markets"].ToObject <ImmutableArray <string> >();
     DiscNumber        = data["disc_number"].ToObject <int>();
     Duration          = TimeSpan.FromMilliseconds(data["duration_ms"].ToObject <int>());
     HasExplicitLyrics = data["explicit"].ToObject <bool>();
     ExternalUrls      = new SpotifyExternalUrlsCollection(data["external_urls"].ToObject <IDictionary <string, string> >());
     Id = new SpotifyUri(data["uri"].ToObject <string>());
     // TODO: IS_PLAYABLE, LINKED_FROM, RESTRICTIONS
     Name         = data["name"].ToObject <string>();
     PreviewUrl   = data["preview_url"].ToObject <string>();
     TrackNumber  = data["track_number"].ToObject <int>();
     IsLocalTrack = data["is_local"].ToObject <bool>();
 }
Ejemplo n.º 8
0
        internal SpotifyAlbum(SpotifyClient client, JObject data) : base(client)
        {
            Type             = data["album_type"].ToObject <AlbumType>();
            Artists          = data["artists"].ToObject <IEnumerable <JObject> >().Select(a => new SpotifyArtistReference(client, a)).ToImmutableArray();
            AvailableMarkets = data["available_markets"].ToObject <ImmutableArray <string> >();
            Copyrights       = data["copyrights"]
                               .ToObject <IEnumerable <JObject> >()
                               .Select(a =>
                                       new SpotifyAlbumCopyright(a["text"].ToObject <string>(), a["type"].ToObject <string>() == "C" ? AlbumCopyrightType.Copyright : AlbumCopyrightType.PerformanceCopyright))
                               .ToImmutableArray();
            ExternalIds  = new SpotifyExternalIdsCollection(data["external_ids"].ToObject <IDictionary <string, string> >());
            ExternalUrls = new SpotifyExternalUrlsCollection(data["external_urls"].ToObject <IDictionary <string, string> >());
            Id           = new SpotifyUri(data["uri"].ToObject <string>());
            Images       = data["images"].ToObject <ImmutableArray <SpotifyImage> >();
            Label        = data["label"].ToObject <string>();
            Name         = data["name"].ToObject <string>();
            Popularity   = data["popularity"].ToObject <int>();

            var rdp = data["release_date_precision"].ToObject <string>();
            var rd  = data["release_date"].ToObject <string>().Split('-');

            switch (rdp)
            {
            case "year":
                ReleaseDate = new DateTimeOffset(int.Parse(rd[0]), 1, 1, 0, 0, 0, TimeSpan.Zero);
                break;

            case "month":
                ReleaseDate = new DateTimeOffset(int.Parse(rd[0]), int.Parse(rd[1]), 1, 0, 0, 0, TimeSpan.Zero);
                break;

            case "day":
                ReleaseDate = new DateTimeOffset(int.Parse(rd[0]), int.Parse(rd[1]), int.Parse(rd[2]), 0, 0, 0, TimeSpan.Zero);
                break;

            default:
                throw new InvalidOperationException("The specified release date precision was not year, month, or day.");
            }

            // TODO: Track Relinking

            Tracks = new SpotifyPagingResponse <SpotifyTrackReference>((JObject)data["tracks"], (o, c) => new SpotifyTrackReference(c, o), a => (JObject)a["tracks"], client);
        }
Ejemplo n.º 9
0
 internal SpotifyEntity(SpotifyClient client)
 {
     Client = client;
 }
Ejemplo n.º 10
0
 internal SpotifyArtistReference(SpotifyClient client, JObject data) : base(client)
 {
     ExternalUrls = new SpotifyExternalUrlsCollection(data["external_urls"].ToObject <IDictionary <string, string> >());
     Id           = new SpotifyUri(data["uri"].ToObject <string>());
     Name         = data["name"].ToObject <string>();
 }
Ejemplo n.º 11
0
 internal SpotifyReference(SpotifyClient client) : base(client)
 {
 }