api_call update

This commit is contained in:
William Noris 2021-10-19 10:31:51 +02:00
parent f5b9fb28de
commit 91dd72c524
4 changed files with 25 additions and 14 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules/
dist/
.parcel-cache/
.parcel-cache/lock.mdb

View File

@ -24,9 +24,6 @@
</section>
<section class="results">
</section>
</body>
</html>

View File

@ -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);

View File

@ -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();
})