Hello! I am having a hard time writing the code for the analysis in extracting the API information from the weather app. I know I will need to store the data in the bucket, and have the API Key for the weather data, but knowing the parameters of which type of SDK I would need is confusing.
Did you have any solution?
I did exactly this on a customer.
My analysis look like this:
const { Analysis, Device, Utils } = require('@tago-io/sdk'); const axios = require('axios'); async function ImportDayForecast(context) { const envVars = Utils.envToJson(context.environment); if (!envVars.device_token) return context.log('Missing device_token environment variable'); const device = new Device({ token: envVars.device_token }); const options = { url: 'Put your API URL here', method: 'GET', }; try { const result = await axios(options); // Some validations, this need to be done by you if (!result.data.hasOwnProperty('data')) return console.log(`No property "data" in API response.`); if (result.data.data.length === 0) return console.log(`Property "data" empty in API response.`); // This code below delete old data, because I dont want save history, if you want to save history, dont use this code await device.deleteData({ variable: 'day_forecast', qty: 10000, }); // Here I customize the response to my application // I omit some irrelevant code in [...] const forecasts = result.data.data.reverse().map((item) => { const forecast = item; [...] return forecast; }); let count = 0; // Here I save new data. // I using metadata to save some Data storage, if you dont use this technique, just save in normal way // I omit some irrelevant code [...] for await (const forecast of forecasts) { try { await device.sendData({ variable: 'day_forecast', value: '', metadata: { temperature: `Min: ${forecast.temperature.min}°C - Max: ${forecast.temperature.max}°C`, humidity: `Min: ${forecast.humidity.min}% - Max: ${forecast.humidity.max}%`, [...] }, }); count += 1; } catch (error) { context.log(`Error when inserting: (${forecast.date_br})`, error); } } context.log(`Successfully Inserted ${count} rows.`); } catch (error) { return context.log(`Error: ${error}`); } } module.exports = new Analysis(ImportDayForecast);
I comment the file to explain.
With the data saved in a bucket, you can do anything in dashboards.
Hope this help you.