Exemplo n.º 1
0
        protected async Task <T> ExecuteAsyncInternal()
        {
            SetParameters();

            var toRemove = Parameters.Where(p => String.IsNullOrEmpty(p.Value)).ToList();

            foreach (var parameter in toRemove)
            {
                Parameters.Remove(parameter.Key);
            }

            Url = BuildRequestUrl();

            var apisig = Auth.GenerateMethodSignature(Method, Parameters);

            var postContent = LastFm.CreatePostBody(Method,
                                                    Auth.ApiKey,
                                                    apisig,
                                                    Parameters);

            try
            {
                var httpClient = HttpClient;
                using (var response = await httpClient.PostAsync(Url, postContent))
                {
                    return(await HandleResponse(response));
                }
            }
            catch (HttpRequestException)
            {
                return(LastResponse.CreateErrorResponse <T>(LastResponseStatus.RequestFailed));
            }
        }
        public override async Task <T> ExecuteAsync()
        {
            SetParameters();

            Url = BuildRequestUrl();

            var apisig = Auth.GenerateMethodSignature(Method, Parameters);

            var postContent = LastFm.CreatePostBody(Method,
                                                    Auth.ApiKey,
                                                    apisig,
                                                    Parameters);

            try
            {
                var httpClient = GetHttpClient();
                var response   = await httpClient.PostAsync(Url, postContent);

                return(await HandleResponse(response));
            }
            catch (HttpRequestException)
            {
                if (LastFm.CatchRequestExceptions)
                {
                    return(LastResponse.CreateErrorResponse <T>(LastFmApiError.RequestFailed));
                }
                else
                {
                    throw;
                }
            }
        }
Exemplo n.º 3
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));
            }
        }