Last active
March 10, 2018 03:10
-
-
Save aershov24/e552937064ceb47e0767045c9e5cb492 to your computer and use it in GitHub Desktop.
NodeJS Client module for OpenUV - Global Real-Time UV Index API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
NodeJS Client module for OpenUV - Global Real-Time UV Index API | |
Installation: | |
npm install --save request | |
npm install --save query-string | |
Usage: | |
const openuv = require('./openuv.js')('v1', 'YOUR_API_KEY'); | |
var params = { | |
lat: -31.67, | |
lng: 115.345 | |
}; | |
openuv.getUVIndex(params, (err, result) => { | |
if (err) { console.log(err); } | |
else { console.log(result.uv); } | |
}); | |
*/ | |
const request = require('request'); | |
const queryString = require('query-string'); | |
module.exports = function (version, apikey) { | |
var openuv = {}; | |
var endpoint = 'https://api.openuv.io/api/'+version; | |
var options = { | |
headers: { | |
'x-access-token': apikey | |
} | |
}; | |
openuv.getUVIndex = function (params, cb) { | |
options.url = endpoint+'/uv?'+queryString.stringify(params); | |
console.log(options); | |
request(options, (error, response, body) => { | |
if (!error && response.statusCode == 200) { | |
cb(null, JSON.parse(body).result); | |
} | |
else { | |
cb(JSON.parse(body).error, null); | |
} | |
}) | |
}; | |
openuv.getForecast = function (params) { | |
options.url = endpoint+'/forecast?'+queryString.stringify(params); | |
request(options, (error, response, body) => { | |
if (!error && response.statusCode == 200) { | |
cb(null, JSON.parse(body).result); | |
} | |
else { | |
cb(JSON.parse(body).error, null); | |
} | |
}) | |
}; | |
openuv.getProtection = function (params) { | |
options.url = endpoint+'/protection?'+queryString.stringify(params); | |
request(options, (error, response, body) => { | |
if (!error && response.statusCode == 200) { | |
cb(null, JSON.parse(body).result); | |
} | |
else { | |
cb(JSON.parse(body).error, null); | |
} | |
}) | |
}; | |
return openuv; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment