Now that you've seen how you can make the most basic type of request to an API
endpoint, I want you to build a Kanye quote machine.
This is what it's going to look like and these are real Kanye quotes.
So every time you click on this button
which has a Kanye emoji, you can see
he'll say different things.
And some of the things he says are incredibly deep and other things are a little
bit weird. But we've created the starting file for this
so you don't have to spend a lot of time building up the UI.
All I want you to do is to figure out how to use the API
which is at kanye.rest
and this is the API endpoint. So you can test it out of course
in your browser just by putting it into the URL bar.
And you can see you get back a very simple JSON with just one key and one
value. And every time you run it, it'll give you a random quote.
Head over to the course resources and download the Kanye quote
starting file. And once you've unzipped it and opened it up,
you can see that there's the background.png, the kanye.png,
and there's all of the existing code that's required to generate this user
interface in tkinter. The point here is,
I know you can make tkinter apps,
but I want you to use the request module that you learned about just now in
order to get this function to work so that every time the Kanye button,
this one, is pressed, then we trigger this get_quote function.
And this is going to fetch the quote from the Kanye API here and fill it into
this quote_text on the canvas. Using what you've learned before
I think you can do this pretty quickly. So pause the video now and give it a go.
1: Yeah.
0: All right. So to begin,
we, of course, need to import our very important requests module,
and you need to install it if you see some red squiggly lines.
But if you see it grayed out like this then it means that we've already installed
it into the starting files. Now,
once we've got hold of that module,
then we can write some code here so that we get our function to work.
Now, what should happen when the user presses on the Kanye button? Well,
we have to fetch a new quote from the API.
This is the API endpoint
and we're going to use the requests module to make a request and get some data
from this API.
So this is the code that we used before to figure out where the ISS was.
And we're going to save this as the response. Now,
once we've got our response,
we're going to call response.raise_for_status.
So if we don't get a 200, which is everything is okay,
then we want to actually raise an exception. Now,
the next thing we're gonna do is I'm going to get my response to show me the
JSON data. And I'm gonna save that as the data.
And then if we take a look at what the response looks like,
then you can see it's just a single key-value pair
and the key is quote and the value is the random quote.
So we can get hold of the actual quote by simply tapping to our data and then
using square brackets to fetch the value under the key quote.
And once we've got that piece of text,
then we want to put it into our quote_text which is on our canvas.
And as you saw before,
the way we do this is we say canvas.itemconfig.
And the item we want to configure is, of course, our quote_text.
And the thing that we want to configure about it is its text.
And we're going to set it to equal this quote that we fetched just now.
So now if I run my code,
you can see my Kanye says app showing up,
and this is where the Kanye quote is going to go.
So if I click on Kanye, you can see how it shows all of his quotes.
So did you manage to get that? If not, be sure to head back to the last lesson and
take a closer look at how we formatted our request and our response.