/// <summary> /// Executes a request and returns the response /// </summary> /// <param name="request">Request to be executed</param> /// <returns>Task<IRestResponse></returns> private async Task <IRestResponse> ExecuteRequestAsync(RestRequest request) { IRestResponse response = null; try { response = await _restClient.ExecuteAsync(request).ConfigureAwait(false); } catch (Exception e) { SpotifyLogger.WriteExceptionToLog(this.GetType().Name, new StackTrace().GetFrame(0).GetMethod().Name, e); throw new HttpException(500, e.Message); } if (response.ErrorException != null) { SpotifyLogger.WriteExceptionToLog(this.GetType().Name, new StackTrace().GetFrame(0).GetMethod().Name, response.ErrorException); throw new HttpException((int)response.StatusCode, response.ErrorException.Message); } else if (response.StatusCode != System.Net.HttpStatusCode.OK) { SpotifyLogger.WriteMessageToLog(this.GetType().Name, new StackTrace().GetFrame(0).GetMethod().Name, $"Status code: {(int)response.StatusCode}, description: {response.StatusDescription}"); throw new HttpException((int)response.StatusCode, response.StatusDescription); } return(response); }
public static List <TopTrackViewModel> ToViewModel(this TopTracks topTracks) { List <TopTrackViewModel> mappedModel = null; try { mappedModel = ModelMapper.Mapper.Map <List <TopTrackViewModel> >(topTracks); } catch (Exception e) { SpotifyLogger.WriteExceptionToLog("TopTracksExtension", "ToViewModel", e); return(null); } return(mappedModel); }
/// <summary> /// Fetch new token from the Spotify Api /// </summary> /// <returns>Task</returns> // https://developer.spotify.com/documentation/general/guides/authorization-guide/ private async Task <AuthenticationResponse> GetAuthenticationTokenResponseAsync() { IRestClient restclient = new RestClient(AuthenticationEndpoint); RestRequest request = RequestBuilder.CreateAuthTokenRequest(ClientId, ClientSecret); try { IRestResponse response = await restclient.ExecuteAsync <AuthenticationResponse>(request).ConfigureAwait(false); var authenticationResponse = DeserializeResponse(response.Content, new AuthenticationResponse()); return(authenticationResponse); } catch (Exception e) { SpotifyLogger.WriteExceptionToLog(this.GetType().Name, new StackTrace().GetFrame(0).GetMethod().Name, e); throw; } }
public List <TopTrackViewModel> Convert(TopTracks source, List <TopTrackViewModel> destination, ResolutionContext context) { List <TopTrackViewModel> ttvm = new List <TopTrackViewModel>(); List <string> albumIds = source.Tracks.Select(x => x.Album.Id).Distinct().ToList(); try { foreach (string id in albumIds) { var album = source.Tracks.FirstOrDefault(a => a.Album.Id == id)?.Album; if (album == null) { continue; } var albumTrackList = source.Tracks.Where(v => v.Album.Id == id).ToList(); ttvm.Add(new TopTrackViewModel() { AlbumId = album.Id, AlbumName = album.Name, SpotifyUrl = album.ExternalUrls.Spotify, AlbumImageSource = album.Images != null && album.Images.Any() ? album.Images.First().Url : string.Empty, ArtistName = album.Artists != null && album.Artists.Any() ? string.Join(", ", album.Artists.Select(n => n.Name).Distinct()) : string.Empty, TrackList = albumTrackList.Select(c => new TrackViewModel() { TrackId = c.Id, TrackName = c.Name, TrackNumber = c.TrackNumber, Duration = Math.Round((decimal)c.Duration_ms / 1000 / 60, 2) }).OrderBy(i => i.TrackNumber).ToList() }); } } catch (Exception e) { SpotifyLogger.WriteExceptionToLog(this.GetType().Name, new StackTrace().GetFrame(0).GetMethod().Name, e); throw; } return(ttvm); }