1
0

Video 55: Building a JSON HTTP Endpoint

This commit is contained in:
Jason Williams
2019-10-10 11:43:47 -06:00
parent a34f5e05c3
commit 040916eaa2
5 changed files with 368 additions and 5 deletions

View File

@@ -0,0 +1,17 @@
const request = require('request')
const forecast = (latitude, longitude, callback) => {
const url = 'https://api.darksky.net/forecast/0466bccb953c0b9daeb091a529da2c0d/' + latitude + ',' + longitude + '?units=si'
request({ url, json: true }, (error, {body}) => {
if (error) {
callback('Unable to connect to weather service!', undefined)
} else if (body.error) {
callback('Unable to find location', undefined)
} else {
callback(undefined, body.daily.data[0].summary + ' It is currently ' + body.currently.temperature + ' degrees out. There is a ' + body.currently.precipProbability + '% chance of rain.')
}
})
}
module.exports = forecast

View File

@@ -0,0 +1,21 @@
const request = require('request')
const geocode = (address, callback) => {
const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/' + encodeURIComponent(address) + '.json?access_token=pk.eyJ1IjoiamF5d2xsIiwiYSI6ImYyZjkyMGNlOTgyNGQ5ZWM5ZDExN2FjNGYwMDZmNTQ4In0.y3lv5ddmGhVNSL7wfMpB0g&limit=1'
request({ url, json: true }, (error, {body}) => {
if (error) {
callback('Unable to connect to location services!', undefined)
} else if (body.features.length === 0) {
callback('Unable to find location. Try another search.', undefined)
} else {
callback(undefined, {
latitude: body.features[0].center[1],
longitude: body.features[0].center[0],
location: body.features[0].place_name
})
}
})
}
module.exports = geocode