So what are the response codes that we can get from an API?
Response codes actually have a very specific meaning,
but the most important thing they tell us is if our request succeeded or
if it failed. Now, if you've been using the internet for a while,
you've probably come across the 404 response code. And the 404
response code basically means that the thing that you're looking for doesn't
exist. And you can see various websites having fun with this
404 page.
Basically when you're trying to retrieve something from a website and it doesn't
actually have the thing you're looking for, then this is the code you get.
But there's actually a lot more response codes other than just 404.
And in fact,
you could summarize these status codes just by looking at the first number.
If it is 100 and something, then it's telling you, hold on,
something's happening. This is not final.
If it's 200 something then it is saying, here you go. Everything was successful.
You should be getting the data you expected.
If it's 300 something then it usually means you don't actually have permission
to get this thing, so go away. 400 something, it means that
you screwed up. Either it could be a 404
which means that you screwed up
and the thing you're looking for doesn't even exist,
or it could be 500 something, which means I,
as in the server that you are making the request to, screwed up. And maybe the
server is down, maybe the website is down or maybe there's some other issue.
So that's just a quick,
at a glance, sort of way of remembering what's actually happening
when you are getting these response codes.
Now you can actually dig deeper into this response object and you can get a
whole bunch of things from it. For example,
if you wanted to know the actual status code instead of just a response object,
then you can say response.status_code,
and you actually only get the status code. But notice how
if I change this URL and I make a typo in here,
instead of iss I have is,
then now I get a 404 as my response status code.
OK. So if we get the status code 200 then it means that we were successful.
But what do we do when we get a different status code?
What should our program do when the request fails?
We might be tempted to write code like this. By checking to see if the response
.status_code is not equal to 200,
we can actually figure out well, this means that there probably was an error.
Now it doesn't really make sense to just print out an error because the user who's
using our program might be expecting something to happen.
So in most, cases you might want to raise an exception instead.
You could say bad response from ISS API.
But we learned from our module on errors and exceptions,
this is not very good. Firstly, this is not a very specific exception.
It's going to raise a very general type of exception. And secondly,
all that we're catching is when the status code is not 200.
What if we wanted you to know specifically which status code we got back? Well,
we could, of course check well
if the response status code was equal to say
404, then we'll raise a particular type of exception that says that resource
does not exist. And then we can have,
um, an elif that says, well,
if the response status code was 401,
then that means that you should raise an exception because this particular code
only happens when you are not authorized to access this data.
Now there's a lot of status codes that you could be getting back.
And in the course
resources I'll link to this particular web page where it tells you all of the
possible codes that you could be getting and what it means. As you can imagine,
it's impossible for us to write this many
if statements checking for every single possibility. What do we do instead?
You can actually do away with all of this by getting the request module
to generate the exception instead.
The requests module is the most popular way for Python developers to work with
APIs. And you can see in their statistics just the number of stars,
number of forks,
and from these stats, you can see just how much active development there is and
how popular this particular module is. Now,
if you want to take a look at the documentation,
then you get taken to their website and it tells you all of the things that it
can do, which is quite a lot actually.
Now I won't read through all of this with you,
but if we take a look at one of the sections, Errors and Exceptions,
you can actually call raise for status on the response you get back in order
to raise a HTTP error
if the request returned an unsuccessful status code.
Basically instead of us trying to raise an exception for every single possible
status code and telling the developer what might be the reason, we can simply use
the request module by saying response.raise_for_status.
So now what you'll see is that if we don't get a 200
which is the pass-- Let's say I mess up this end point again,
then you'll see an exception being raised and it's telling us, look,
it's a 404 client error. This URL is not found.
So this request module is actually really powerful and we're going to see how to use
various aspects of it in the upcoming lessons.
But for now, the first thing we need to get hold of is the actual data from this
particular API. To do that,
we tap into our response and we get the JSON data.
So this is the actual data. And when I print it,
you can see that it looks exactly the same as what we got in our browser.
Coming back to our code where we have this JSON,
we can tap into it just like we would for any Python dictionary.
We could use some square brackets and then type in the name of a key that we're
interested in. For example,
if this was a Python dictionary and we wanted to know the iss_position,
then we would add the square brackets and then pass in iss_ position.
And now when we print data,
you can see it's now narrowed down and drilled into the position to give us this
longitude and latitude.
So we can go even further and tag on another set of square brackets and try to
get hold of, say the longitude.
And now we get that actual piece of data. There you have it.
We get access to the longitude by tapping into our data in the format of a JSON
and then getting the iss_position. And inside that dictionary,
we can get hold of the longitude. And we can do the same thing in order to get
hold of the latitude,
1: right?
0: But we would change the key of course, to latitude.
So now I'm going to create a tuple with the longitude and the latitude.
And now if I print it out,
you can see how I've managed to nicely format my code and get the pieces of
data that I want in order to be able to work with it in my project. Now,
if you wanna see where that particular latitude and longitude is on the world
map, then we can use a tool called latlong.net.
So if you go to geographic tools and go to latitude and longitude to address,
then you can paste in the longitude and latitude values that we got from
our API. And when we click convert, then you'll see on the map,
it shows where in the world the ISS is right now.
So at the moment, it's somewhere to the East of Japan over the Pacific ocean.
And of course, it's telling you no address found because I don't think anybody
lives right there.
So have a play around with that and make sure that you've fully understood
what's happening in these few lines of code and how we use the API endpoint to
get the data that we're showing up right here. In the next lesson
I've got a challenge for you.