1
00:00:00,270 --> 00:00:05,130
Now that you've seen how you can make the most basic type of request to an API

2
00:00:05,130 --> 00:00:09,600
endpoint, I want you to build a Kanye quote machine.

3
00:00:09,960 --> 00:00:14,880
This is what it's going to look like and these are real Kanye quotes.

4
00:00:15,300 --> 00:00:17,970
So every time you click on this button

5
00:00:18,000 --> 00:00:21,690
which has a Kanye emoji, you can see

6
00:00:21,690 --> 00:00:23,220
he'll say different things.

7
00:00:24,000 --> 00:00:29,000
And some of the things he says are incredibly deep and other things are a little

8
00:00:29,730 --> 00:00:33,810
bit weird. But we've created the starting file for this

9
00:00:34,080 --> 00:00:37,110
so you don't have to spend a lot of time building up the UI.

10
00:00:37,470 --> 00:00:41,850
All I want you to do is to figure out how to use the API

11
00:00:41,880 --> 00:00:43,950
which is at kanye.rest

12
00:00:44,370 --> 00:00:49,020
and this is the API endpoint. So you can test it out of course

13
00:00:49,020 --> 00:00:52,110
in your browser just by putting it into the URL bar.

14
00:00:52,560 --> 00:00:57,560
And you can see you get back a very simple JSON with just one key and one

15
00:00:58,530 --> 00:01:03,060
value. And every time you run it, it'll give you a random quote.

16
00:01:03,900 --> 00:01:08,100
Head over to the course resources and download the Kanye quote

17
00:01:08,130 --> 00:01:12,060
starting file. And once you've unzipped it and opened it up,

18
00:01:12,330 --> 00:01:16,290
you can see that there's the background.png, the kanye.png,

19
00:01:16,650 --> 00:01:21,650
and there's all of the existing code that's required to generate this user

20
00:01:22,830 --> 00:01:26,220
interface in tkinter. The point here is,

21
00:01:26,520 --> 00:01:28,530
I know you can make tkinter apps,

22
00:01:28,830 --> 00:01:33,570
but I want you to use the request module that you learned about just now in

23
00:01:33,570 --> 00:01:38,400
order to get this function to work so that every time the Kanye button,

24
00:01:38,550 --> 00:01:43,080
this one, is pressed, then we trigger this get_quote function.

25
00:01:43,500 --> 00:01:48,500
And this is going to fetch the quote from the Kanye API here and fill it into

26
00:01:51,780 --> 00:01:56,490
this quote_text on the canvas. Using what you've learned before

27
00:01:56,850 --> 00:02:00,990
I think you can do this pretty quickly. So pause the video now and give it a go.

28
00:02:05,300 --> 00:02:05,810
Yeah.

29
00:02:05,810 --> 00:02:07,070
All right. So to begin,

30
00:02:07,130 --> 00:02:11,660
we, of course, need to import our very important requests module,

31
00:02:12,290 --> 00:02:15,890
and you need to install it if you see some red squiggly lines.

32
00:02:16,310 --> 00:02:20,210
But if you see it grayed out like this then it means that we've already installed

33
00:02:20,210 --> 00:02:23,210
it into the starting files. Now,

34
00:02:23,210 --> 00:02:25,580
once we've got hold of that module,

35
00:02:25,640 --> 00:02:30,080
then we can write some code here so that we get our function to work.

36
00:02:30,620 --> 00:02:35,540
Now, what should happen when the user presses on the Kanye button? Well,

37
00:02:35,780 --> 00:02:38,690
we have to fetch a new quote from the API.

38
00:02:39,290 --> 00:02:41,360
This is the API endpoint

39
00:02:41,660 --> 00:02:46,660
and we're going to use the requests module to make a request and get some data

40
00:02:47,930 --> 00:02:49,490
from this API.

41
00:02:50,240 --> 00:02:54,170
So this is the code that we used before to figure out where the ISS was.

42
00:02:54,680 --> 00:02:59,170
And we're going to save this as the response. Now,

43
00:02:59,170 --> 00:03:01,060
once we've got our response,

44
00:03:01,120 --> 00:03:05,260
we're going to call response.raise_for_status.

45
00:03:05,290 --> 00:03:09,880
So if we don't get a 200, which is everything is okay,

46
00:03:10,120 --> 00:03:13,300
then we want to actually raise an exception. Now,

47
00:03:13,360 --> 00:03:17,830
the next thing we're gonna do is I'm going to get my response to show me the

48
00:03:17,890 --> 00:03:21,370
JSON data. And I'm gonna save that as the data.

49
00:03:22,030 --> 00:03:25,930
And then if we take a look at what the response looks like,

50
00:03:26,230 --> 00:03:30,160
then you can see it's just a single key-value pair

51
00:03:30,520 --> 00:03:35,050
and the key is quote and the value is the random quote.

52
00:03:35,680 --> 00:03:40,680
So we can get hold of the actual quote by simply tapping to our data and then

53
00:03:41,260 --> 00:03:45,730
using square brackets to fetch the value under the key quote.

54
00:03:46,570 --> 00:03:48,610
And once we've got that piece of text,

55
00:03:48,700 --> 00:03:53,080
then we want to put it into our quote_text which is on our canvas.

56
00:03:53,560 --> 00:03:55,120
And as you saw before,

57
00:03:55,150 --> 00:03:59,290
the way we do this is we say canvas.itemconfig.

58
00:03:59,620 --> 00:04:03,700
And the item we want to configure is, of course, our quote_text.

59
00:04:04,240 --> 00:04:07,810
And the thing that we want to configure about it is its text.

60
00:04:08,170 --> 00:04:11,860
And we're going to set it to equal this quote that we fetched just now.

61
00:04:13,060 --> 00:04:15,610
So now if I run my code,

62
00:04:16,000 --> 00:04:19,120
you can see my Kanye says app showing up,

63
00:04:19,540 --> 00:04:22,029
and this is where the Kanye quote is going to go.

64
00:04:22,510 --> 00:04:27,510
So if I click on Kanye, you can see how it shows all of his quotes.

65
00:04:29,230 --> 00:04:34,060
So did you manage to get that? If not, be sure to head back to the last lesson and

66
00:04:34,120 --> 00:04:38,680
take a closer look at how we formatted our request and our response.

