WEBVTT

0
00:00.990 --> 00:01.350
All right.

1
00:01.350 --> 00:06.350
So the first thing we need to do that we learnt in order to work with APIs is to

2
00:06.510 --> 00:08.910
import the requests module.

3
00:09.390 --> 00:11.520
And once we've imported that,

4
00:11.640 --> 00:16.640
we need to get a response from the URL that we saw over here.

5
00:18.270 --> 00:22.440
As I mentioned, the end point is everything that's before the question mark.

6
00:22.890 --> 00:26.760
So let's put that in as the URL for our get request.

7
00:27.270 --> 00:30.180
And then in addition, we need to add some parameters.

8
00:30.690 --> 00:35.040
So let's make our parameters a Python dictionary,

9
00:35.760 --> 00:40.110
and the key must match the key that's shown here

10
00:40.500 --> 00:43.830
and the value has to match the value that's given here as well.

11
00:44.250 --> 00:46.770
So we've got two keys; amount and type.

12
00:47.220 --> 00:49.680
So these are both going to be strings,

13
00:49.710 --> 00:53.340
but the values can be numbers if it is appropriate.

14
00:53.850 --> 00:58.170
So amount sets the number of questions we want back from the API,

15
00:58.770 --> 01:03.270
and then the type sets the type of questions that we want. So in our case,

16
01:03.300 --> 01:08.220
we want the boolean data type that matches this exact entry.

17
01:08.520 --> 01:11.160
So that way we get back true-false questions.

18
01:11.940 --> 01:16.410
Now that we've done that we can use this parameters dictionary as the

19
01:16.410 --> 01:17.243
params,

20
01:17.700 --> 01:22.700
and then we want our response to raise an exception if there were any errors.

21
01:23.460 --> 01:27.750
And then we're going to get our response to give us the data using the JSON

22
01:27.750 --> 01:31.290
method. If this is our data,

23
01:31.320 --> 01:34.650
let's go ahead and print it and see what it looks like.

24
01:35.100 --> 01:40.100
We can run this particular file, data.py, by right-clicking and just saying

25
01:40.170 --> 01:44.940
run data. And once it's done, you can see we printed out the data

26
01:45.450 --> 01:50.450
and the part that we're interested in is the value of this key results,

27
01:51.300 --> 01:55.050
because it contains the list of questions.

28
01:55.590 --> 01:59.760
That's the only part we're interested in. So in order to tap into that,

29
02:00.060 --> 02:03.750
we have to add a set of square brackets and then pass in the key

30
02:04.260 --> 02:08.100
which is called results. So let's put that as the key.

31
02:08.400 --> 02:13.400
And now we're fetching all of the items that are the results from this data

32
02:14.430 --> 02:19.140
object that we get back. So now, if I run this again, you can see

33
02:19.140 --> 02:22.560
we now have a list of question objects

34
02:22.890 --> 02:25.080
which each contains a dictionary

35
02:25.350 --> 02:29.520
and it looks now pretty much identical to this structure that we had before.

36
02:30.300 --> 02:34.890
Now, instead of printing this, we can save it as the question_data.

37
02:36.450 --> 02:40.200
So now this question_data and has replaced this question_data,

38
02:40.620 --> 02:41.760
and as I mentioned,

39
02:41.790 --> 02:46.790
we can run our main.py and it should now work just as it did before.

40
02:49.200 --> 02:54.200
But this time it's going to use questions that came from our API.

41
02:54.840 --> 02:58.890
The cold war ended with Joseph Stalin's death, true or false.

42
02:59.170 --> 03:03.430
I think that's actually false. But notice how

43
03:03.430 --> 03:05.830
when we get the data back from the API,

44
03:06.160 --> 03:11.160
it's formatted in such a way that we've got certain symbols that have been

45
03:11.290 --> 03:14.290
encoded using this particular format

46
03:14.650 --> 03:19.650
because all of this is meant to represent a single apostrophe. In the next

47
03:19.900 --> 03:20.350
lesson,

48
03:20.350 --> 03:25.120
I'm going to show you how we can format the text that we get back to turn this

49
03:25.120 --> 03:28.030
encoding into the actual characters

50
03:28.180 --> 03:32.650
that it should be. For all of that and more, I'll see you on the next lesson.