comp426-2022-fall / a02

Create a command line Node.js package that ingests API data from Open-Meteo
GNU Affero General Public License v3.0
0 stars 0 forks source link

Clarification Needed: Required Weather Variables and URL Placement in Code #1

Closed bdhack23 closed 1 year ago

bdhack23 commented 2 years ago

I'm having trouble understanding which weather variables we need to select for the request URL. Are they the ones listed in parsed form in the instructions, or are they different? Also, I'm confused as to where we put our URL in our code so it knows what to do with the information.

vennila-t commented 2 years ago

I would need to confirm this with @jdmar3, but I believe the request URL will be in your cli.js file, after the help text and the timezone. And unless otherwise stated, I would just use the similar variables as the example.

jdmar3 commented 2 years ago

I updated the session notes from when we worked with fetch() in class. This should help you get a sense of where the URL goes and how you can manipulate parts of it.

https://github.com/comp426-2022-fall/schedule/blob/main/07-manipulate-data.md

As for the variables, look at the example in galo.sh where the following block lives:

# Get one week of rain data from open-meteo.com
getdata () {
  curl -s -G \
    -d "latitude=${LAT}" \
    -d "longitude=${LONG}" \
    -d "daily=weathercode,temperature_2m_max,temperature_2m_min,sunrise,sunset,precipitation_sum,precipitation_hours,windspeed_10m_max,windgusts_10m_max,winddirection_10m_dominant" \
    -d "current_weather=true&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch" \
    -d "timezone=${TZ}" \
    https://api.open-meteo.com/v1/forecast
}

Each -d above is a block of URL encoded data that is being fed to the API by Curl in the Bash example. Curl concatenates them. There is a way to do this with fetch in JS as well, but you can also just concatenate the URL together and feed it to fetch as a string.

So, for example:

const tz = "America/New_York"

const latitude = "35.90"

const longitude = "-79.05"

const base_url = ''https://api.open-meteo.com/v1/forecast"

const data_string = "latitude=" + latitude + "&longitude=" + longitude +"&daily=weathercode,temperature_2m_max,temperature_2m_min,sunrise,sunset,precipitation_sum,precipitation_hours,windspeed_10m_max,windgusts_10m_max,winddirection_10m_dominant&current_weather=true&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch&timezone=" + tz

const url = base_url + "?" + data_string

const response = await fetch( url )

const data = await response.json()

console.log( url )

console.log( data )

Note that I added "?" inside the url constant that goes inside fetch(). This is because for URL-encoded data, the question mark is what tells the endpoint that the data is about to begin.

And also in between each key=value pair in the data string, we have to put an ampersand & to add a subsequent key to pass the API.

SO, start with the URL from Open-Meteo URL builder and work backward.

Put that URL inside fetch() and see if it works. Once it does, slice it up and include your variables.

Use console.log() to your advantage to see what strings the variables are actually storing like in the example above.

In the above example, the last two lines should echo the full URL for the API that you are making the call against, and then the data that returns. You should be able to run the entire example above and use it to make sense of what needs to go where and what the URL coming from the Open-Meteo URL builder should look like.