WEBVTT

0
00:00.900 --> 00:04.350
So what are the response codes that we can get from an API?

1
00:04.890 --> 00:08.220
Response codes actually have a very specific meaning,

2
00:08.400 --> 00:12.290
but the most important thing they tell us is if our request succeeded or

3
00:12.590 --> 00:16.010
if it failed. Now, if you've been using the internet for a while,

4
00:16.010 --> 00:20.630
you've probably come across the 404 response code. And the 404

5
00:20.630 --> 00:24.230
response code basically means that the thing that you're looking for doesn't

6
00:24.230 --> 00:28.880
exist. And you can see various websites having fun with this 

7
00:28.880 --> 00:29.713
404 page.

8
00:29.960 --> 00:33.530
Basically when you're trying to retrieve something from a website and it doesn't

9
00:33.530 --> 00:36.650
actually have the thing you're looking for, then this is the code you get.

10
00:37.100 --> 00:41.150
But there's actually a lot more response codes other than just 404.

11
00:41.690 --> 00:42.650
And in fact,

12
00:42.680 --> 00:46.700
you could summarize these status codes just by looking at the first number.

13
00:47.240 --> 00:51.620
If it is 100 and something, then it's telling you, hold on,

14
00:51.800 --> 00:54.050
something's happening. This is not final.

15
00:54.410 --> 00:58.790
If it's 200 something then it is saying, here you go. Everything was successful.

16
00:58.940 --> 01:00.980
You should be getting the data you expected.

17
01:01.940 --> 01:06.800
If it's 300 something then it usually means you don't actually have permission

18
01:06.800 --> 01:11.600
to get this thing, so go away. 400 something, it means that 

19
01:11.630 --> 01:15.020
you screwed up. Either it could be a 404

20
01:15.380 --> 01:16.910
which means that you screwed up

21
01:16.970 --> 01:19.130
and the thing you're looking for doesn't even exist,

22
01:19.610 --> 01:22.700
or it could be 500 something, which means I,

23
01:22.760 --> 01:27.760
as in the server that you are making the request to, screwed up. And maybe the

24
01:28.370 --> 01:32.720
server is down, maybe the website is down or maybe there's some other issue.

25
01:33.620 --> 01:34.940
So that's just a quick,

26
01:34.970 --> 01:38.870
at a glance, sort of way of remembering what's actually happening

27
01:38.870 --> 01:40.970
when you are getting these response codes.

28
01:42.110 --> 01:46.640
Now you can actually dig deeper into this response object and you can get a

29
01:46.640 --> 01:49.310
whole bunch of things from it. For example,

30
01:49.310 --> 01:54.020
if you wanted to know the actual status code instead of just a response object,

31
01:54.350 --> 01:57.230
then you can say response.status_code,

32
01:57.560 --> 02:01.040
and you actually only get the status code. But notice how

33
02:01.040 --> 02:04.910
if I change this URL and I make a typo in here,

34
02:05.240 --> 02:07.220
instead of iss I have is,

35
02:07.580 --> 02:11.600
then now I get a 404 as my response status code.

36
02:12.860 --> 02:17.300
OK. So if we get the status code 200 then it means that we were successful.

37
02:17.780 --> 02:20.150
But what do we do when we get a different status code?

38
02:20.420 --> 02:23.150
What should our program do when the request fails?

39
02:23.690 --> 02:28.690
We might be tempted to write code like this. By checking to see if the response

40
02:30.170 --> 02:33.620
.status_code is not equal to 200,

41
02:33.920 --> 02:38.570
we can actually figure out well, this means that there probably was an error.

42
02:39.260 --> 02:44.260
Now it doesn't really make sense to just print out an error because the user who's

43
02:44.480 --> 02:47.930
using our program might be expecting something to happen.

44
02:48.350 --> 02:52.700
So in most, cases you might want to raise an exception instead.

45
02:52.700 --> 02:57.700
You could say bad response from ISS API.

46
02:58.730 --> 03:02.230
But we learned from our module on errors and exceptions,

47
03:02.560 --> 03:06.730
this is not very good. Firstly, this is not a very specific exception.

48
03:07.090 --> 03:11.230
It's going to raise a very general type of exception. And secondly,

49
03:11.260 --> 03:14.650
all that we're catching is when the status code is not 200.

50
03:14.980 --> 03:19.240
What if we wanted you to know specifically which status code we got back? Well,

51
03:19.240 --> 03:20.830
we could, of course check well

52
03:20.840 --> 03:24.730
if the response status code was equal to say

53
03:24.730 --> 03:29.730
404, then we'll raise a particular type of exception that says that resource

54
03:31.870 --> 03:36.670
does not exist. And then we can have,

55
03:36.700 --> 03:38.830
um, an elif that says, well,

56
03:38.830 --> 03:42.940
if the response status code was 401,

57
03:43.210 --> 03:48.210
then that means that you should raise an exception because this particular code

58
03:50.170 --> 03:55.170
only happens when you are not authorized to access this data.

59
03:58.660 --> 04:02.710
Now there's a lot of status codes that you could be getting back.

60
04:03.040 --> 04:04.060
And in the course

61
04:04.060 --> 04:08.680
resources I'll link to this particular web page where it tells you all of the

62
04:08.680 --> 04:13.680
possible codes that you could be getting and what it means. As you can imagine,

63
04:13.990 --> 04:16.960
it's impossible for us to write this many

64
04:16.960 --> 04:21.880
if statements checking for every single possibility. What do we do instead?

65
04:22.540 --> 04:26.680
You can actually do away with all of this by getting the request module

66
04:26.950 --> 04:29.200
to generate the exception instead.

67
04:29.860 --> 04:34.860
The requests module is the most popular way for Python developers to work with

68
04:37.450 --> 04:42.310
APIs. And you can see in their statistics just the number of stars,

69
04:42.340 --> 04:43.240
number of forks,

70
04:43.870 --> 04:48.190
and from these stats, you can see just how much active development there is and

71
04:48.220 --> 04:51.370
how popular this particular module is. Now,

72
04:51.400 --> 04:53.560
if you want to take a look at the documentation,

73
04:53.830 --> 04:58.240
then you get taken to their website and it tells you all of the things that it

74
04:58.240 --> 05:01.750
can do, which is quite a lot actually.

75
05:02.500 --> 05:04.450
Now I won't read through all of this with you,

76
05:04.720 --> 05:08.170
but if we take a look at one of the sections, Errors and Exceptions,

77
05:08.530 --> 05:13.530
you can actually call raise for status on the response you get back in order

78
05:13.540 --> 05:15.490
to raise a HTTP error

79
05:15.610 --> 05:18.790
if the request returned an unsuccessful status code.

80
05:19.600 --> 05:24.600
Basically instead of us trying to raise an exception for every single possible

81
05:24.670 --> 05:29.670
status code and telling the developer what might be the reason, we can simply use

82
05:30.520 --> 05:35.520
the request module by saying response.raise_for_status.

83
05:36.070 --> 05:40.840
So now what you'll see is that if we don't get a 200

84
05:40.900 --> 05:45.220
which is the pass-- Let's say I mess up this end point again,

85
05:45.610 --> 05:49.270
then you'll see an exception being raised and it's telling us, look,

86
05:49.270 --> 05:53.590
it's a 404 client error. This URL is not found.

87
05:54.970 --> 05:59.720
So this request module is actually really powerful and we're going to see how to use

88
05:59.750 --> 06:03.170
various aspects of it in the upcoming lessons.

89
06:03.680 --> 06:08.510
But for now, the first thing we need to get hold of is the actual data from this

90
06:08.510 --> 06:11.270
particular API. To do that,

91
06:11.450 --> 06:15.650
we tap into our response and we get the JSON data.

92
06:16.010 --> 06:19.640
So this is the actual data. And when I print it,

93
06:19.910 --> 06:24.320
you can see that it looks exactly the same as what we got in our browser.

94
06:25.160 --> 06:28.730
Coming back to our code where we have this JSON,

95
06:29.150 --> 06:33.440
we can tap into it just like we would for any Python dictionary.

96
06:33.740 --> 06:38.740
We could use some square brackets and then type in the name of a key that we're

97
06:39.170 --> 06:41.720
interested in. For example,

98
06:41.720 --> 06:46.720
if this was a Python dictionary and we wanted to know the iss_position,

99
06:47.360 --> 06:52.360
then we would add the square brackets and then pass in iss_ position.

100
06:54.170 --> 06:56.450
And now when we print data,

101
06:56.480 --> 07:01.370
you can see it's now narrowed down and drilled into the position to give us this

102
07:01.370 --> 07:02.540
longitude and latitude.

103
07:02.960 --> 07:07.880
So we can go even further and tag on another set of square brackets and try to

104
07:07.880 --> 07:10.370
get hold of, say the longitude.

105
07:11.390 --> 07:16.040
And now we get that actual piece of data. There you have it.

106
07:16.070 --> 07:21.070
We get access to the longitude by tapping into our data in the format of a JSON

107
07:22.610 --> 07:27.380
and then getting the iss_position. And inside that dictionary,

108
07:27.560 --> 07:32.560
we can get hold of the longitude. And we can do the same thing in order to get

109
07:33.050 --> 07:35.090
hold of the latitude,

110
07:37.480 --> 07:38.170
<v 1>right?</v>

111
07:38.170 --> 07:41.350
<v 0>But we would change the key of course, to latitude.</v>

112
07:43.510 --> 07:48.510
So now I'm going to create a tuple with the longitude and the latitude.

113
07:49.660 --> 07:51.850
And now if I print it out,

114
07:51.880 --> 07:56.880
you can see how I've managed to nicely format my code and get the pieces of

115
07:57.370 --> 08:02.320
data that I want in order to be able to work with it in my project. Now,

116
08:02.350 --> 08:06.970
if you wanna see where that particular latitude and longitude is on the world

117
08:06.970 --> 08:10.390
map, then we can use a tool called latlong.net.

118
08:10.750 --> 08:15.580
So if you go to geographic tools and go to latitude and longitude to address,

119
08:15.850 --> 08:20.850
then you can paste in the longitude and latitude values that we got from

120
08:22.060 --> 08:26.470
our API. And when we click convert, then you'll see on the map,

121
08:26.560 --> 08:30.340
it shows where in the world the ISS is right now.

122
08:31.120 --> 08:35.800
So at the moment, it's somewhere to the East of Japan over the Pacific ocean.

123
08:36.040 --> 08:40.120
And of course, it's telling you no address found because I don't think anybody

124
08:40.120 --> 08:40.953
lives right there.

125
08:41.830 --> 08:45.310
So have a play around with that and make sure that you've fully understood

126
08:45.340 --> 08:50.340
what's happening in these few lines of code and how we use the API endpoint to

127
08:50.710 --> 08:54.490
get the data that we're showing up right here. In the next lesson

128
08:54.550 --> 08:55.720
I've got a challenge for you.