(cityCode, days)
| 133 | * @returns {object} |
| 134 | */ |
| 135 | export const getPredictionWeather = async (cityCode, days) => { |
| 136 | const baseUrl = `${CPTEC_URL}/cidade/`; |
| 137 | let url = baseUrl; |
| 138 | if (days <= 4) { |
| 139 | url += `${cityCode}/previsao.xml`; |
| 140 | } else { |
| 141 | url += `7dias/${cityCode}/previsao.xml`; |
| 142 | } |
| 143 | |
| 144 | const weatherPredictions = await axios.get(url, { |
| 145 | responseType: 'application/xml', |
| 146 | responseEncoding: 'binary', |
| 147 | }); |
| 148 | |
| 149 | const parsed = parser.parse(weatherPredictions.data); |
| 150 | |
| 151 | if (parsed.cidade) { |
| 152 | const jsonData = formatPrediction(parsed); |
| 153 | if (jsonData.cidade === 'null') { |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | // If number of days requested was greater than 7, load extended data from service |
| 158 | // Disabled by inconsistences on CPTEC endpoints |
| 159 | /* if (days > 7) { |
| 160 | const extendedPredictions = await axios.get( |
| 161 | `${baseUrl + cityCode}/estendida.xml`, |
| 162 | { |
| 163 | responseType: 'application/xml', |
| 164 | responseEncoding: 'binary', |
| 165 | } |
| 166 | ); |
| 167 | |
| 168 | const extendedJsonData = parser.parse(extendedPredictions.data); |
| 169 | if (extendedJsonData.cidade) { |
| 170 | jsonData.clima = [ |
| 171 | ...jsonData.clima, |
| 172 | ...formatPrediction(extendedJsonData).clima, |
| 173 | ]; |
| 174 | } |
| 175 | } */ |
| 176 | |
| 177 | // IF total data greater than requested number of days, slice array into correct size |
| 178 | if (jsonData.clima.length > days) { |
| 179 | jsonData.clima = jsonData.clima.slice(0, days); |
| 180 | } |
| 181 | |
| 182 | return jsonData; |
| 183 | } |
| 184 | return []; |
| 185 | }; |
no test coverage detected