1
00:00:00,900 --> 00:00:04,350
So what are the response codes that we can get from an API?

2
00:00:04,890 --> 00:00:08,220
Response codes actually have a very specific meaning,

3
00:00:08,400 --> 00:00:12,290
but the most important thing they tell us is if our request succeeded or

4
00:00:12,590 --> 00:00:16,010
if it failed. Now, if you've been using the internet for a while,

5
00:00:16,010 --> 00:00:20,630
you've probably come across the 404 response code. And the 404

6
00:00:20,630 --> 00:00:24,230
response code basically means that the thing that you're looking for doesn't

7
00:00:24,230 --> 00:00:28,880
exist. And you can see various websites having fun with this 

8
00:00:28,880 --> 00:00:29,713
404 page.

9
00:00:29,960 --> 00:00:33,530
Basically when you're trying to retrieve something from a website and it doesn't

10
00:00:33,530 --> 00:00:36,650
actually have the thing you're looking for, then this is the code you get.

11
00:00:37,100 --> 00:00:41,150
But there's actually a lot more response codes other than just 404.

12
00:00:41,690 --> 00:00:42,650
And in fact,

13
00:00:42,680 --> 00:00:46,700
you could summarize these status codes just by looking at the first number.

14
00:00:47,240 --> 00:00:51,620
If it is 100 and something, then it's telling you, hold on,

15
00:00:51,800 --> 00:00:54,050
something's happening. This is not final.

16
00:00:54,410 --> 00:00:58,790
If it's 200 something then it is saying, here you go. Everything was successful.

17
00:00:58,940 --> 00:01:00,980
You should be getting the data you expected.

18
00:01:01,940 --> 00:01:06,800
If it's 300 something then it usually means you don't actually have permission

19
00:01:06,800 --> 00:01:11,600
to get this thing, so go away. 400 something, it means that 

20
00:01:11,630 --> 00:01:15,020
you screwed up. Either it could be a 404

21
00:01:15,380 --> 00:01:16,910
which means that you screwed up

22
00:01:16,970 --> 00:01:19,130
and the thing you're looking for doesn't even exist,

23
00:01:19,610 --> 00:01:22,700
or it could be 500 something, which means I,

24
00:01:22,760 --> 00:01:27,760
as in the server that you are making the request to, screwed up. And maybe the

25
00:01:28,370 --> 00:01:32,720
server is down, maybe the website is down or maybe there's some other issue.

26
00:01:33,620 --> 00:01:34,940
So that's just a quick,

27
00:01:34,970 --> 00:01:38,870
at a glance, sort of way of remembering what's actually happening

28
00:01:38,870 --> 00:01:40,970
when you are getting these response codes.

29
00:01:42,110 --> 00:01:46,640
Now you can actually dig deeper into this response object and you can get a

30
00:01:46,640 --> 00:01:49,310
whole bunch of things from it. For example,

31
00:01:49,310 --> 00:01:54,020
if you wanted to know the actual status code instead of just a response object,

32
00:01:54,350 --> 00:01:57,230
then you can say response.status_code,

33
00:01:57,560 --> 00:02:01,040
and you actually only get the status code. But notice how

34
00:02:01,040 --> 00:02:04,910
if I change this URL and I make a typo in here,

35
00:02:05,240 --> 00:02:07,220
instead of iss I have is,

36
00:02:07,580 --> 00:02:11,600
then now I get a 404 as my response status code.

37
00:02:12,860 --> 00:02:17,300
OK. So if we get the status code 200 then it means that we were successful.

38
00:02:17,780 --> 00:02:20,150
But what do we do when we get a different status code?

39
00:02:20,420 --> 00:02:23,150
What should our program do when the request fails?

40
00:02:23,690 --> 00:02:28,690
We might be tempted to write code like this. By checking to see if the response

41
00:02:30,170 --> 00:02:33,620
.status_code is not equal to 200,

42
00:02:33,920 --> 00:02:38,570
we can actually figure out well, this means that there probably was an error.

43
00:02:39,260 --> 00:02:44,260
Now it doesn't really make sense to just print out an error because the user who's

44
00:02:44,480 --> 00:02:47,930
using our program might be expecting something to happen.

45
00:02:48,350 --> 00:02:52,700
So in most, cases you might want to raise an exception instead.

46
00:02:52,700 --> 00:02:57,700
You could say bad response from ISS API.

47
00:02:58,730 --> 00:03:02,230
But we learned from our module on errors and exceptions,

48
00:03:02,560 --> 00:03:06,730
this is not very good. Firstly, this is not a very specific exception.

49
00:03:07,090 --> 00:03:11,230
It's going to raise a very general type of exception. And secondly,

50
00:03:11,260 --> 00:03:14,650
all that we're catching is when the status code is not 200.

51
00:03:14,980 --> 00:03:19,240
What if we wanted you to know specifically which status code we got back? Well,

52
00:03:19,240 --> 00:03:20,830
we could, of course check well

53
00:03:20,840 --> 00:03:24,730
if the response status code was equal to say

54
00:03:24,730 --> 00:03:29,730
404, then we'll raise a particular type of exception that says that resource

55
00:03:31,870 --> 00:03:36,670
does not exist. And then we can have,

56
00:03:36,700 --> 00:03:38,830
um, an elif that says, well,

57
00:03:38,830 --> 00:03:42,940
if the response status code was 401,

58
00:03:43,210 --> 00:03:48,210
then that means that you should raise an exception because this particular code

59
00:03:50,170 --> 00:03:55,170
only happens when you are not authorized to access this data.

60
00:03:58,660 --> 00:04:02,710
Now there's a lot of status codes that you could be getting back.

61
00:04:03,040 --> 00:04:04,060
And in the course

62
00:04:04,060 --> 00:04:08,680
resources I'll link to this particular web page where it tells you all of the

63
00:04:08,680 --> 00:04:13,680
possible codes that you could be getting and what it means. As you can imagine,

64
00:04:13,990 --> 00:04:16,959
it's impossible for us to write this many

65
00:04:16,959 --> 00:04:21,880
if statements checking for every single possibility. What do we do instead?

66
00:04:22,540 --> 00:04:26,680
You can actually do away with all of this by getting the request module

67
00:04:26,950 --> 00:04:29,200
to generate the exception instead.

68
00:04:29,860 --> 00:04:34,860
The requests module is the most popular way for Python developers to work with

69
00:04:37,450 --> 00:04:42,310
APIs. And you can see in their statistics just the number of stars,

70
00:04:42,340 --> 00:04:43,240
number of forks,

71
00:04:43,870 --> 00:04:48,190
and from these stats, you can see just how much active development there is and

72
00:04:48,220 --> 00:04:51,370
how popular this particular module is. Now,

73
00:04:51,400 --> 00:04:53,560
if you want to take a look at the documentation,

74
00:04:53,830 --> 00:04:58,240
then you get taken to their website and it tells you all of the things that it

75
00:04:58,240 --> 00:05:01,750
can do, which is quite a lot actually.

76
00:05:02,500 --> 00:05:04,450
Now I won't read through all of this with you,

77
00:05:04,720 --> 00:05:08,170
but if we take a look at one of the sections, Errors and Exceptions,

78
00:05:08,530 --> 00:05:13,530
you can actually call raise for status on the response you get back in order

79
00:05:13,540 --> 00:05:15,490
to raise a HTTP error

80
00:05:15,610 --> 00:05:18,790
if the request returned an unsuccessful status code.

81
00:05:19,600 --> 00:05:24,600
Basically instead of us trying to raise an exception for every single possible

82
00:05:24,670 --> 00:05:29,670
status code and telling the developer what might be the reason, we can simply use

83
00:05:30,520 --> 00:05:35,520
the request module by saying response.raise_for_status.

84
00:05:36,070 --> 00:05:40,840
So now what you'll see is that if we don't get a 200

85
00:05:40,900 --> 00:05:45,220
which is the pass-- Let's say I mess up this end point again,

86
00:05:45,610 --> 00:05:49,270
then you'll see an exception being raised and it's telling us, look,

87
00:05:49,270 --> 00:05:53,590
it's a 404 client error. This URL is not found.

88
00:05:54,970 --> 00:05:59,720
So this request module is actually really powerful and we're going to see how to use

89
00:05:59,750 --> 00:06:03,170
various aspects of it in the upcoming lessons.

90
00:06:03,680 --> 00:06:08,510
But for now, the first thing we need to get hold of is the actual data from this

91
00:06:08,510 --> 00:06:11,270
particular API. To do that,

92
00:06:11,450 --> 00:06:15,650
we tap into our response and we get the JSON data.

93
00:06:16,010 --> 00:06:19,640
So this is the actual data. And when I print it,

94
00:06:19,910 --> 00:06:24,320
you can see that it looks exactly the same as what we got in our browser.

95
00:06:25,160 --> 00:06:28,730
Coming back to our code where we have this JSON,

96
00:06:29,150 --> 00:06:33,440
we can tap into it just like we would for any Python dictionary.

97
00:06:33,740 --> 00:06:38,740
We could use some square brackets and then type in the name of a key that we're

98
00:06:39,170 --> 00:06:41,720
interested in. For example,

99
00:06:41,720 --> 00:06:46,720
if this was a Python dictionary and we wanted to know the iss_position,

100
00:06:47,360 --> 00:06:52,360
then we would add the square brackets and then pass in iss_ position.

101
00:06:54,170 --> 00:06:56,450
And now when we print data,

102
00:06:56,480 --> 00:07:01,370
you can see it's now narrowed down and drilled into the position to give us this

103
00:07:01,370 --> 00:07:02,540
longitude and latitude.

104
00:07:02,960 --> 00:07:07,880
So we can go even further and tag on another set of square brackets and try to

105
00:07:07,880 --> 00:07:10,370
get hold of, say the longitude.

106
00:07:11,390 --> 00:07:16,040
And now we get that actual piece of data. There you have it.

107
00:07:16,070 --> 00:07:21,070
We get access to the longitude by tapping into our data in the format of a JSON

108
00:07:22,610 --> 00:07:27,380
and then getting the iss_position. And inside that dictionary,

109
00:07:27,560 --> 00:07:32,560
we can get hold of the longitude. And we can do the same thing in order to get

110
00:07:33,050 --> 00:07:35,090
hold of the latitude,

111
00:07:37,480 --> 00:07:38,170
right?

112
00:07:38,170 --> 00:07:41,350
But we would change the key of course, to latitude.

113
00:07:43,510 --> 00:07:48,510
So now I'm going to create a tuple with the longitude and the latitude.

114
00:07:49,660 --> 00:07:51,850
And now if I print it out,

115
00:07:51,880 --> 00:07:56,880
you can see how I've managed to nicely format my code and get the pieces of

116
00:07:57,370 --> 00:08:02,320
data that I want in order to be able to work with it in my project. Now,

117
00:08:02,350 --> 00:08:06,970
if you wanna see where that particular latitude and longitude is on the world

118
00:08:06,970 --> 00:08:10,390
map, then we can use a tool called latlong.net.

119
00:08:10,750 --> 00:08:15,580
So if you go to geographic tools and go to latitude and longitude to address,

120
00:08:15,850 --> 00:08:20,850
then you can paste in the longitude and latitude values that we got from

121
00:08:22,060 --> 00:08:26,470
our API. And when we click convert, then you'll see on the map,

122
00:08:26,560 --> 00:08:30,340
it shows where in the world the ISS is right now.

123
00:08:31,120 --> 00:08:35,799
So at the moment, it's somewhere to the East of Japan over the Pacific ocean.

124
00:08:36,039 --> 00:08:40,120
And of course, it's telling you no address found because I don't think anybody

125
00:08:40,120 --> 00:08:40,953
lives right there.

126
00:08:41,830 --> 00:08:45,310
So have a play around with that and make sure that you've fully understood

127
00:08:45,340 --> 00:08:50,340
what's happening in these few lines of code and how we use the API endpoint to

128
00:08:50,710 --> 00:08:54,490
get the data that we're showing up right here. In the next lesson

129
00:08:54,550 --> 00:08:55,720
I've got a challenge for you.

