In this video we're going to continue to explore our request.
We're going to learn how a request can automatically pass the Jason for us.
We're also going to figure out how we can actually print a forecast using the response data.
And finally we're going to explore some options for the dark sky API allowing us to do things like change
the language from something like English to French and change the units from something like Imperial
over to as I say you could get the temperature back in Celsius as opposed to Fahrenheit.
Let's get started by having the request module automatically pass the Jason response for us.
We can do this by customizing the options object and there is a list of all available options over in
the documentation.
So if I head back over to the request module page and scroll down to the table of contents way near
the bottom we have all available options.
If we click this this is going to list out all of the options we can use.
Now up top we already have the you are Al option which we've already used.
If we scroll down the list a little ways we're going to see a Jason option.
Jason in lower case can get set to equal True or false in our case we're going to set Jason to true
which is going to have the request module automatically pass.
Jason responses for us.
So over inside of our code what do we need to do.
We're going to add a second property onto this object.
So right here alongside of you are L we'll set up Jason in lower case.
This is important.
If you use uppercase it's not gonna work.
Then we're going to set it equal to the Boolean true saying we would like a request to pass this as
Jason.
Now from here that means response.
That body is already going to be an object.
There is no need to actually pass it.
So let's go ahead and change what we're doing inside of our function.
Right here I am going to use console dialog to print the same stuff using our new setup.
That would be first up response response dot body still exists though since we've set Jason to true
is no longer a string.
It is now the past object from there we're going to access currently which is what we were already accessing
and dumping.
That's what we're seeing down below.
This is Andrew with another interruption as I mentioned in the last lesson of the current weather data
for Dark Sky was on currently.
But the current weather data for weather stack is on current.
So if you're using the weather stack API you would want to access response dot body dot current as shown
below.
Now we can save app dot and J.
S I can rerun the program and what do we get.
We get the exact same response.
I have my object showing up with my current information it's still clear and the temperature has increased
a little bit to fifty seven point five five degrees.
So now that request is passing the Jason for us we don't have to manually add that code every single
time we're trying to make an H TTP request from node j as a firm here.
I want to focus on printing a forecast to the user.
Now that means we have to figure out what data to include and what to leave off and that can be tricky
to do when you're looking at a dump of your data from the terminal.
I find it much easier to use the browser to explore what data I want to include from a Jason response.
Now currently the information we're seeing is very hard to pass since it is just a Jason string.
There are chrome extensions out there though that allow you to see nice past formatted data.
There are extensions for other browsers as well like Chrome Firefox and Internet Explorer.
So what I'm going to do is search chrome extension and the one we're going to use is called Jason for
matter though there are literally dozens of different options out there.
Right here I have the chrome extension and all I'm going to do is install it.
I'll take a quick moment to add it to Chrome and once we have it installed successfully we can go ahead
and move back to that page refresh things.
And this time the chrome extension is going to notice that Jason has been returned and it's going to
pass it for us.
Now over here with this particular extension I can also view the raw data should I want to.
But the whole goal is to view the past data.
Now we can collapse the various properties easily navigating what we have here.
So we have this root object.
It starts up here on line 1 and it gets closed way way down below at the very end of this object.
So way down here on the last line everything inside of there is part of that object.
Now this object is accessible to us as body.
So an IMAX guessing response dot body I am accessing this object from there I can grab any of its properties
like currently.
Which is the fourth one listed that's why response dot body dot currently works.
I could also use response dot body dot time zone as shown here to get the time zone for the current
location.
So now we have a slightly better idea as to how we're accessing this information below.
Currently we have a minute early so we can get a minute by minute forecast.
If I was to collapse that we have hourly below that we have daily and below that we have some flags
and we also have the offset which are not particularly useful for our purposes.
Now what are we trying to do here.
Well currently we're just going to use the currently property that we do have other forecast information
we could use to get even more data which we'll explore a little later.
So for now we're just going to focus on this object and all of the properties are values we can use.
Let's go ahead and talk about exactly what type of forecast we'd like to print down below.
I have some challenged comments.
It's going to be up to you to use the information on currently to print a little message using that
actual information.
So in general the goal is to print something like this.
It is currently fifty eight point five five degrees out there is a zero percent chance of rain.
Now this piece of information the current temperature and this piece of information the chance of precipitation.
They're both available on that currently object.
I have a temperature property.
This is where I get the temperature from and I also have properties related to precipitation.
Right here I have pre sip probability currently at zero.
This is Andrew from the future with another quick interruption.
So with the weather stock API we have access to a different set of weather data.
So let's talk about what's going to need to change and we'll get started by exploring the root properties
we have on our Jason response.
So right here there are three.
We get started with the request property which contains information about the request that weather stack
processed.
We have the latitude and longitude.
We also have the language and the units.
We're not going to use any of that data.
So let's collapse it.
Next up we have information about the location that we're viewing the weather data for.
Right here.
I can see this is for North Beach California.
Let's collapse location as well.
That leaves the last property of the third one which is current current contains the current weather
data.
And this is the property that you'll need to work with for the challenge.
Now if we head over to visual studio code I've made some changes to what you're supposed to do.
So this right here should be exactly what you have.
I have my weather stack U.R.L. setup and I'm accessing response dot body dot current.
All I've done is changed the challenge comments down below.
So what you'll end up doing is printing something like the following it is currently 9 degrees out.
It feels like five degrees out.
So here we are using the temperature and the apparent temperature the feels like temperature for when
we account for things like wind and humidity.
So you can access both of the temperature and the feels like temperature on that current property.
So right over here we have current you can access the temperature on temperature right here and you
can access the apparent temperature on fields like which we have down below.
So for this forecast I can see the temperature is 10.
Well the feels like is eight.
So there must be some windchill factoring into the difference.
So this is what you'll end up working with you'll end up printing something like this using the weather
stack API.
OK that is it for the update.
Let's jump back into the original lesson so using these two pieces of information we want to generate
the following forecast.
That means you're going to comment out this line or remove it and add another council dot log call in
to print your forecast.
From there you're going to test your work.
So you want to run the script and make sure you're seeing your string with the correct values added
inside.
Take as much time as you need to generate this string using those two pieces of dynamic information.
And when you're done come back and click play and we'll work through it together
how that go.
Let's get things started by working with our message.
So I'm going to use console dot log to print something and that something is the string right here with
those two dynamic values included.
So we start off with some static text I'll create that in a string.
It is currently followed by a space.
Then we're going to concatenate on the actual temperature.
So like we did before.
That's going to be response dot body dot currently.
That's going to bring us to this object right here.
And on that object we have the temperature property which is the one we're looking for.
So dot temperature excellent.
Now after the temperature we go back to some static text so I'll concatenate on another string and we
will start with a space the space between the temperature and the D in degrees.
Now I'll start typing out degrees out period followed by the start of the next sentence.
There is a space and in here we have to insert the.
Percent chance of rain.
So once again I'm going to concatenate on a value that will be a response dot body dot currently dot.
And right here we are looking for preset probability.
So precision using these shorthand for the word probability.
And finally we can concatenate on the closing static text so after our probability it's percent chance
of rain.
So right here a percent chance of rain.
Excellent.
Now we have this really long string created and we can go ahead and actually test our work I'm back
with another quick interruption.
If you're working with the weather stack API let's go over the console log call for that.
So we start off with it's currently the string followed by the current temperature right here.
You should have access that firm response dot body dot current dot temperature after the current temperature.
We move on to more static text right here.
I have degrees out which finishes up the first sentence from there we start our second sentence with
it feels like and then we get the apparent temperature on a response dotted body dot current dot feels
like from there we wrap up the second sentence with degrees out in eight period.
So this is the solution you should have come up with.
If you were working with the weather stack API now we can test our work down below by just rerunning
app dot J.S. so I'll use node space app dot J.S. and if I run things I would expect to get the correct
result right here.
I get it is currently 10 degrees out.
It feels like seven degrees out which is fantastic.
With that change made let's head back into the regular video so I'm going to rerun the program from
the terminal down below to start I'll use clear to clear the current output then node space act J asked
to rerun the program and what do I get I get it is currently fifty eight point two two degrees out there
is a zero percent chance of rain which is fantastic we were able to make an H TTP request for data parts
that data into a format that was usable and actually use it inside of our node J.S. application in a
meaningful way or this update what we'll end up doing is figuring out how we can change the units for
the data that comes back.
So right now we're seeing the temperature in Celsius but if we want to we could request that temperature
from weather stack in Kelvin or Fahrenheit.
Now in the original lesson what we did is we explored the dark sky API to figure out how to get that
done.
Then we changed our U.R.L. and we watched the data change in the response we'll end up doing that exact
same thing for weather stack.
So first up let's check out the documentation so we can figure out how to get this done.
I'll switch over to the browser I'll switch over to the quick start guide we explored earlier.
Then I'll scroll to the top and right here we can head over to whether stacks documentation.
So I'll head over to the documentation page and right here we can learn all the different ways we can
work with the weather stack API.
Now what we're looking for can be found in the left hand side bar so down below we have getting started.
We have API features and we have options under options.
We are looking for the units parameter.
So for our particular U.R.L. you'll remember that we set up our API key and we also set up our query
what we'll do here is set up something else.
We will set up units so I'll click on units parameter that will bring me down to the documentation.
And right here I can see that I can set units equal to one of the following values if I set units equal
to m.
I'll see my data using the metric system.
Now this is the default option.
So we're already seeing our temperature in Celsius.
If I switch the value over to S for scientific I would see my temperature in Kelvin.
And finally if I switched units over to F for Fahrenheit I could view the temperature in Fahrenheit.
So right here let's explore how we can get that done.
And since I live in the States I'll be switching things over to Fahrenheit so I'll head back over to
visual studio code and what we'll end up doing is adding another query parameter.
So right here I have my first one access key.
If I scroll over I have my second one query will add a third one so right at the end I'll use an ampersand.
Then we can set up a another key value pair.
So right here I'll go ahead and use the key of units followed by the value.
So units equals right here.
I'll use f once again you can use m intensity the temperature in Celsius s to see it in Kelvin or F
to see it in Fahrenheit.
So with units set up let's say you've asked J.S. down below.
All I'm going to do is rerun the program so I'll run apt J ask through node once again and if I do right
here I can see my data looks pretty different.
Right here I am seeing my temperatures and Fahrenheit.
It's currently 45 degrees and it feels like 41.
So now we know how we can customize the units that are coming back from our API.
Now at this point in the original lesson we had added just one additional piece of information to our
weather forecast and that was it for the lesson.
So we added that additional piece of information as the very first thing we render.
So to explore what will end up adding let's head back over to our API request in the browser so I'll
switch back over to the browser.
I'll head over to our API response and right here what we'll end up doing is starting things off with
a general description of the weather.
Now we could collapse at the request property.
We could also collapse the location property right here that just leaves us with current and what will
end up doing is printing the weather description.
So right here that would be overcast and that will show up first.
Now it will always be overcast sometimes it will be sunny or partly cloudy whatever it is.
That's what we'd like to show up so we can access that data on current.
Then from there we would access it on the weather descriptions array.
What we'll do is pull in the first weather description and render that as part of our forecast.
We can get that done by heading over to app J.S. and adding that as the very first thing in console
dialogue.
So right here that would be response dot body from here we will pull something off of the current property
right here as we just explored.
That was Dot whether underscore descriptions.
Now remember that is an array of strings.
So I'll use bracket notation to grab the first item by its index.
That would be an index of 0.
Next up right after that what I'll do is use the plus operator to concatenate the rest of my string
onto the end.
Now in this case if we used the current data what we would see right here is the word overcast followed
directly by it.
There wouldn't even be a space in between what I'll end up doing is starting this string off with a
period end of space.
So this data looks like it's in its own sentence now that we have that in place.
We can save things and test our work now in the original video.
We added something similar though it came from a different property.
So when you do watch the original video you'll see something different right here.
But this is what we need for the weather stack API.
So down below let's just rerun our program once again.
And if I do right here we can see exactly what we had before.
Now it looks like in the last couple of minutes the weather did indeed change.
So right here I see partly cloudy followed by It is currently 45 degrees out.
It feels like 41 degrees out.
So this is awesome.
We now have a summary of the weather.
Then we have the actual temperature followed by the apparent temperature when we factor in humidity
and wind.
So in the original video that's where we had stopped for this one.
What we'll end up doing is moving on to the next lesson where it will be up to you to work with a completely
different API.
Now the good news is that when it comes to these big updates because of the API change we're basically
done.
There's a couple of small updates throughout the next several lessons but there's actually nothing that
needs to change in the next one.
It was really just the last one and this one that required some more substantial changes now that we
have that code in place though we are good to go.
So let's jump right into the next one which is a challenge for you to fetch data from a new API.