<reponame>Hanippa/DI_Bootcamp<filename>Week_9/Day_3/ExercisesXP/script.js<gh_stars>0// 🌟 Exercise 1 : Giphy API
// Instructions

// With your knewly accumulated knowledge of the Fetch API lets write some cool code!

//     You will work with this part of the documention

//     You will use this Gif URL: https://api.giphy.com/v1/gifs/search?q=hilarious&rating=g&api_key=ngw6fo1pu3tjgnp9jnlp7vnwvfqb9yn7

// Explanation of the Gif URL and the queries

//     q Request Parameter: Search query term or phrase. Above, the URL is searching for “hilarious” gifs

//     rating Request Parameter: Filters results by specified rating. We are searching for Level 1 gifs. Check out the ratings documentation

//     api_key Request Paramater : GIPHY API Key. Our API KEY is ngw6fo1pu3tjgnp9jnlp7vnwvfqb9yn7

//     Create a program to retrieve the data from the API URL provided above.
//     Use the fetch() method to make a GET request to the Giphy API and Console.log the Javascript Object that you receive.
//     Make sure to check the status of the Response and to catch any occuring errors.


const fetchGifs = async (q , rating , limit , offset , api_key) => {
    const url = `https://api.giphy.com/v1/gifs/search?q=${q}&rating=${rating}&api_key=${api_key}&limit=${limit}&offset=${offset}`
    let response = await fetch(url)
    if(response.status !== 200){
        throw new Error('cannot fetch gifs')
    }
    let json = await response.json()

    return json
}

const logdata = async (q , rating , limit ,offset, api_key) =>{
    let response = await fetchGifs(q , rating , limit,offset , api_key )
    console.log(response)
}


// 🌟 Exercise 2 : Giphy API
// Instructions

//     Read carefully the documention to understand all the possible queries that the URL accept.
//     Use the Fetch API to retrieve 10 gifs about the “sun”. The starting position of the results should be 2.
//     Make sure to check the status of the Response and to catch any occuring errors.
//     Console.log the Javascript Object that you receive.




logdata('sun' , 'g' , 10, 2, 'ngw6fo1pu3tjgnp9jnlp7vnwvfqb9yn7')


// 🌟 Exercise 3 : Async function
// Instructions

// Improve the program below :

// fetch("https://www.swapi.tech/api/starships/9/")
//     .then(response => response.json())
//     .then(objectStarWars => console.log(objectStarWars.result));

//     Create an async function, that will await for the above GET request.
//     The program shouldn’t contain any then() method.
//     Make sure to check the status of the Response and to catch any occuring errors.



const starfunc = async () => {
    let response = await  fetch("https://www.swapi.tech/api/starships/9/");
    let json = await response.json()
    return json
}
const logstar = async () =>{
    let response = await starfunc()
    console.log(response)
}

logstar()


// 🌟 Exercise 4: Analyze
// Instructions

// Analyse the code provided below - what will be the outcome?

// function resolveAfter2Seconds() {
//     return new Promise(resolve => {
//         setTimeout(() => {
//             resolve('resolved');
//         }, 2000);
//     });
// }

// async function asyncCall() {
//     console.log('calling');
//     let result = await resolveAfter2Seconds();
//     console.log(result);
// }

// asyncCall(); // the programa will log calling, then it will resolve the promise, this is because the await in a async is waiting for the promise that the function returns to resolve and then continures.
