#100DaysOfWeb in Python Transcripts
Chapter: Days 17-20: Calling APIs in Flask
Lecture: Breaking down the Poke API response

Login or purchase this course to watch this video and the rest of the course contents.
0:00 For the fun part, now we get to actually play with the data that we're going to be calling from the API. One of the fun things to do
0:09 and probably the better thing to do is to play around with all of this API call stuff in the Python shell before you put it into an application,
0:20 that will you know what works, you get to play around with it, see test things out and you know, just have a bit of fun.
0:26 So let's bring up the shell here. And what we're going to do, we're still in our virtual environment, that way all of the modules
0:33 that we imported or pip installed, are accessible to this Python shell. And we're just going to run a couple of the commands
0:41 that we're going to put into our routes.py file, but we can test them here first, without having to keep editing that file,
0:49 saving it running it we just do it live. Right? Thinking ahead, we are going to have a list of Pokemon. Right? So let's create an empty list
1:00 I've already done this testing before, obviously, this I'm just saving a bit of time. Next up we need to still import everything, so we import requests
1:11 because we going to do a request we will import json because we going to work with JSON. And just for demonstrative purposes,
1:18 we're going to import pprint So from pprint and this is pretty print, it'll just make our return JSON output look nicer,
1:27 but I'll show you that in a minute. So from pprint, import pprint as pp just so I don't have to type that pprint every time
1:35 I only have to type in pp now to call that. All right, next we need to do our request, so we'll do r = requests.getget.
1:46 And based on this Pokemon API page over here, we have our API call Alright, so we have the actual endpoint we going to talk to and that is going to be
2:00 https://pokeapi.co/api/v2/pokemon-color And we're talking to Pokemon color end point. All right, and we'll hit enter on that.
2:18 That should work yes we have it. And now we're going to actually format it nicely. With we're going to decode it nicely with JSON.
2:27 So pokedata we going to call this object pokedata = r.json() Right? And now we can pretty print this out to actually format it nicely.
2:40 Because look at this if we just print this out normally we get this it's a bit ugly, right? We want it to format nicely
2:50 So we can go pprint sorry of pokedata. Right and we have these endpoints here for the different colors so we can we now know that people can specify
3:02 black, blue, brown and blah, blah, blah, down the list to yellow. Anything else will fail. But that's a catch That's an exception for another day.
3:11 For now, let's just work on these. What we can do now is let's change our request to include one of these colors on the end, we're just hard coding it.
3:21 Remember, the user will be able to specify this for now let's hard code it. And the color we shall choose let's go with yellow. That will work.
3:32 And now we can reassign pokedata. Right? And we can do pprint of pokedata. And what we've done is we've sent a request off to the Pokemon API,
3:44 and we've said return all the Pokemon that are of the color yellow are tagged as the color yellow. And now what we're doing is
3:52 we're just printing all of those Pokemon out. Now mind you, you're not just going to get the name at this point,
3:57 you're going to get that Pokemon and all their attributes. So just be prepared we've got quite a lot here, look at that list there's heaps there.
4:05 Let's just focus on the bottom one here. And it says whatever they whatever that is, I've never heard of that one. So we've got that Pokemon
4:16 And now they had that Pokemon itself has an endpoint. So this URL if we were to pop that into the browser, we get the return value of that Pokemon,
4:26 all this stats, right? But all we want is their name. So we want to call on this name object here, this name key here I should say.
4:37 So what we can do is, we can go for i in pokedata, that means each one of these, each one of these entries here is an individual entry
4:50 that we're going to iterate over using i, so for i in pokedata. Now if we scroll to the top of this output, you'll see that these all come
5:05 underneath one object here one key sorry called Pokemon species. So we have to so these are all individual line items within Pokemon species, right
5:17 So we need to specify that Pokemon species. Now this is all pretty much JSON decoding stuff, and I'm just sort of skimming over it
5:25 because that could be a whole chapter in itself. It was in our previous course so check that out if you haven't, I go into way more detail on that one.
5:33 So for i in pokedata and we're going to specify Pokemon species. And what are we going to do every time we hit one of these we are going to append
5:50 the name of that Pokemon to our Pokemon list remember we created that Pokemon that empty Pokemon list at the start, so we are going to go i name.
6:05 And we close that off. That's done and now we can do for i in Pokemon. Print i and then we have a list of names
6:19 and this is the data that will be returned to our page. So all of that code we just used will actually work
6:26 and we can pop that into a function in R dot pie. And I'll show you that in the next video. Once that's in there, we can take this pokemon list
6:36 that we've just specified here this one there, and we can return that to our template our Flask template. So let's move on to the next video.


Talk Python's Mastodon Michael Kennedy's Mastodon