#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.
0:03 This is pretty straightforward,
0:04 but it does introduce an entirely new concept.
0:08 I could ask for a user that doesn't exist, right?
0:11 Over here, we can go to our get_user,
0:14 and Mark, I don't think Mark exists in the database,
0:16 it just says, hey, we would find him.
0:19 I'm sure that Mark doesn't exist.
0:21 Eh, it still thinks it would find him.
0:23 And the status code is 200 OK.
0:25 So what we need to do is say,
0:27 yes, this user was found and here's their details,
0:30 or no, the user was not found, and here's what happened.
0:34 So it's really easy to do this with the code
0:36 we've already written and encapsulated in our service.
0:46 Alright, so, there's our user.
0:48 And if it wasn't null, we could return it.
0:51 Be super easy.
0:52 Let's go ahead and write that code really quick.
0:56 Now, to all these database entities
0:58 that need to be returned, if I just try to return Player,
1:01 let's try to make this work here.
1:04 We know Computer exists, let's try to get them.
1:06 What happens? Boom.
1:08 Player is not JSON serializable.
1:09 Failure.
1:11 What we have to put here is not
1:13 a random object out of the database, but a dictionary.
1:20 I've added this to_json method for every one of these.
1:23 The one that's most interesting is this,
1:26 this move here, so you can see this to_json,
1:29 that is going to create this dictionary to be sent back,
1:33 right, and it's kind of encapsulated here with this class,
1:35 which I think is pretty cool.
1:37 So what we got to go over here and say to_json now,
1:40 we do that, boom, there's our computer, ID is 1.
1:47 What about Michael, does he exist?
1:49 None type has no attribute to JSON.
1:51 No, there is no Michael, so what we need to do
1:53 instead of let it crash, is we need to say,
1:55 sorry, that user wasn't found.
1:56 So we'll say if not player,
1:59 and we can just say this, abort, sorry flask.abort(404), Not Found
2:06 Alright, now let's try it for one that doesn't exist.
2:09 Okay, the page was not found, 404, that's fine.
2:12 Maybe we could give a better message about the user,
2:14 but notice this is a little bit off.
2:18 What we need to do is actually return a response
2:20 and set the status to be 404
2:22 in our little not found handler.
2:26 Alright, there we go, 404 not found.
2:28 That's what our client can use
2:29 to say no no no, that didn't work.
2:30 We could put a little JSON message in here if we want,
2:33 but it doesn't really matter for what we're doing.
2:35 Okay, go back to working one.
2:39 Here we go.
2:40 Okay, so our get user is implemented,
2:43 and the interesting thing that we learned here
2:45 is this response message, a flask.abort(404)
2:49 which really redirects over to this not found handler,
2:52 and does whatever it's going to do.
2:54 So this is really important when people are trying
2:56 to interact with things that don't exist.