#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Implementing the find-user method

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's turn our attention to finding a user. This is pretty straightforward, but it does introduce an entirely new concept.
0:09 I could ask for a user that doesn't exist, right? Over here, we can go to our get_user, and Mark, I don't think Mark exists in the database,
0:17 it just says, hey, we would find him. I'm sure that Mark doesn't exist. Eh, it still thinks it would find him. And the status code is 200 OK.
0:26 So what we need to do is say, yes, this user was found and here's their details, or no, the user was not found, and here's what happened.
0:35 So it's really easy to do this with the code we've already written and encapsulated in our service. Alright, so, there's our user.
0:49 And if it wasn't null, we could return it. Be super easy. Let's go ahead and write that code really quick. Now, to all these database entities
0:59 that need to be returned, if I just try to return Player, let's try to make this work here. We know Computer exists, let's try to get them.
1:07 What happens? Boom. Player is not JSON serializable. Failure. What we have to put here is not a random object out of the database, but a dictionary.
1:21 I've added this to_json method for every one of these. The one that's most interesting is this, this move here, so you can see this to_json,
1:30 that is going to create this dictionary to be sent back, right, and it's kind of encapsulated here with this class, which I think is pretty cool.
1:38 So what we got to go over here and say to_json now, we do that, boom, there's our computer, ID is 1. What about Michael, does he exist?
1:50 None type has no attribute to JSON. No, there is no Michael, so what we need to do instead of let it crash, is we need to say,
1:56 sorry, that user wasn't found. So we'll say if not player, and we can just say this, abort, sorry flask.abort(404), Not Found
2:07 Alright, now let's try it for one that doesn't exist. Okay, the page was not found, 404, that's fine.
2:13 Maybe we could give a better message about the user, but notice this is a little bit off. What we need to do is actually return a response
2:21 and set the status to be 404 in our little not found handler. Alright, there we go, 404 not found. That's what our client can use
2:30 to say no no no, that didn't work. We could put a little JSON message in here if we want, but it doesn't really matter for what we're doing.
2:36 Okay, go back to working one. Here we go. Okay, so our get user is implemented, and the interesting thing that we learned here
2:46 is this response message, a flask.abort(404) which really redirects over to this not found handler, and does whatever it's going to do.
2:55 So this is really important when people are trying to interact with things that don't exist.

Talk Python's Mastodon Michael Kennedy's Mastodon