#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Defining the API methods in Flask

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, we're going to do something that makes my skin crawl a little bit. We're going to write a ton of code into this one,
0:07 ginormous, what's going to become a ginormous single file web app, yuk. Afterwards, I'll refactor it in a way that I think is much cleaner and simpler.
0:18 If you don't like the refactored one I do, you can just stop with what we're going to do this time around and leave it organized this way.
0:24 I think you'll find it to be a lot nicer. Okay, so what is the goal of this particular video? We're going to go in here, and we're going to define
0:33 7 API methods, the 7 that we talked about at the beginning, and recall down here, we saw how to do this.
0:39 We have some kind of route, some kind of HTTP verb. These are very important in RESTful services. We get the data; this, we're going to get
0:48 from the database in the end. And, we're going to return just the JSON response of that dictionary. So, just to keep things movin' along,
1:00 I'm going to go down here, and I'm going to, let's put these at the very bottom. I'm going to paste in some stuff, and we'll talk about it.
1:11 These are not implementations. These are just getting started. So, the first one is going to be: find a particular user.
1:18 So, we want to have api/game/users. And then, you'll put the name here. So, api/game/users/{name}, /computer /jeff or /jennifer.
1:30 You say, whatever you want, there at the end, and that becomes this argument, right here. So, let's just do something like: return would find,
1:41 just to make sure everything's working. We'll see. So, we still got to put the details of what that means. It turns out this one's quite simple.
1:49 Some are really easy, some, not so much. So, what is the next one? So, down here, we're going to create a user.
1:57 And, we're going to do an HTTP PUT to the user's collection. Why? Well, there's only two reasonable options, here. GET, get is out.
2:06 Delete is obviously out. PATCH, obviously, they're out. But, POST or PUT, and if you think you can do the operation
2:13 again and again and it shouldn't affect the state of the system, well then, PUT is probably what you want to do if you know where it goes.
2:19 So, it's kind of, we're creating the /user, whatever we paste in here. So, this is going to create a user, and it's going to be interesting.
2:26 I'll just say, we'd create_user. It's going to be interesting how we get to that data, and that's not too hard. Next, we're going to create a new game.
2:38 So, over here, we have game. And then, you're going to just do a POST to game. You don't know where the ID or the link of that game is going to go.
2:45 So, we're using POST as opposed to here, we know the name of the user we're creating, which drives the URL. That's the reason PUT.
2:51 You don't have to be this nuanced with the rest, but this is the way I'm doin' it. Next one up, we're just going to be able to get all the rules.
2:58 Remember, we want to be able to change the rules, potentially, in the server and have all the clients immediately adapt.
3:04 And, this is a read operation, so we're going to do a GET, very, very straightforward. We'll implement that momentarily.
3:12 Next step, we want to get the game status. This is a read operation, so it's GET. We have api/game, and then here, in this part,
3:19 we're going to do the game ID. So, API/gameS223/status. And, that's going to be passed in here,
3:28 and then we'll just say, almost there, with our methods. So, the next thing we want to do is just get the top scores. Again, this is a read operation.
3:37 So, we'll just return. And finally, comes our most complicated but also most important method. Those often go together, don't they? Called play_round.
3:49 And, because this is modifying, it's doing a POST. That's not where it goes, so this one is, it's ready to POST.
3:56 Okay, so we have all of these methods, here, if I can get my little green helper thing to get out of the way. You notice that somewhere along the way,
4:04 it was detecting changes, and it said, "A view mapping over ID. "Existing end point: find_user." That just happened 'cause I duplicated something
4:14 before I typed it in. That happens, sometimes, when Flask gets into bug mode, is you can be messin' with it, and if you put, like,
4:19 junk and you accidentally save it, it tries to reload. But then, it detects some kind of error, and if I actually take that away and save again,
4:27 it's, the process is gone, got to restart it. Okay, so let's just test one of these: would find user jeff. How cool, huh?
4:42 And, it's workin' pretty well. This works pretty well because this is a get operation, but how do you test this users by doing a PUT operation
4:51 in the browser? Now, it's super easy. So, we're going to employ Postman to configure all this stuff for us.

Talk Python's Mastodon Michael Kennedy's Mastodon