Unverified Commit a370eccb authored by SirSoliloque's avatar SirSoliloque Committed by GitHub
Browse files

Merge pull request #7 from SirSoliloque/Weather

Weather
parents 2eca96bc 7e8511c4
Loading
Loading
Loading
Loading

js/Token.js

0 → 100644
+1 −0
Original line number Diff line number Diff line
const Token = "309012362b9634f89946d5050b8a8c1c5568bc8e441088815b3203153fc3bc67"
 No newline at end of file

js/WeatherCard.js

0 → 100644
+96 −0
Original line number Diff line number Diff line
// Description: Classe WeatherCard
class WeatherCard{
    static json;
    static city;
    static forecast;
    constructor(CodeInsee) {
        this.CodeInsee = CodeInsee;
        this.json = null;
        this.city = null;
        this.forecast = null;
    }

    async fetchData() {
        try {
            // Utilisez "await" pour attendre la réponse de la requête
            const response = await fetch(
                `https://api.meteo-concept.com/api/forecast/daily/0?token=${Token}&insee=${this.CodeInsee}`
            );
            if (!response.ok) {
                throw new Error(`Erreur de requête: ${response.status}`);
            }

            // Parsez la réponse en JSON
            const data = await response.json();
            this.json = data;
            this.city = data.city;
            this.forecast = data.forecast;

            console.log(this.json);
            console.log(this.city);
            console.log(this.forecast);
            console.log(this.city.name);
        } catch (error) {
            console.error("Erreur lors de la récupération des données météo:", error);
        }
    }
    getjson(){
        return this.json;
    }
    Ville(){
        return this.city["name"];
    }
    latitudeVille(){
        return this.city.latitude;
    }
    longitudeVille(){
        return this.city.longitude;
    }
    TempMin(){
        return this.forecast.tmin;
    }
    TempMax(){
        return this.forecast.tmax;
    }
    Sunhour(){
        return this.forecast.sun_hours;
    }

    determineWeather(){
        let weather=this.forecast.weather;
        if (weather<0){
            return Error;
        }
        if (weather<=2){
            return "soleil";
        }
        if (weather<=5){
            return "nuage";
        }
        if (weather<=7){
            return "brume";
        }
        if (weather<=16){
            return "pluie";
        }
        if (weather<=32){
            return "neige";
        }
        if (weather<=48){
            return "pluie";
        }
        if (weather<=78){
            return "neige";
        }
        if (weather<=140){
            return "orage";
        }
        if (weather<=78){
            return "vent";
        }
        
        if (weather==235){
            return "grele";
        }
    }
}
 No newline at end of file

js/fetchUtility.js

0 → 100644
+48 −0
Original line number Diff line number Diff line
/**
 * Parses the JSON returned by a network request
 *
 * @param  {object} response A response from a network request
 *
 * @return {object}          The parsed JSON from the request
 */
function parseJSON(response) {
    return response.json();
}

/**
 * Checks if a network request came back fine, and throws an error if not
 *
 * @param  {object} response   A response from a network request
 *
 * @return {object|undefined} Returns either the response, or throws an error
 */
function checkStatus(response) {
    if (response.ok) {
        return response;
    }

    const error = new Error(response.statusText);
    error.response = response;
    throw error;
}



/**
 * Requests a URL, returning a promise
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 *
 * @return {object}           The response data
 */
function request(url, options) {
    return fetch(url, options)
        .then(checkStatus)
        .then(parseJSON)
        .catch((error) => {
            console.log(error);
        })
        
}