api_call update
This commit is contained in:
parent
f5b9fb28de
commit
91dd72c524
|
@ -1,3 +1,4 @@
|
|||
node_modules/
|
||||
dist/
|
||||
.parcel-cache/
|
||||
.parcel-cache/lock.mdb
|
||||
|
|
|
@ -24,9 +24,6 @@
|
|||
|
||||
</section>
|
||||
|
||||
<section class="results">
|
||||
|
||||
</section>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -1,18 +1,31 @@
|
|||
import { api_call } from "./util";
|
||||
import { api_call, API_URL } from "./util";
|
||||
|
||||
const planetes = document.getElementById("resultats");
|
||||
|
||||
console.log('planet.js loaded');
|
||||
|
||||
api_call("planets", console.log);
|
||||
api_call(API_URL + "/planets", console.log);
|
||||
|
||||
|
||||
function remplissage(prout){
|
||||
console.log(prout.results[0]);
|
||||
function remplissage(results){
|
||||
|
||||
const previousUrl = results.previous;
|
||||
const nextUrl = results.next;
|
||||
const tabResults = results.results;
|
||||
|
||||
for(resultPlanet of tabResults){
|
||||
const nom = resultPlanet.name;
|
||||
const diametre = resultPlanet.diameter;
|
||||
const climate = resultPlanet.climate;
|
||||
const terrain = resultPlanet.terrain;
|
||||
const population = resultPlanet.population;
|
||||
|
||||
// const tabResidents = resultPLanet.residents;
|
||||
// const tabResidents = call_api(`people/`);
|
||||
const tabFilms = resultPLanet.films;
|
||||
|
||||
}
|
||||
|
||||
api_call("films/1", (response) => {
|
||||
console.log(response);
|
||||
});
|
||||
|
||||
planetes.innerHTML =
|
||||
`<section class="resultat">
|
||||
|
@ -25,4 +38,4 @@ function remplissage(prout){
|
|||
</section>`;
|
||||
}
|
||||
|
||||
api_call("planets", remplissage);
|
||||
// api_call(API_URL + "/planets", remplissage);
|
|
@ -1,14 +1,14 @@
|
|||
console.log("util.js loaded");
|
||||
|
||||
const URL = "https://swapi.dev/api";
|
||||
export const API_URL = "https://swapi.dev/api";
|
||||
|
||||
/**
|
||||
* Envoie une requête GET à l'api swappi
|
||||
* @param {string} endpoint l'endpoint à interroger (ex: planets)
|
||||
* @param {function} callback La fonction qui traitera la réponse JSON en retour
|
||||
*/
|
||||
export const api_call = (endpoint, callback) => {
|
||||
fetch(`${URL}/${endpoint}`)
|
||||
export const api_call = (url, callback) => {
|
||||
fetch(url)
|
||||
.then(response => {
|
||||
return response.json();
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue