Last active
July 28, 2020 05:57
-
-
Save jinwook-k/8e67976b3bc8f5eb9720a2bea42eead8 to your computer and use it in GitHub Desktop.
server/api/weather.js
This file contains hidden or 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
const axios = require("axios"); | |
// Configuring the path to read the environment variable file, .env, to get the weather api key | |
require('dotenv').config({path: "./../../../.env"}); | |
const baseUrl = "http://api.openweathermap.org/data/2.5/weather"; | |
class Weather { | |
/** | |
* Gets the weather data based on the zipcode and which temp system to converge to (imperial/metric system) | |
* | |
* @param {number} zipCode The zipcode used to get the weather info from the weather api | |
* @param {string} tempMetric This is either "imperial" (use Fahrenheit) or "metric" (use Celsius) | |
* @return {JSON} The data response from the weather api call. | |
*/ | |
getWeatherData = async (zipCode, tempMetric) => { | |
/** | |
* Use get api for "By ZIP code" (https://openweathermap.org/current#zip) | |
* - The "us" query stands for "United States | |
* - "process.env.WEATHER_KEY" is the api key that we get from the .env file | |
* - "units" query can be either imperial (Fahrenheit) or metric (Celsius) | |
*/ | |
let url = `${baseUrl}?zip=${zipCode},us&appid=${process.env.WEATHER_KEY}&units=${tempMetric}`; | |
// Awaitable call to get the information from the weather api and then return the data. | |
// TODO: Add error handling for this call | |
return (await axios(url)).data; | |
} | |
} | |
module.exports = Weather; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment