All right.
So the first thing we need to do that we learnt in order to work with APIs is to
import the requests module.
And once we've imported that,
we need to get a response from the URL that we saw over here.
As I mentioned, the end point is everything that's before the question mark.
So let's put that in as the URL for our get request.
And then in addition, we need to add some parameters.
So let's make our parameters a Python dictionary,
and the key must match the key that's shown here
and the value has to match the value that's given here as well.
So we've got two keys; amount and type.
So these are both going to be strings,
but the values can be numbers if it is appropriate.
So amount sets the number of questions we want back from the API,
and then the type sets the type of questions that we want. So in our case,
we want the boolean data type that matches this exact entry.
So that way we get back true-false questions.
Now that we've done that we can use this parameters dictionary as the
params,
and then we want our response to raise an exception if there were any errors.
And then we're going to get our response to give us the data using the JSON
method. If this is our data,
let's go ahead and print it and see what it looks like.
We can run this particular file, data.py, by right-clicking and just saying
run data. And once it's done, you can see we printed out the data
and the part that we're interested in is the value of this key results,
because it contains the list of questions.
That's the only part we're interested in. So in order to tap into that,
we have to add a set of square brackets and then pass in the key
which is called results. So let's put that as the key.
And now we're fetching all of the items that are the results from this data
object that we get back. So now, if I run this again, you can see
we now have a list of question objects
which each contains a dictionary
and it looks now pretty much identical to this structure that we had before.
Now, instead of printing this, we can save it as the question_data.
So now this question_data and has replaced this question_data,
and as I mentioned,
we can run our main.py and it should now work just as it did before.
But this time it's going to use questions that came from our API.
The cold war ended with Joseph Stalin's death, true or false.
I think that's actually false. But notice how
when we get the data back from the API,
it's formatted in such a way that we've got certain symbols that have been
encoded using this particular format
because all of this is meant to represent a single apostrophe. In the next
lesson,
I'm going to show you how we can format the text that we get back to turn this
encoding into the actual characters
that it should be. For all of that and more, I'll see you on the next lesson.