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