public async override Task <PageResponse <LastTrack> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastFmApiError error;

            if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
            {
                JToken jtoken = JsonConvert.DeserializeObject <JToken>(json).SelectToken("recenttracks");

                var tracksToken = jtoken.SelectToken("track");

                var tracks = new List <LastTrack>();
                foreach (var track in tracksToken.Children())
                {
                    var t = LastTrack.ParseJToken(track);

                    tracks.Add(t);
                }

                var pageresponse = PageResponse <LastTrack> .CreateSuccessResponse(tracks);

                var attrToken = jtoken.SelectToken("@attr");
                pageresponse.AddPageInfoFromJToken(attrToken);

                return(pageresponse);
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastTrack> >(error));
            }
        }
Ejemplo n.º 2
0
        public async override Task <PageResponse <LastStation> > HandleResponse(HttpResponseMessage response)
        {
            string json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                JToken jtoken = JsonConvert.DeserializeObject <JToken>(json).SelectToken("recentstations");

                var stationsToken = jtoken.SelectToken("station");

                var stations = stationsToken.Children().Select(LastStation.ParseJToken).ToList();

                var pageresponse = PageResponse <LastStation> .CreateSuccessResponse(stations);

                var attrToken = jtoken.SelectToken("@attr");
                pageresponse.AddPageInfoFromJToken(attrToken);

                return(pageresponse);
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastStation> >(status));
            }
        }
Ejemplo n.º 3
0
        public override async Task <PageResponse <LastArtist> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            var jtoken       = JsonConvert.DeserializeObject <JToken>(json);
            var resultsToken = jtoken.SelectToken("topartists");

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                if (string.IsNullOrEmpty(resultsToken.SelectToken("@attr.tag").Value <string>()))
                {
                    return(PageResponse <LastArtist> .CreateErrorResponse(LastResponseStatus.MissingParameters));
                }

                var itemsToken = resultsToken.SelectToken("artist");

                return(PageResponse <LastArtist> .CreateSuccessResponse(itemsToken, resultsToken, LastArtist.ParseJToken, LastPageResultsType.Attr));
            }
            else
            {
                // The tag api always returns a "valid" response, so
                return(PageResponse <LastArtist> .CreateErrorResponse(status));
            }
        }
Ejemplo n.º 4
0
        public override async Task <LastResponse <string> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            if (LastFm.IsResponseValid(json, out LastResponseStatus status) && response.IsSuccessStatusCode)
            {
                var token = JsonConvert.DeserializeObject <JObject>(json).GetValue("token");
                return(LastResponse <string> .CreateSuccessResponse(token.Value <string>()));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <LastResponse <string> >(status));
            }
        }
Ejemplo n.º 5
0
        public override async Task <ScrobbleResponse> HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                return(await ScrobbleResponse.CreateSuccessResponse(json));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <ScrobbleResponse>(status));
            }
        }
Ejemplo n.º 6
0
        public async static Task <LastResponse> HandleResponse(HttpResponseMessage response)
        {
            string json = await response.Content.ReadAsStringAsync();

            LastFmApiError error;

            if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
            {
                return(LastResponse.CreateSuccessResponse());
            }
            else
            {
                return(LastResponse.CreateErrorResponse <LastResponse>(error));
            }
        }
Ejemplo n.º 7
0
        public async override Task <PageResponse <LastTag> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken     = JsonConvert.DeserializeObject <JToken>(json);
                var itemsToken = jtoken.SelectToken("toptags.tag");

                return(PageResponse <LastTag> .CreateSuccessResponse(itemsToken, LastTag.ParseJToken));
            }

            return(PageResponse <LastTag> .CreateErrorResponse(status));
        }
Ejemplo n.º 8
0
        public void IsResponseValid()
        {
            LastResponseStatus status;

            Assert.IsFalse(LastFm.IsResponseValid(null, out status));
            Assert.IsFalse(LastFm.IsResponseValid("{invalid json", out status));

            var error6 = Encoding.UTF8.GetString(ArtistApiResponses.ArtistGetTagsError);

            Assert.IsFalse(LastFm.IsResponseValid(error6, out status));
            Assert.AreEqual(LastResponseStatus.MissingParameters, status);

            var goodResponse = Encoding.UTF8.GetString(ArtistApiResponses.ArtistGetInfoSuccess);

            Assert.IsTrue(LastFm.IsResponseValid(goodResponse, out status));
            Assert.AreEqual(LastResponseStatus.Successful, status);
        }
Ejemplo n.º 9
0
        public async override Task <PageResponse <LastTag> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken     = JsonConvert.DeserializeObject <JToken>(json).SelectToken("similartags");
                var itemsToken = jtoken.SelectToken("tag");
                var attrToken  = jtoken.SelectToken("@attr");
                var relatedTag = attrToken.SelectToken("tag").Value <string>();

                return(PageResponse <LastTag> .CreateSuccessResponse(itemsToken, jt => LastTag.ParseJToken(jt, relatedTag)));
            }

            return(LastResponse.CreateErrorResponse <PageResponse <LastTag> >(status));
        }
Ejemplo n.º 10
0
        public async override Task <LastResponse <LastAlbum> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken = JsonConvert.DeserializeObject <JToken>(json);
                var album  = LastAlbum.ParseJToken(jtoken.SelectToken("album"));

                return(LastResponse <LastAlbum> .CreateSuccessResponse(album));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <LastResponse <LastAlbum> >(status));
            }
        }
Ejemplo n.º 11
0
        public async override Task <PageResponse <LastTrack> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastFmApiError error;

            if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
            {
                var jtoken     = JsonConvert.DeserializeObject <JToken>(json);
                var itemsToken = jtoken.SelectToken("similartracks").SelectToken("track");

                return(PageResponse <LastTrack> .CreateSuccessResponse(itemsToken, null, LastTrack.ParseJToken));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastTrack> >(error));
            }
        }
        public async override Task <LastResponse <LastArtist> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastFmApiError error;

            if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
            {
                var jtoken = JsonConvert.DeserializeObject <JToken>(json);
                var artist = LastArtist.ParseJToken(jtoken.SelectToken("artist"));

                return(LastResponse <LastArtist> .CreateSuccessResponse(artist));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <LastResponse <LastArtist> >(error));
            }
        }
Ejemplo n.º 13
0
        public async override Task <PageResponse <LastArtist> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken     = JsonConvert.DeserializeObject <JToken>(json);
                var itemsToken = jtoken.SelectToken("similarartists").SelectToken("artist");

                return(PageResponse <LastArtist> .CreateSuccessResponse(itemsToken, LastArtist.ParseJToken));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastArtist> >(status));
            }
        }
Ejemplo n.º 14
0
        public override async Task <LastResponse <LastUserSession> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var sessionObject = JsonConvert.DeserializeObject <JObject>(json).GetValue("session");
                var session       = JsonConvert.DeserializeObject <LastUserSession>(sessionObject.ToString());

                return(LastResponse <LastUserSession> .CreateSuccessResponse(session));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <LastResponse <LastUserSession> >(status));
            }
        }
Ejemplo n.º 15
0
        public override async Task <PageResponse <LastWeeklyChartList> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken         = JsonConvert.DeserializeObject <JToken>(json);
                var chartListToken = jtoken.SelectToken("weeklychartlist");
                var itemsToken     = chartListToken.SelectToken("chart");
                var pageInfoToken  = chartListToken.SelectToken("@attr");

                return(PageResponse <LastWeeklyChartList> .CreateSuccessResponse(itemsToken, pageInfoToken, LastWeeklyChartList.ParseJToken, LastPageResultsType.Attr));
            }

            return(LastResponse.CreateErrorResponse <PageResponse <LastWeeklyChartList> >(status));
        }
Ejemplo n.º 16
0
        public override async Task <PageResponse <LastAlbum> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken        = JToken.Parse(json);
                var itemsToken    = jtoken.SelectToken("topalbums").SelectToken("album");
                var pageInfoToken = jtoken.SelectToken("@attr");

                return(PageResponse <LastAlbum> .CreateSuccessResponse(itemsToken, pageInfoToken, LastAlbum.ParseJToken, LastPageResultsType.Attr));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastAlbum> >(status));
            }
        }
Ejemplo n.º 17
0
        public override async Task <LastResponse <LastUser> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken    = JsonConvert.DeserializeObject <JToken>(json);
                var userToken = jtoken.SelectToken("user");
                var user      = LastUser.ParseJToken(userToken);

                return(LastResponse <LastUser> .CreateSuccessResponse(user));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <LastResponse <LastUser> >(status));
            }
        }
Ejemplo n.º 18
0
        public async override Task <PageResponse <LastTag> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastFmApiError error;

            if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
            {
                var jtoken       = JsonConvert.DeserializeObject <JToken>(json);
                var resultsToken = jtoken.SelectToken("toptags");
                var itemsToken   = resultsToken.SelectToken("tag");

                return(PageResponse <LastTag> .CreateSuccessResponse(itemsToken, resultsToken, LastTag.ParseJToken, LastPageResultsType.Attr));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastTag> >(error));
            }
        }
Ejemplo n.º 19
0
        public override async Task <PageResponse <LastTag> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jo            = JObject.Parse(json);
                var tagsToken     = jo.SelectToken("tags.tag");
                var pageInfoToken = jo.SelectToken("@attr");

                return(PageResponse <LastTag> .CreateSuccessResponse(tagsToken, pageInfoToken, LastTag.ParseJToken, LastPageResultsType.Attr));
            }
            else
            {
                return(PageResponse <LastTag> .CreateErrorResponse(status));
            }
        }
Ejemplo n.º 20
0
        public override async Task <PageResponse <LastTrack> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken       = JsonConvert.DeserializeObject <JToken>(json);
                var resultsToken = jtoken.SelectToken("results");
                var itemsToken   = resultsToken.SelectToken("trackmatches").SelectToken("track");

                return(PageResponse <LastTrack> .CreateSuccessResponse(itemsToken, resultsToken, LastTrack.ParseJToken, LastPageResultsType.OpenQuery));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastTrack> >(status));
            }
        }
Ejemplo n.º 21
0
        public async override Task <PageResponse <LastTrack> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastResponseStatus status;

            if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
            {
                var jtoken        = JsonConvert.DeserializeObject <JToken>(json).SelectToken("tracks");
                var tracksToken   = jtoken.SelectToken("track");
                var pageInfoToken = jtoken.SelectToken("@attr");

                return(PageResponse <LastTrack> .CreateSuccessResponse(tracksToken, pageInfoToken, LastTrack.ParseJToken, LastPageResultsType.Attr));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastTrack> >(status));
            }
        }
Ejemplo n.º 22
0
        public override async Task <PageResponse <LastShout> > HandleResponse(HttpResponseMessage response)
        {
            var json = await response.Content.ReadAsStringAsync();

            LastFmApiError error;

            if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
            {
                var jtoken        = JsonConvert.DeserializeObject <JToken>(json);
                var shoutsToken   = jtoken.SelectToken("shouts");
                var itemsToken    = shoutsToken.SelectToken("shout");
                var pageInfoToken = shoutsToken.SelectToken("@attr");

                return(PageResponse <LastShout> .CreateSuccessResponse(itemsToken, pageInfoToken, LastShout.ParseJToken));
            }
            else
            {
                return(LastResponse.CreateErrorResponse <PageResponse <LastShout> >(error));
            }
        }
Ejemplo n.º 23
0
        public async Task <LastResponse> ScrobbleAsync(Scrobble scrobble)
        {
            const string apiMethod = "track.scrobble";

            var methodParameters = new Dictionary <string, string>
            {
                { "artist", scrobble.Artist },
                { "album", scrobble.Album },
                { "track", scrobble.Track },
                { "albumArtist", scrobble.AlbumArtist },
                { "chosenByUser", Convert.ToInt32(scrobble.ChosenByUser).ToString() },
                { "timestamp", scrobble.TimePlayed.ToUnixTimestamp().ToString() },
                { "sk", Auth.UserSession.Token }
            };

            var apisig = Auth.GenerateMethodSignature(apiMethod, methodParameters);

            var postContent = LastFm.CreatePostBody(apiMethod,
                                                    Auth.ApiKey,
                                                    apisig,
                                                    methodParameters);

            var httpClient = new HttpClient();
            HttpResponseMessage response = await httpClient.PostAsync(LastFm.ApiRoot, postContent);

            string json = await response.Content.ReadAsStringAsync();

            LastFmApiError error;

            if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
            {
                return(LastResponse.CreateSuccessResponse());
            }
            else
            {
                return(LastResponse.CreateErrorResponse <LastResponse>(error));
            }
        }