{"size": 1630, "ext": "js", "max_stars_repo_path": "mer-b/back/nominatism.js", "max_stars_repo_name": "Tuddual/Hyblab2021", "max_stars_repo_stars_event_min_datetime": "2021-01-26T08:09:36.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-29T13:53:05.000Z", "max_issues_repo_path": "mer-b/back/nominatism.js", "max_issues_repo_name": "Drarig29/Hyblab2021", "max_issues_repo_issues_event_min_datetime": "2021-01-16T11:26:02.000Z", "max_issues_repo_issues_event_max_datetime": "2021-02-08T18:29:49.000Z", "max_forks_repo_path": "mer-b/back/nominatism.js", "max_forks_repo_name": "Tuddual/Hyblab2021", "max_forks_repo_forks_event_min_datetime": "2021-01-08T16:56:04.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-27T09:57:54.000Z"}
{"max_stars_count": 6, "max_issues_count": 21, "max_forks_count": 15, "avg_line_length": 33.9583333333, "max_line_length": 370, "alphanum_fraction": 0.6042944785}
'user strict';
const fetch = require('node-fetch');
const convert = require("xml-js");
const cst = require("./constants.json");

exports.api_fetch = async (plages) => {

    let data = [];

    for (const node of plages) {

        try {
            var res = await fetch(cst.nominatim.api_url + `lat=${node.latitude}&lon=${node.longitude}`);
        } catch (e) {
            res = {
                ok: false,
                status: e.code,
                msg: e.message
            }
        }

        if (!res.ok) {
            return error.e(res.status, (res.msg || `An error has occured when fetching on the nominatim api.`));
        }

        const xml = await res.text();
        data.push(convert.xml2js(xml, {compact: true, spaces: 4}));
    }
    return {
        ok: true,
        data: data
    }
}

exports.format = (plages, adress) => {

    for (let i =0; i<plages.length; i++) {

        plages[i].adresse = {
            rue: (adress[i].reversegeocode.addressparts.hasOwnProperty("road")? adress[i].reversegeocode.addressparts.road._text : ""),
            code_postale: (adress[i].reversegeocode.addressparts.hasOwnProperty("postcode")? adress[i].reversegeocode.addressparts.postcode._text : ""),
            commune: (adress[i].reversegeocode.addressparts.hasOwnProperty("city")? adress[i].reversegeocode.addressparts.city._text : adress[i].reversegeocode.addressparts.hasOwnProperty("town")? adress[i].reversegeocode.addressparts.town._text : adress[i].reversegeocode.addressparts.hasOwnProperty("village")? adress[i].reversegeocode.addressparts.village._text : "")
        };
    }

    return plages
}
