WEBVTT

0
00:00.270 --> 00:05.130
Now that you've seen how you can make the most basic type of request to an API

1
00:05.130 --> 00:09.600
endpoint, I want you to build a Kanye quote machine.

2
00:09.960 --> 00:14.880
This is what it's going to look like and these are real Kanye quotes.

3
00:15.300 --> 00:17.970
So every time you click on this button

4
00:18.000 --> 00:21.690
which has a Kanye emoji, you can see

5
00:21.690 --> 00:23.220
he'll say different things.

6
00:24.000 --> 00:29.000
And some of the things he says are incredibly deep and other things are a little

7
00:29.730 --> 00:33.810
bit weird. But we've created the starting file for this

8
00:34.080 --> 00:37.110
so you don't have to spend a lot of time building up the UI.

9
00:37.470 --> 00:41.850
All I want you to do is to figure out how to use the API

10
00:41.880 --> 00:43.950
which is at kanye.rest

11
00:44.370 --> 00:49.020
and this is the API endpoint. So you can test it out of course

12
00:49.020 --> 00:52.110
in your browser just by putting it into the URL bar.

13
00:52.560 --> 00:57.560
And you can see you get back a very simple JSON with just one key and one

14
00:58.530 --> 01:03.060
value. And every time you run it, it'll give you a random quote.

15
01:03.900 --> 01:08.100
Head over to the course resources and download the Kanye quote

16
01:08.130 --> 01:12.060
starting file. And once you've unzipped it and opened it up,

17
01:12.330 --> 01:16.290
you can see that there's the background.png, the kanye.png,

18
01:16.650 --> 01:21.650
and there's all of the existing code that's required to generate this user

19
01:22.830 --> 01:26.220
interface in tkinter. The point here is,

20
01:26.520 --> 01:28.530
I know you can make tkinter apps,

21
01:28.830 --> 01:33.570
but I want you to use the request module that you learned about just now in

22
01:33.570 --> 01:38.400
order to get this function to work so that every time the Kanye button,

23
01:38.550 --> 01:43.080
this one, is pressed, then we trigger this get_quote function.

24
01:43.500 --> 01:48.500
And this is going to fetch the quote from the Kanye API here and fill it into

25
01:51.780 --> 01:56.490
this quote_text on the canvas. Using what you've learned before

26
01:56.850 --> 02:00.990
I think you can do this pretty quickly. So pause the video now and give it a go.

27
02:05.300 --> 02:05.810
<v 1>Yeah.</v>

28
02:05.810 --> 02:07.070
<v 0>All right. So to begin,</v>

29
02:07.130 --> 02:11.660
we, of course, need to import our very important requests module,

30
02:12.290 --> 02:15.890
and you need to install it if you see some red squiggly lines.

31
02:16.310 --> 02:20.210
But if you see it grayed out like this then it means that we've already installed

32
02:20.210 --> 02:23.210
it into the starting files. Now,

33
02:23.210 --> 02:25.580
once we've got hold of that module,

34
02:25.640 --> 02:30.080
then we can write some code here so that we get our function to work.

35
02:30.620 --> 02:35.540
Now, what should happen when the user presses on the Kanye button? Well,

36
02:35.780 --> 02:38.690
we have to fetch a new quote from the API.

37
02:39.290 --> 02:41.360
This is the API endpoint

38
02:41.660 --> 02:46.660
and we're going to use the requests module to make a request and get some data

39
02:47.930 --> 02:49.490
from this API.

40
02:50.240 --> 02:54.170
So this is the code that we used before to figure out where the ISS was.

41
02:54.680 --> 02:59.170
And we're going to save this as the response. Now,

42
02:59.170 --> 03:01.060
once we've got our response,

43
03:01.120 --> 03:05.260
we're going to call response.raise_for_status.

44
03:05.290 --> 03:09.880
So if we don't get a 200, which is everything is okay,

45
03:10.120 --> 03:13.300
then we want to actually raise an exception. Now,

46
03:13.360 --> 03:17.830
the next thing we're gonna do is I'm going to get my response to show me the

47
03:17.890 --> 03:21.370
JSON data. And I'm gonna save that as the data.

48
03:22.030 --> 03:25.930
And then if we take a look at what the response looks like,

49
03:26.230 --> 03:30.160
then you can see it's just a single key-value pair

50
03:30.520 --> 03:35.050
and the key is quote and the value is the random quote.

51
03:35.680 --> 03:40.680
So we can get hold of the actual quote by simply tapping to our data and then

52
03:41.260 --> 03:45.730
using square brackets to fetch the value under the key quote.

53
03:46.570 --> 03:48.610
And once we've got that piece of text,

54
03:48.700 --> 03:53.080
then we want to put it into our quote_text which is on our canvas.

55
03:53.560 --> 03:55.120
And as you saw before,

56
03:55.150 --> 03:59.290
the way we do this is we say canvas.itemconfig.

57
03:59.620 --> 04:03.700
And the item we want to configure is, of course, our quote_text.

58
04:04.240 --> 04:07.810
And the thing that we want to configure about it is its text.

59
04:08.170 --> 04:11.860
And we're going to set it to equal this quote that we fetched just now.

60
04:13.060 --> 04:15.610
So now if I run my code,

61
04:16.000 --> 04:19.120
you can see my Kanye says app showing up,

62
04:19.540 --> 04:22.030
and this is where the Kanye quote is going to go.

63
04:22.510 --> 04:27.510
So if I click on Kanye, you can see how it shows all of his quotes.

64
04:29.230 --> 04:34.060
So did you manage to get that? If not, be sure to head back to the last lesson and

65
04:34.120 --> 04:38.680
take a closer look at how we formatted our request and our response.