I'm doing some exercises with Angular2 +, I do not have much experience with this version so I do not know how to achieve the idea that I have.
I want to invoke a service that returns the 10 most popular songs of the moment, when that answer arrives I would like to consume the Youtube API to obtain the data of each of the songs, that is, I want to make a cycle that invokes the API of youtube as many times as results bring the first service.
Here is an excerpt of what I have tried and that obviously does not work.
songs: Song[];
...
getItunesList(): Observable < Song[] > {
let urlBase = "https://www.googleapis.com/youtube/v3/search?part= id,snippet&maxResults=1&q=";
let youtubeKey = "&key=myKey";
return this.http
.get('http://www.mocky.io/v2/59ebfde03100001b02d24da4')
.map(response => response.json().songsList as Song[])
.mergeMap(songsList => {
//Un ciclo para recorrer las diez canciones
songsList.forEach(item => {
//En el ciclo haría un llamado al servicio de youtube, pasando los parametros
return this.http.get(urlBase + songsList[item].name + " " + songsList[item].artist + urlFin);
})
}).map(youTubeData => {
//Ordenar resspuesta de youtube en el arreglo de objetos songs y finalmente devolver un solo arreglo con toda la información
song.url = 'https://www.youtube.com/watch?v=' + youTubeData.items[0].id.videoId;
song.thumbnail = youTubeData.items[0].snippet.thumbnails.default.url;
return song;
});
}
Please, I would like guidance on how to achieve the cycle of HTTP invocations within an answer, thanks!